blob: 2b56d83e745f21f132fb89715c68ec2007b47e75 [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
219 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
220 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
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200224 if (arg1 >= BITS_PER_WORD) {
225 // 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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200389STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000390 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
391 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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200401STATIC int list_get(mp_parse_node_t *pn, int 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 Georgeb9791222014-01-23 00:34:21 +0000558 EMIT_ARG(load_const_verbatim_str, vstr_str(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
Damien429d7192013-10-04 19:53:11 +0100814 int 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 }
819 for (int i = 0; i < num_tail; i++) {
820 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100821 if (have_star_index < 0) {
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 }
830 if (have_star_index < 0) {
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 }
840 for (int i = 0; i < num_tail; i++) {
841 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 Georgeb9791222014-01-23 00:34:21 +0000863 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100864 break;
865 }
866 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100867 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100868 return;
869 }
870 } else {
Damiend99b0522013-12-21 18:17:45 +0000871 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
872 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100873 case PN_power:
874 // lhs is an index or attribute
875 c_assign_power(comp, pns, assign_kind);
876 break;
877
878 case PN_testlist_star_expr:
879 case PN_exprlist:
880 // lhs is a tuple
881 if (assign_kind != ASSIGN_STORE) {
882 goto bad_aug;
883 }
Damien George0288cf02014-04-11 11:53:00 +0000884 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100885 break;
886
887 case PN_atom_paren:
888 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000889 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100890 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100891 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000892 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
893 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100894 goto testlist_comp;
895 } else {
896 // parenthesis around 1 item, is just that item
897 pn = pns->nodes[0];
898 goto tail_recursion;
899 }
900 break;
901
902 case PN_atom_bracket:
903 // lhs is something in brackets
904 if (assign_kind != ASSIGN_STORE) {
905 goto bad_aug;
906 }
Damiend99b0522013-12-21 18:17:45 +0000907 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100908 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000909 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000910 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
911 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100912 goto testlist_comp;
913 } else {
914 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000915 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100916 }
917 break;
918
919 default:
Damien George9d181f62014-04-27 16:55:27 +0100920 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100921 }
922 return;
923
924 testlist_comp:
925 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000926 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
927 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
928 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100929 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000930 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000931 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000932 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100933 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000934 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
935 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000936 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100937 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100938 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100939 } else {
940 // sequence with 2 items
941 goto sequence_with_2_items;
942 }
943 } else {
944 // sequence with 2 items
945 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000946 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100947 }
948 return;
949 }
950 return;
951
Damien George9d181f62014-04-27 16:55:27 +0100952 cannot_assign:
953 compile_syntax_error(comp, pn, "can't assign to expression");
954 return;
955
Damien429d7192013-10-04 19:53:11 +0100956 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100957 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100958}
959
960// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100961// if we are not in CPython compatibility mode then:
962// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
963// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
964// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100965STATIC 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 +0100966 assert(n_pos_defaults >= 0);
967 assert(n_kw_defaults >= 0);
968
Damien429d7192013-10-04 19:53:11 +0100969 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000970 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100971 int nfree = 0;
972 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000973 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
974 id_info_t *id = &comp->scope_cur->id_info[i];
975 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
976 for (int j = 0; j < this_scope->id_info_len; j++) {
977 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100978 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000979#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100980 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000981#else
982 // in Micro Python we load closures using LOAD_FAST
Damien George7ff996c2014-09-08 23:05:16 +0100983 EMIT_ARG(load_fast, id->qst, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000984#endif
Damien318aec62013-12-10 18:28:17 +0000985 nfree += 1;
986 }
987 }
Damien429d7192013-10-04 19:53:11 +0100988 }
989 }
990 }
Damien429d7192013-10-04 19:53:11 +0100991
992 // make the function/closure
993 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100994 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100995 } else {
Damien George3558f622014-04-20 17:50:40 +0100996 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100997 }
998}
999
Damien George6be0b0a2014-08-15 14:30:52 +01001000STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001001 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001002 comp->have_star = true;
1003 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001004 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1005 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001006 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001007 } else {
1008 // named star
Damien429d7192013-10-04 19:53:11 +01001009 }
Damien George8b19db02014-04-11 23:25:34 +01001010 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001011
1012 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001013 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001014 // TODO do we need to do anything with this?
1015
1016 } else {
1017 mp_parse_node_t pn_id;
1018 mp_parse_node_t pn_colon;
1019 mp_parse_node_t pn_equal;
1020 if (MP_PARSE_NODE_IS_ID(pn)) {
1021 // this parameter is just an id
1022
1023 pn_id = pn;
1024 pn_colon = MP_PARSE_NODE_NULL;
1025 pn_equal = MP_PARSE_NODE_NULL;
1026
1027 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1028 // this parameter has a colon and/or equal specifier
1029
1030 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1031 pn_id = pns->nodes[0];
1032 pn_colon = pns->nodes[1];
1033 pn_equal = pns->nodes[2];
1034
1035 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001036 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001037 assert(0);
1038 return;
1039 }
1040
1041 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1042 // this parameter does not have a default value
1043
1044 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001045 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001046 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001047 return;
1048 }
1049
1050 } else {
1051 // this parameter has a default value
1052 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1053
Damien George8b19db02014-04-11 23:25:34 +01001054 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001055 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001056#if MICROPY_EMIT_CPYTHON
1057 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1058 compile_node(comp, pn_equal);
1059#else
Damien George69b89d22014-04-11 13:38:30 +00001060 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1061 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001062 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1063 // we need to do this here before we start building the map for the default keywords
1064 if (comp->num_default_params > 0) {
1065 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001066 } else {
1067 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001068 }
Damien George69b89d22014-04-11 13:38:30 +00001069 // first default dict param, so make the map
1070 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001071 }
Damien Georgef0778a72014-06-07 22:01:00 +01001072
1073 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001074 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001075 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001076 EMIT(store_map);
1077#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001078 } else {
Damien George69b89d22014-04-11 13:38:30 +00001079 comp->num_default_params += 1;
1080 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001081 }
1082 }
1083
1084 // TODO pn_colon not implemented
1085 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001086 }
1087}
1088
1089// leaves function object on stack
1090// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001091STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001092 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001093 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001094 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001095 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001096 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001097 }
1098
1099 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001100 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001101 uint old_num_dict_params = comp->num_dict_params;
1102 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001103
1104 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001105 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001106 comp->num_dict_params = 0;
1107 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001108 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001109
Damien Georgea91ac202014-10-05 19:01:34 +01001110 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001111 return MP_QSTR_NULL;
1112 }
1113
Damien Georgee337f1e2014-03-31 15:18:37 +01001114#if !MICROPY_EMIT_CPYTHON
1115 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001116 // the default keywords args may have already made the tuple; if not, do it now
1117 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001118 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001119 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001120 }
1121#endif
1122
Damien429d7192013-10-04 19:53:11 +01001123 // get the scope for this function
1124 scope_t *fscope = (scope_t*)pns->nodes[4];
1125
1126 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001127 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001128
1129 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001130 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001131 comp->num_dict_params = old_num_dict_params;
1132 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001133
1134 // return its name (the 'f' in "def f(...):")
1135 return fscope->simple_name;
1136}
1137
1138// leaves class object on stack
1139// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001140STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001141 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001142 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001143 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001144 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001145 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001146 }
1147
1148 EMIT(load_build_class);
1149
1150 // scope for this class
1151 scope_t *cscope = (scope_t*)pns->nodes[3];
1152
1153 // compile the class
1154 close_over_variables_etc(comp, cscope, 0, 0);
1155
1156 // get its name
Damien George968bf342014-04-27 19:12:05 +01001157 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001158
1159 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001160 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1161 mp_parse_node_t parents = pns->nodes[1];
1162 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1163 parents = MP_PARSE_NODE_NULL;
1164 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001165 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001166 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001167
1168 // return its name (the 'C' in class C(...):")
1169 return cscope->simple_name;
1170}
1171
Damien6cdd3af2013-10-05 18:08:26 +01001172// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001173STATIC 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 +00001174 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001175 return false;
1176 }
1177
1178 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001179 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001180 return true;
1181 }
1182
Damiend99b0522013-12-21 18:17:45 +00001183 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001184 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001185 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001186#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001187 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001188 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001189 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001190 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001191#endif
Damien3ef4abb2013-10-12 16:53:13 +01001192#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001193 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001194 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001195#endif
Damien6cdd3af2013-10-05 18:08:26 +01001196 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001197 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001198 }
1199
1200 return true;
1201}
1202
Damien George969a6b32014-12-10 22:07:04 +00001203STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001204 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001205 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001206 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1207
Damien6cdd3af2013-10-05 18:08:26 +01001208 // inherit emit options for this function/class definition
1209 uint emit_options = comp->scope_cur->emit_options;
1210
1211 // compile each decorator
1212 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001213 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001214 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1215 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001216
1217 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001218 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001219 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1220
1221 // check for built-in decorators
1222 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1223 // this was a built-in
1224 num_built_in_decorators += 1;
1225
1226 } else {
1227 // not a built-in, compile normally
1228
1229 // compile the decorator function
1230 compile_node(comp, name_nodes[0]);
1231 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001232 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001233 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001234 }
1235
1236 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001237 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001238 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001239 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001240 compile_node(comp, pns_decorator->nodes[1]);
1241 }
Damien429d7192013-10-04 19:53:11 +01001242 }
1243 }
1244
1245 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001246 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001247 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001248 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001249 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001250 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001251 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001252 } else {
1253 // shouldn't happen
1254 assert(0);
1255 }
1256
1257 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001258 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001259 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001260 }
1261
1262 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001263 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001264}
1265
Damien George969a6b32014-12-10 22:07:04 +00001266STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001267 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001268 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001269 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001270}
1271
Damien George6be0b0a2014-08-15 14:30:52 +01001272STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001273 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001274 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001275 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1276 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001277
1278 compile_node(comp, pns->nodes[0]); // base of the power node
1279
Damiend99b0522013-12-21 18:17:45 +00001280 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1281 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1282 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1283 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001284 for (int i = 0; i < n - 1; i++) {
1285 compile_node(comp, pns1->nodes[i]);
1286 }
Damiend99b0522013-12-21 18:17:45 +00001287 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1288 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001289 }
Damiend99b0522013-12-21 18:17:45 +00001290 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001291 // can't delete function calls
1292 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001293 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001294 compile_node(comp, pns1->nodes[0]);
1295 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001296 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1297 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001298 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001299 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001300 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001301 }
1302 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001303 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001304 }
1305
Damiend99b0522013-12-21 18:17:45 +00001306 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001307 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001308 }
Damiend99b0522013-12-21 18:17:45 +00001309 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1310 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1311 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1312 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001313 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1314
Damiend99b0522013-12-21 18:17:45 +00001315 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1316 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1317 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001318 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001319 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001320 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001321 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001322 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001323 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001324 c_del_stmt(comp, pns->nodes[0]);
1325 for (int i = 0; i < n; i++) {
1326 c_del_stmt(comp, pns1->nodes[i]);
1327 }
Damiend99b0522013-12-21 18:17:45 +00001328 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001329 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001330 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001331 } else {
1332 // sequence with 2 items
1333 goto sequence_with_2_items;
1334 }
1335 } else {
1336 // sequence with 2 items
1337 sequence_with_2_items:
1338 c_del_stmt(comp, pns->nodes[0]);
1339 c_del_stmt(comp, pns->nodes[1]);
1340 }
1341 } else {
1342 // tuple with 1 element
1343 c_del_stmt(comp, pn);
1344 }
1345 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001346 // TODO is there anything else to implement?
1347 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001348 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001349
1350 return;
1351
1352cannot_delete:
1353 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001354}
1355
Damien George969a6b32014-12-10 22:07:04 +00001356STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001357 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1358}
1359
Damien George969a6b32014-12-10 22:07:04 +00001360STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001361 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001362 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001363 }
Damien George090c9232014-10-17 14:08:49 +00001364 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001365 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001366}
1367
Damien George969a6b32014-12-10 22:07:04 +00001368STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001369 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001370 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001371 }
Damien George090c9232014-10-17 14:08:49 +00001372 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001373 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001374}
1375
Damien George969a6b32014-12-10 22:07:04 +00001376STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001377 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001378 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001379 return;
1380 }
Damiend99b0522013-12-21 18:17:45 +00001381 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001382 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001383 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001384 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001385 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001386 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1387 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 +01001388
Damien George6f355fd2014-04-10 14:11:31 +01001389 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001390 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1391 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1392 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001393 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001394 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1395 } else {
1396 compile_node(comp, pns->nodes[0]);
1397 }
1398 EMIT(return_value);
1399}
1400
Damien George969a6b32014-12-10 22:07:04 +00001401STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001402 compile_node(comp, pns->nodes[0]);
1403 EMIT(pop_top);
1404}
1405
Damien George969a6b32014-12-10 22:07:04 +00001406STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001407 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001408 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001409 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001410 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001411 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001412 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001413 compile_node(comp, pns->nodes[0]);
1414 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001415 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001416 } else {
1417 // raise x
1418 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001419 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001420 }
1421}
1422
Damien George635543c2014-04-10 12:56:52 +01001423// q_base holds the base of the name
1424// eg a -> q_base=a
1425// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001426STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001427 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001428 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1429 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001430 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001431 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001432 pn = pns->nodes[0];
1433 is_as = true;
1434 }
Damien George635543c2014-04-10 12:56:52 +01001435 if (MP_PARSE_NODE_IS_NULL(pn)) {
1436 // empty name (eg, from . import x)
1437 *q_base = MP_QSTR_;
1438 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1439 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001440 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001441 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001442 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001443 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001444 }
Damien George635543c2014-04-10 12:56:52 +01001445 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001446 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1447 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1448 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001449 // a name of the form a.b.c
1450 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001451 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001452 }
Damiend99b0522013-12-21 18:17:45 +00001453 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001454 int len = n - 1;
1455 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001456 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001457 }
Damien George55baff42014-01-21 21:40:13 +00001458 byte *q_ptr;
1459 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001460 for (int i = 0; i < n; i++) {
1461 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001462 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001463 }
Damien George39dc1452014-10-03 19:52:22 +01001464 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001465 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001466 memcpy(str_dest, str_src, str_src_len);
1467 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001468 }
Damien George635543c2014-04-10 12:56:52 +01001469 qstr q_full = qstr_build_end(q_ptr);
1470 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001471 if (is_as) {
1472 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001473 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001474 }
1475 }
1476 } else {
Damien George635543c2014-04-10 12:56:52 +01001477 // shouldn't happen
1478 assert(0);
Damien429d7192013-10-04 19:53:11 +01001479 }
1480 } else {
Damien George635543c2014-04-10 12:56:52 +01001481 // shouldn't happen
1482 assert(0);
Damien429d7192013-10-04 19:53:11 +01001483 }
1484}
1485
Damien George6be0b0a2014-08-15 14:30:52 +01001486STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001487 EMIT_ARG(load_const_small_int, 0); // level 0 import
1488 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1489 qstr q_base;
1490 do_import_name(comp, pn, &q_base);
1491 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001492}
1493
Damien George969a6b32014-12-10 22:07:04 +00001494STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001495 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1496}
1497
Damien George969a6b32014-12-10 22:07:04 +00001498STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001499 mp_parse_node_t pn_import_source = pns->nodes[0];
1500
1501 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1502 uint import_level = 0;
1503 do {
1504 mp_parse_node_t pn_rel;
1505 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)) {
1506 // This covers relative imports with dots only like "from .. import"
1507 pn_rel = pn_import_source;
1508 pn_import_source = MP_PARSE_NODE_NULL;
1509 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1510 // This covers relative imports starting with dot(s) like "from .foo import"
1511 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1512 pn_rel = pns_2b->nodes[0];
1513 pn_import_source = pns_2b->nodes[1];
1514 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1515 } else {
1516 // Not a relative import
1517 break;
1518 }
1519
1520 // get the list of . and/or ...'s
1521 mp_parse_node_t *nodes;
1522 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1523
1524 // count the total number of .'s
1525 for (int i = 0; i < n; i++) {
1526 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1527 import_level++;
1528 } else {
1529 // should be an MP_TOKEN_ELLIPSIS
1530 import_level += 3;
1531 }
1532 }
1533 } while (0);
1534
Damiend99b0522013-12-21 18:17:45 +00001535 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001536 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001537
1538 // build the "fromlist" tuple
1539#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001540 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001541#else
Damien George708c0732014-04-27 19:23:46 +01001542 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001543 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001544#endif
1545
1546 // do the import
Damien George635543c2014-04-10 12:56:52 +01001547 qstr dummy_q;
1548 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001549 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001550
Damien429d7192013-10-04 19:53:11 +01001551 } else {
Damien George635543c2014-04-10 12:56:52 +01001552 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001553
1554 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001555 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001556 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001557#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001558 {
1559 vstr_t *vstr = vstr_new();
1560 vstr_printf(vstr, "(");
1561 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001562 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1563 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1564 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001565 if (i > 0) {
1566 vstr_printf(vstr, ", ");
1567 }
1568 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001569 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001570 const byte *str = qstr_data(id2, &len);
1571 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001572 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001573 }
Damien02f89412013-12-12 15:13:36 +00001574 if (n == 1) {
1575 vstr_printf(vstr, ",");
1576 }
1577 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001578 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001579 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001580 }
Damiendb4c3612013-12-10 17:27:24 +00001581#else
1582 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001583 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1584 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1585 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001586 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001587 }
Damien Georgeb9791222014-01-23 00:34:21 +00001588 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001589#endif
1590
1591 // do the import
Damien George635543c2014-04-10 12:56:52 +01001592 qstr dummy_q;
1593 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001594 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001595 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1596 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1597 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001598 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001599 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001600 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001601 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001602 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001603 }
1604 }
1605 EMIT(pop_top);
1606 }
1607}
1608
Damien George584ba672014-12-21 17:26:45 +00001609STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1610 bool added;
1611 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1612 if (!added) {
1613 compile_syntax_error(comp, pn, "identifier already used");
1614 return;
1615 }
1616 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1617
1618 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1619 id_info = scope_find_global(comp->scope_cur, qst);
1620 if (id_info != NULL) {
1621 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1622 }
1623}
1624
Damien George969a6b32014-12-10 22:07:04 +00001625STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001626 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001627 mp_parse_node_t *nodes;
1628 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1629 for (int i = 0; i < n; i++) {
1630 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001631 }
1632 }
1633}
1634
Damien George584ba672014-12-21 17:26:45 +00001635STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1636 bool added;
1637 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1638 if (!added) {
1639 compile_syntax_error(comp, pn, "identifier already used");
1640 return;
1641 }
1642 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1643 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)) {
1644 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1645 return;
1646 }
1647 id_info->kind = ID_INFO_KIND_FREE;
1648 scope_close_over_in_parents(comp->scope_cur, qst);
1649}
1650
Damien George969a6b32014-12-10 22:07:04 +00001651STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001652 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001653 if (comp->scope_cur->kind == SCOPE_MODULE) {
1654 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1655 return;
1656 }
1657 mp_parse_node_t *nodes;
1658 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1659 for (int i = 0; i < n; i++) {
1660 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001661 }
1662 }
1663}
1664
Damien George969a6b32014-12-10 22:07:04 +00001665STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001666 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001667 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001668 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 +00001669 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001670 // assertion message
1671 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001672 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001673 }
Damien Georgeb9791222014-01-23 00:34:21 +00001674 EMIT_ARG(raise_varargs, 1);
1675 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001676}
1677
Damien George969a6b32014-12-10 22:07:04 +00001678STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001679 // TODO proper and/or short circuiting
1680
Damien George6f355fd2014-04-10 14:11:31 +01001681 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001682
Damien George391db862014-10-17 17:57:33 +00001683 // optimisation: don't emit anything when "if False" (not in CPython)
1684 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1685 uint l_fail = comp_next_label(comp);
1686 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001687
Damien George391db862014-10-17 17:57:33 +00001688 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001689
Damien George391db862014-10-17 17:57:33 +00001690 // optimisation: skip everything else when "if True" (not in CPython)
1691 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1692 goto done;
1693 }
1694
1695 if (
1696 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1697 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1698 // optimisation: don't jump if last instruction was return
1699 && !EMIT(last_emit_was_return_value)
1700 ) {
1701 // jump over elif/else blocks
1702 EMIT_ARG(jump, l_end);
1703 }
1704
1705 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001706 }
1707
Damien George235f9b32014-10-17 17:30:16 +00001708 // compile elif blocks (if any)
1709 mp_parse_node_t *pn_elif;
1710 int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1711 for (int i = 0; i < n_elif; i++) {
1712 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1713 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001714
Damien George391db862014-10-17 17:57:33 +00001715 // optimisation: don't emit anything when "if False" (not in CPython)
1716 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1717 uint l_fail = comp_next_label(comp);
1718 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001719
Damien George391db862014-10-17 17:57:33 +00001720 compile_node(comp, pns_elif->nodes[1]); // elif block
1721
1722 // optimisation: skip everything else when "elif True" (not in CPython)
1723 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1724 goto done;
1725 }
1726
1727 // optimisation: don't jump if last instruction was return
1728 if (!EMIT(last_emit_was_return_value)) {
1729 EMIT_ARG(jump, l_end);
1730 }
1731 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001732 }
1733 }
1734
1735 // compile else block
1736 compile_node(comp, pns->nodes[3]); // can be null
1737
Damien George391db862014-10-17 17:57:33 +00001738done:
Damien Georgeb9791222014-01-23 00:34:21 +00001739 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001740}
1741
Damien Georgecbddb272014-02-01 20:08:18 +00001742#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001743 uint16_t old_break_label = comp->break_label; \
1744 uint16_t old_continue_label = comp->continue_label; \
1745 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001746 uint break_label = comp_next_label(comp); \
1747 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001748 comp->break_label = break_label; \
1749 comp->continue_label = continue_label; \
1750 comp->break_continue_except_level = comp->cur_except_level;
1751
1752#define END_BREAK_CONTINUE_BLOCK \
1753 comp->break_label = old_break_label; \
1754 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001755 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001756
Damien George969a6b32014-12-10 22:07:04 +00001757STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001758 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001759
Damience89a212013-10-15 22:25:17 +01001760 // compared to CPython, we have an optimised version of while loops
1761#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001762 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001763 EMIT_ARG(setup_loop, break_label);
1764 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001765 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1766 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001767 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001768 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001769 }
Damien Georgeb9791222014-01-23 00:34:21 +00001770 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001771 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1772 // this is a small hack to agree with CPython
1773 if (!node_is_const_true(pns->nodes[0])) {
1774 EMIT(pop_block);
1775 }
Damience89a212013-10-15 22:25:17 +01001776#else
Damien George391db862014-10-17 17:57:33 +00001777 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1778 uint top_label = comp_next_label(comp);
1779 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1780 EMIT_ARG(jump, continue_label);
1781 }
1782 EMIT_ARG(label_assign, top_label);
1783 compile_node(comp, pns->nodes[1]); // body
1784 EMIT_ARG(label_assign, continue_label);
1785 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1786 }
Damience89a212013-10-15 22:25:17 +01001787#endif
1788
1789 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001790 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001791
1792 compile_node(comp, pns->nodes[2]); // else
1793
Damien Georgeb9791222014-01-23 00:34:21 +00001794 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001795}
1796
Damien George2ac4af62014-08-15 16:45:41 +01001797#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001798// This function compiles an optimised for-loop of the form:
1799// for <var> in range(<start>, <end>, <step>):
1800// <body>
1801// else:
1802// <else>
1803// <var> must be an identifier and <step> must be a small-int.
1804//
1805// Semantics of for-loop require:
1806// - final failing value should not be stored in the loop variable
1807// - if the loop never runs, the loop variable should never be assigned
1808// - assignments to <var>, <end> or <step> in the body do not alter the loop
1809// (<step> is a constant for us, so no need to worry about it changing)
1810//
1811// If <end> is a small-int, then the stack during the for-loop contains just
1812// the current value of <var>. Otherwise, the stack contains <end> then the
1813// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001814STATIC 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 +00001815 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001816
Damien George6f355fd2014-04-10 14:11:31 +01001817 uint top_label = comp_next_label(comp);
1818 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001819
Damien Georgee181c0d2014-12-12 17:19:56 +00001820 // put the end value on the stack if it's not a small-int constant
1821 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1822 if (end_on_stack) {
1823 compile_node(comp, pn_end);
1824 }
1825
1826 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001827 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001828
Damien Georgeb9791222014-01-23 00:34:21 +00001829 EMIT_ARG(jump, entry_label);
1830 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001831
Damien Georgec33ce602014-12-11 17:35:23 +00001832 // duplicate next value and store it to var
1833 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001834 c_assign(comp, pn_var, ASSIGN_STORE);
1835
Damienf3822fc2013-11-09 20:12:03 +00001836 // compile body
1837 compile_node(comp, pn_body);
1838
Damien Georgeb9791222014-01-23 00:34:21 +00001839 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001840
Damien Georgee181c0d2014-12-12 17:19:56 +00001841 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001842 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001843 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001844
Damien Georgeb9791222014-01-23 00:34:21 +00001845 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001846
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001847 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001848 if (end_on_stack) {
1849 EMIT(dup_top_two);
1850 EMIT(rot_two);
1851 } else {
1852 EMIT(dup_top);
1853 compile_node(comp, pn_end);
1854 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001855 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1856 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001857 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001858 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001859 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001860 }
Damien Georgeb9791222014-01-23 00:34:21 +00001861 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001862
1863 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001864 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001865
1866 compile_node(comp, pn_else);
1867
Damien Georgeb9791222014-01-23 00:34:21 +00001868 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001869
1870 // discard final value of var that failed the loop condition
1871 EMIT(pop_top);
1872
1873 // discard <end> value if it's on the stack
1874 if (end_on_stack) {
1875 EMIT(pop_top);
1876 }
Damienf72fd0e2013-11-06 20:20:49 +00001877}
Damien George2ac4af62014-08-15 16:45:41 +01001878#endif
Damienf72fd0e2013-11-06 20:20:49 +00001879
Damien George969a6b32014-12-10 22:07:04 +00001880STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001881#if !MICROPY_EMIT_CPYTHON
1882 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1883 // this is actually slower, but uses no heap memory
1884 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001885 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 +00001886 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001887 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1888 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1889 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1890 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001891 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1892 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001893 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001894 mp_parse_node_t pn_range_start;
1895 mp_parse_node_t pn_range_end;
1896 mp_parse_node_t pn_range_step;
1897 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001898 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001899 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001900 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001901 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001902 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001903 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001904 } else if (n_args == 2) {
1905 pn_range_start = args[0];
1906 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001907 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001908 } else {
1909 pn_range_start = args[0];
1910 pn_range_end = args[1];
1911 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001912 // We need to know sign of step. This is possible only if it's constant
1913 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1914 optimize = false;
1915 }
Damienf72fd0e2013-11-06 20:20:49 +00001916 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001917 }
1918 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001919 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1920 return;
1921 }
1922 }
1923 }
1924#endif
1925
Damien Georgecbddb272014-02-01 20:08:18 +00001926 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001927 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001928
Damien George6f355fd2014-04-10 14:11:31 +01001929 uint pop_label = comp_next_label(comp);
1930 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001931
Damience89a212013-10-15 22:25:17 +01001932 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1933#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001934 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001935#endif
1936
Damien429d7192013-10-04 19:53:11 +01001937 compile_node(comp, pns->nodes[1]); // iterator
1938 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001939 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001940 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001941 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1942 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001943 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001944 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001945 }
Damien Georgeb9791222014-01-23 00:34:21 +00001946 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001947 EMIT(for_iter_end);
1948
1949 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001950 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001951
Damience89a212013-10-15 22:25:17 +01001952#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001953 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001954#endif
Damien429d7192013-10-04 19:53:11 +01001955
1956 compile_node(comp, pns->nodes[3]); // else (not tested)
1957
Damien Georgeb9791222014-01-23 00:34:21 +00001958 EMIT_ARG(label_assign, break_label);
1959 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001960}
1961
Damien George6be0b0a2014-08-15 14:30:52 +01001962STATIC 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 +01001963 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001964 uint l1 = comp_next_label(comp);
1965 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001966
Damien Georgeb9791222014-01-23 00:34:21 +00001967 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001968 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001969
Damien429d7192013-10-04 19:53:11 +01001970 compile_node(comp, pn_body); // body
1971 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001972 EMIT_ARG(jump, success_label); // jump over exception handler
1973
1974 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001975 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001976
Damien George6f355fd2014-04-10 14:11:31 +01001977 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001978
1979 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001980 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1981 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001982
1983 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001984 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001985
Damiend99b0522013-12-21 18:17:45 +00001986 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001987 // this is a catch all exception handler
1988 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001989 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001990 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001991 return;
1992 }
1993 } else {
1994 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001995 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1996 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1997 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1998 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001999 // handler binds the exception to a local
2000 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002001 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002002 }
2003 }
2004 EMIT(dup_top);
2005 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002006 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00002007 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002008 }
2009
2010 EMIT(pop_top);
2011
2012 if (qstr_exception_local == 0) {
2013 EMIT(pop_top);
2014 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002015 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002016 }
2017
2018 EMIT(pop_top);
2019
Damien George6f355fd2014-04-10 14:11:31 +01002020 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002021 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002022 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002023 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002024 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002025 }
2026 compile_node(comp, pns_except->nodes[1]);
2027 if (qstr_exception_local != 0) {
2028 EMIT(pop_block);
2029 }
2030 EMIT(pop_except);
2031 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002032 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2033 EMIT_ARG(label_assign, l3);
2034 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2035 EMIT_ARG(store_id, qstr_exception_local);
2036 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002037
Damien George8dcc0c72014-03-27 10:55:21 +00002038 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002039 EMIT(end_finally);
2040 }
Damien Georgeb9791222014-01-23 00:34:21 +00002041 EMIT_ARG(jump, l2);
2042 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002043 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002044 }
2045
Damien George8dcc0c72014-03-27 10:55:21 +00002046 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002047 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002048 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002049
Damien Georgeb9791222014-01-23 00:34:21 +00002050 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002051 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002052 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002053}
2054
Damien George6be0b0a2014-08-15 14:30:52 +01002055STATIC 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 +01002056 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002057
Damien Georgeb9791222014-01-23 00:34:21 +00002058 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002059 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002060
Damien429d7192013-10-04 19:53:11 +01002061 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002062 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002063 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002064 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002065 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002066 } else {
2067 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2068 }
2069 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002070 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2071 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002072 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002073
Damien George8dcc0c72014-03-27 10:55:21 +00002074 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002075 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002076}
2077
Damien George969a6b32014-12-10 22:07:04 +00002078STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002079 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2080 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2081 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002082 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002083 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2084 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002085 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002086 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002087 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002088 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002089 // no finally
2090 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2091 } else {
2092 // have finally
Damiend99b0522013-12-21 18:17:45 +00002093 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 +01002094 }
2095 } else {
2096 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002097 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002098 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002099 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002100 }
2101 } else {
2102 // shouldn't happen
2103 assert(0);
2104 }
2105}
2106
Damien George6be0b0a2014-08-15 14:30:52 +01002107STATIC 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 +01002108 if (n == 0) {
2109 // no more pre-bits, compile the body of the with
2110 compile_node(comp, body);
2111 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002112 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002113 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002114 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002115 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002116 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002117 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002118 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2119 } else {
2120 // this pre-bit is just an expression
2121 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002122 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002123 EMIT(pop_top);
2124 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002125 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002126 // compile additional pre-bits and the body
2127 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2128 // finish this with block
2129 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002130 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2131 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002132 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002133 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002134 EMIT(end_finally);
2135 }
2136}
2137
Damien George969a6b32014-12-10 22:07:04 +00002138STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002139 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002140 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002141 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2142 assert(n > 0);
2143
2144 // compile in a nested fashion
2145 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2146}
2147
Damien George969a6b32014-12-10 22:07:04 +00002148STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002149 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002150 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2151 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002152 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002153 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002154 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002155 EMIT(pop_top);
2156
Damien429d7192013-10-04 19:53:11 +01002157 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002158 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002159 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002160 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
2161 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002162 // do nothing with a lonely constant
2163 } else {
2164 compile_node(comp, pns->nodes[0]); // just an expression
2165 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2166 }
Damien429d7192013-10-04 19:53:11 +01002167 }
2168 } else {
Damiend99b0522013-12-21 18:17:45 +00002169 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2170 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002171 if (kind == PN_expr_stmt_augassign) {
2172 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2173 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002174 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002175 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002176 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002177 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2178 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2179 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2180 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2181 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2182 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2183 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2184 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2185 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2186 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2187 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2188 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2189 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
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;
2353 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2354 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002355 }
2356 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002357 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2358 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2359 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002360 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002361 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002362 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002363 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002364 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002365 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002366 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002367 }
2368 } else {
2369 // shouldn't happen
2370 assert(0);
2371 }
2372 } else {
2373 // shouldn't happen
2374 assert(0);
2375 }
2376 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002377 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002378 }
2379 }
2380 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002381 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002382 EMIT_ARG(jump, l_end);
2383 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002384 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002385 EMIT(rot_two);
2386 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002387 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002388 }
2389}
2390
Damien George969a6b32014-12-10 22:07:04 +00002391STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002392 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002393}
2394
Damien George969a6b32014-12-10 22:07:04 +00002395STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002396 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002397}
2398
Damien George969a6b32014-12-10 22:07:04 +00002399STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002400 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002401}
2402
Damien George969a6b32014-12-10 22:07:04 +00002403STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002404 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002405}
2406
Damien George969a6b32014-12-10 22:07:04 +00002407STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002408 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002409 compile_node(comp, pns->nodes[0]);
2410 for (int i = 1; i + 1 < num_nodes; i += 2) {
2411 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002412 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002413 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002414 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002415 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002416 } else {
2417 // shouldn't happen
2418 assert(0);
2419 }
2420 }
2421}
2422
Damien George969a6b32014-12-10 22:07:04 +00002423STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002424 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002425 compile_node(comp, pns->nodes[0]);
2426 for (int i = 1; i + 1 < num_nodes; i += 2) {
2427 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002428 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002429 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002430 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002431 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002432 } else {
2433 // shouldn't happen
2434 assert(0);
2435 }
2436 }
2437}
2438
Damien George969a6b32014-12-10 22:07:04 +00002439STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002440 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002441 compile_node(comp, pns->nodes[0]);
2442 for (int i = 1; i + 1 < num_nodes; i += 2) {
2443 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002444 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002445 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002446 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002447 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002448 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002449 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002450 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002451 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002452 } else {
2453 // shouldn't happen
2454 assert(0);
2455 }
2456 }
2457}
2458
Damien George969a6b32014-12-10 22:07:04 +00002459STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002460 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002461 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002462 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002463 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002464 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002465 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002466 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002467 } else {
2468 // shouldn't happen
2469 assert(0);
2470 }
2471}
2472
Damien George969a6b32014-12-10 22:07:04 +00002473STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002474 // this is to handle special super() call
2475 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2476
2477 compile_generic_all_nodes(comp, pns);
2478}
2479
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002480STATIC 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 +01002481 // function to call is on top of stack
2482
Damien George35e2a4e2014-02-05 00:51:47 +00002483#if !MICROPY_EMIT_CPYTHON
2484 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002485 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 +00002486 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002487 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002488 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002489 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002490 // first argument found; load it and call super
Damien George2bf7c092014-04-09 15:26:46 +01002491 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002492 EMIT_ARG(call_function, 2, 0, 0);
2493 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002494 }
2495 }
Damien George01039b52014-12-21 17:44:27 +00002496 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002497 return;
2498 }
2499#endif
2500
Damien George2c0842b2014-04-27 16:46:51 +01002501 // get the list of arguments
2502 mp_parse_node_t *args;
2503 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002504
Damien George2c0842b2014-04-27 16:46:51 +01002505 // compile the arguments
2506 // Rather than calling compile_node on the list, we go through the list of args
2507 // explicitly here so that we can count the number of arguments and give sensible
2508 // error messages.
2509 int n_positional = n_positional_extra;
2510 uint n_keyword = 0;
2511 uint star_flags = 0;
2512 for (int i = 0; i < n_args; i++) {
2513 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2514 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2515 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2516 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2517 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2518 return;
2519 }
2520 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2521 compile_node(comp, pns_arg->nodes[0]);
2522 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2523 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2524 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2525 return;
2526 }
2527 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2528 compile_node(comp, pns_arg->nodes[0]);
2529 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2530 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2531 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2532 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2533 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2534 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2535 return;
2536 }
Damien George968bf342014-04-27 19:12:05 +01002537 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002538 compile_node(comp, pns2->nodes[0]);
2539 n_keyword += 1;
2540 } else {
2541 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2542 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2543 n_positional++;
2544 }
2545 } else {
2546 goto normal_argument;
2547 }
2548 } else {
2549 normal_argument:
2550 if (n_keyword > 0) {
2551 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2552 return;
2553 }
2554 compile_node(comp, args[i]);
2555 n_positional++;
2556 }
Damien429d7192013-10-04 19:53:11 +01002557 }
2558
Damien George2c0842b2014-04-27 16:46:51 +01002559 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002560 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002561 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002562 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002563 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002564 }
Damien429d7192013-10-04 19:53:11 +01002565}
2566
Damien George969a6b32014-12-10 22:07:04 +00002567STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002568 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002569 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002570 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 +01002571 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002572 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2573 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002574 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002575 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002576 i += 1;
2577 } else {
2578 compile_node(comp, pns->nodes[i]);
2579 }
Damien George35e2a4e2014-02-05 00:51:47 +00002580 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002581 }
2582}
2583
Damien George969a6b32014-12-10 22:07:04 +00002584STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002585 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002586 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002587}
2588
Damien George969a6b32014-12-10 22:07:04 +00002589STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002590 // a list of strings
Damien63321742013-12-10 17:41:49 +00002591
2592 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002593 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002594 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002595 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002596 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002597 int pn_kind;
2598 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2599 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2600 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2601 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2602 } else {
2603 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2604 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002605 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2606 pn_kind = MP_PARSE_NODE_STRING;
2607 } else {
2608 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2609 pn_kind = MP_PARSE_NODE_BYTES;
2610 }
Damien George40f3c022014-07-03 13:25:24 +01002611 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002612 }
Damien63321742013-12-10 17:41:49 +00002613 if (i == 0) {
2614 string_kind = pn_kind;
2615 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002616 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002617 return;
2618 }
Damien429d7192013-10-04 19:53:11 +01002619 }
Damien63321742013-12-10 17:41:49 +00002620
Damien63321742013-12-10 17:41:49 +00002621 // concatenate string/bytes
Damien George4c81ba82015-01-13 16:21:23 +00002622 byte *s_dest;
2623 mp_obj_t obj = mp_obj_str_builder_start(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, n_bytes, &s_dest);
Damien63321742013-12-10 17:41:49 +00002624 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002625 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002626 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002627 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2628 memcpy(s_dest, s, s_len);
2629 s_dest += s_len;
2630 } else {
2631 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002632 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2633 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002634 }
Damien63321742013-12-10 17:41:49 +00002635 }
Damien George4c81ba82015-01-13 16:21:23 +00002636 EMIT_ARG(load_const_obj, mp_obj_str_builder_end(obj));
Damien429d7192013-10-04 19:53:11 +01002637}
2638
2639// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002640STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002641 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2642 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2643 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002644
Damien George36db6bc2014-05-07 17:24:22 +01002645 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002646 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002647 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 +01002648 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002649 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002650 }
2651
2652 // get the scope for this comprehension
2653 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2654
2655 // compile the comprehension
2656 close_over_variables_etc(comp, this_scope, 0, 0);
2657
2658 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2659 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002660 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002661}
2662
Damien George969a6b32014-12-10 22:07:04 +00002663STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002664 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002665 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002666 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2667 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2668 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2669 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2670 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2671 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2672 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002673 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002674 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002675 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002676 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002677 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002678 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002679 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002680 // generator expression
2681 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2682 } else {
2683 // tuple with 2 items
2684 goto tuple_with_2_items;
2685 }
2686 } else {
2687 // tuple with 2 items
2688 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002689 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002690 }
2691 } else {
2692 // parenthesis around a single item, is just that item
2693 compile_node(comp, pns->nodes[0]);
2694 }
2695}
2696
Damien George969a6b32014-12-10 22:07:04 +00002697STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002698 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002699 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002700 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002701 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2702 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2703 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2704 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2705 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002706 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002707 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002708 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002709 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002710 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002711 // list of many items
2712 compile_node(comp, pns2->nodes[0]);
2713 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002714 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002715 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002716 // list comprehension
2717 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2718 } else {
2719 // list with 2 items
2720 goto list_with_2_items;
2721 }
2722 } else {
2723 // list with 2 items
2724 list_with_2_items:
2725 compile_node(comp, pns2->nodes[0]);
2726 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002727 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002728 }
2729 } else {
2730 // list with 1 item
2731 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002732 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002733 }
2734}
2735
Damien George969a6b32014-12-10 22:07:04 +00002736STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002737 mp_parse_node_t pn = pns->nodes[0];
2738 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002739 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002740 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002741 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2742 pns = (mp_parse_node_struct_t*)pn;
2743 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002744 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002745 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002746 compile_node(comp, pn);
2747 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002748 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2749 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2750 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2751 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002752 // dict/set with multiple elements
2753
2754 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002755 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002756 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2757
2758 // first element sets whether it's a dict or set
2759 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002760 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002761 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002762 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002763 compile_node(comp, pns->nodes[0]);
2764 EMIT(store_map);
2765 is_dict = true;
2766 } else {
2767 // a set
2768 compile_node(comp, pns->nodes[0]); // 1st value of set
2769 is_dict = false;
2770 }
2771
2772 // process rest of elements
2773 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002774 mp_parse_node_t pn = nodes[i];
2775 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002776 compile_node(comp, pn);
2777 if (is_dict) {
2778 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002779 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002780 return;
2781 }
2782 EMIT(store_map);
2783 } else {
2784 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002785 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002786 return;
2787 }
2788 }
2789 }
2790
Damien Georgee37dcaa2014-12-27 17:07:16 +00002791 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002792 // if it's a set, build it
2793 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002794 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002795 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002796 #endif
Damiend99b0522013-12-21 18:17:45 +00002797 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002798 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002799 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002800 // a dictionary comprehension
2801 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2802 } else {
2803 // a set comprehension
2804 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2805 }
2806 } else {
2807 // shouldn't happen
2808 assert(0);
2809 }
2810 } else {
2811 // set with one element
2812 goto set_with_one_element;
2813 }
2814 } else {
2815 // set with one element
2816 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002817 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002818 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002819 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002820 #else
2821 assert(0);
2822 #endif
Damien429d7192013-10-04 19:53:11 +01002823 }
2824}
2825
Damien George969a6b32014-12-10 22:07:04 +00002826STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002827 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002828}
2829
Damien George969a6b32014-12-10 22:07:04 +00002830STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002831 // object who's index we want is on top of stack
2832 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002833 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002834}
2835
Damien George969a6b32014-12-10 22:07:04 +00002836STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002837 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002838 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002839}
2840
Damien George83204f32014-12-27 17:20:41 +00002841#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002842STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002843 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2844 mp_parse_node_t pn = pns->nodes[0];
2845 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002846 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002847 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2848 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002849 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2850 pns = (mp_parse_node_struct_t*)pn;
2851 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002852 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002853 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002854 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002855 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002857 } else {
2858 // [?::x]
2859 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002861 }
Damiend99b0522013-12-21 18:17:45 +00002862 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002863 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002864 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2865 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2866 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2867 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002868 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002869 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002870 } else {
2871 // [?:x:x]
2872 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002873 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002874 }
2875 } else {
2876 // [?:x]
2877 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002878 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002879 }
2880 } else {
2881 // [?:x]
2882 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002883 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002884 }
2885}
2886
Damien George969a6b32014-12-10 22:07:04 +00002887STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002888 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002889 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2890 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002891}
2892
Damien George969a6b32014-12-10 22:07:04 +00002893STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002894 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002895 compile_subscript_3_helper(comp, pns);
2896}
Damien George83204f32014-12-27 17:20:41 +00002897#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002898
Damien George969a6b32014-12-10 22:07:04 +00002899STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002900 // if this is called then we are compiling a dict key:value pair
2901 compile_node(comp, pns->nodes[1]); // value
2902 compile_node(comp, pns->nodes[0]); // key
2903}
2904
Damien George969a6b32014-12-10 22:07:04 +00002905STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002906 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002907 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002908 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002909}
2910
Damien George969a6b32014-12-10 22:07:04 +00002911STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002912 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002913 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002914 return;
2915 }
Damiend99b0522013-12-21 18:17:45 +00002916 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002917 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002918 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002919 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2920 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002921 compile_node(comp, pns->nodes[0]);
2922 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002923 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002924 EMIT(yield_from);
2925 } else {
2926 compile_node(comp, pns->nodes[0]);
2927 EMIT(yield_value);
2928 }
2929}
2930
Damiend99b0522013-12-21 18:17:45 +00002931typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002932STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002933#define nc NULL
2934#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002935#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002936#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002937#undef nc
2938#undef c
2939#undef DEF_RULE
2940};
2941
Damien George6be0b0a2014-08-15 14:30:52 +01002942STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002943 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002944 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002945 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002946 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002947 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002948 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002949 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002950 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002951 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002952 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2953 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2954 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2955 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002956 case MP_PARSE_NODE_TOKEN:
2957 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002958 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002959 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002960 // do nothing
2961 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002962 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002963 }
2964 break;
Damien429d7192013-10-04 19:53:11 +01002965 default: assert(0);
2966 }
2967 } else {
Damiend99b0522013-12-21 18:17:45 +00002968 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002969 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002970 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George4c81ba82015-01-13 16:21:23 +00002971 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2972 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_bytes) {
2973 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01002974 } else {
Damien George5042bce2014-05-25 22:06:06 +01002975 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2976 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002977#if MICROPY_DEBUG_PRINTERS
Damien George01039b52014-12-21 17:44:27 +00002978 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien George5042bce2014-05-25 22:06:06 +01002979 mp_parse_node_print(pn, 0);
2980#endif
2981 compile_syntax_error(comp, pn, "internal compiler error");
2982 } else {
2983 f(comp, pns);
2984 }
Damien429d7192013-10-04 19:53:11 +01002985 }
2986 }
2987}
2988
Damien George2ac4af62014-08-15 16:45:41 +01002989STATIC 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 +01002990 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002991 qstr param_name = MP_QSTR_NULL;
2992 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002993 if (MP_PARSE_NODE_IS_ID(pn)) {
2994 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002995 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002996 // comes after a star, so counts as a keyword-only parameter
2997 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002998 } else {
Damien George2827d622014-04-27 15:50:52 +01002999 // comes before a star, so counts as a positional parameter
3000 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003001 }
Damienb14de212013-10-06 00:28:28 +01003002 } else {
Damiend99b0522013-12-21 18:17:45 +00003003 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3004 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3005 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3006 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003007 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003008 // comes after a star, so counts as a keyword-only parameter
3009 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003010 } else {
Damien George2827d622014-04-27 15:50:52 +01003011 // comes before a star, so counts as a positional parameter
3012 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003013 }
Damiend99b0522013-12-21 18:17:45 +00003014 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003015 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003016 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003017 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003018 // bare star
3019 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003020 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003021 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003022 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003023 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003024 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01003025 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01003026 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003027 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003028 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3029 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003030 } else {
3031 // shouldn't happen
3032 assert(0);
3033 }
Damiend99b0522013-12-21 18:17:45 +00003034 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
3035 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003036 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003037 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003038 } else {
Damienb14de212013-10-06 00:28:28 +01003039 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01003040 assert(0);
3041 }
Damien429d7192013-10-04 19:53:11 +01003042 }
3043
Damien George2827d622014-04-27 15:50:52 +01003044 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003045 bool added;
3046 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3047 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003048 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003049 return;
3050 }
Damien429d7192013-10-04 19:53:11 +01003051 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003052 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003053 }
3054}
3055
Damien George2827d622014-04-27 15:50:52 +01003056STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003057 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003058}
3059
Damien George2827d622014-04-27 15:50:52 +01003060STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003061 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3062}
3063
3064STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3065 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3066 // no annotation
3067 return;
3068 }
3069
3070 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3071 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3072 // named parameter with possible annotation
3073 // fallthrough
3074 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3075 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3076 // named star with possible annotation
3077 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3078 // fallthrough
3079 } else {
3080 // no annotation
3081 return;
3082 }
3083 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3084 // double star with possible annotation
3085 // fallthrough
3086 } else {
3087 // no annotation
3088 return;
3089 }
3090
3091 mp_parse_node_t pn_annotation = pns->nodes[1];
3092
3093 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3094 #if MICROPY_EMIT_NATIVE
3095 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3096 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3097 assert(id_info != NULL);
3098
3099 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3100 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3101 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3102 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3103 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003104 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003105 }
3106 }
3107 #endif // MICROPY_EMIT_NATIVE
3108 }
Damien429d7192013-10-04 19:53:11 +01003109}
3110
Damien George6be0b0a2014-08-15 14:30:52 +01003111STATIC 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 +01003112 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003113 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003114 // no more nested if/for; compile inner expression
3115 compile_node(comp, pn_inner_expr);
3116 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003117 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003118 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003119 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003120 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003121 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003122 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003123 #endif
Damien429d7192013-10-04 19:53:11 +01003124 } else {
3125 EMIT(yield_value);
3126 EMIT(pop_top);
3127 }
Damiend99b0522013-12-21 18:17:45 +00003128 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003129 // if condition
Damiend99b0522013-12-21 18:17:45 +00003130 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003131 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3132 pn_iter = pns_comp_if->nodes[1];
3133 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003134 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003135 // for loop
Damiend99b0522013-12-21 18:17:45 +00003136 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003137 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003138 uint l_end2 = comp_next_label(comp);
3139 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003140 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003141 EMIT_ARG(label_assign, l_top2);
3142 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003143 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3144 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 +00003145 EMIT_ARG(jump, l_top2);
3146 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003147 EMIT(for_iter_end);
3148 } else {
3149 // shouldn't happen
3150 assert(0);
3151 }
3152}
3153
Damien George1463c1f2014-04-25 23:52:57 +01003154STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3155#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003156 // see http://www.python.org/dev/peps/pep-0257/
3157
3158 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003159 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003160 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003161 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003162 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003163 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3164 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003165 for (int i = 0; i < num_nodes; i++) {
3166 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003167 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 +00003168 // not a newline, so this is the first statement; finish search
3169 break;
3170 }
3171 }
3172 // 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 +00003173 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003174 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003175 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003176 } else {
3177 return;
3178 }
3179
3180 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003181 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3182 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003183 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3184 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3185 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3186 // compile the doc string
3187 compile_node(comp, pns->nodes[0]);
3188 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003189 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003190 }
3191 }
Damien George1463c1f2014-04-25 23:52:57 +01003192#endif
Damien429d7192013-10-04 19:53:11 +01003193}
3194
Damien George1463c1f2014-04-25 23:52:57 +01003195STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003196 comp->pass = pass;
3197 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003198 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003199 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003200
Damien George36db6bc2014-05-07 17:24:22 +01003201 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003202 // reset maximum stack sizes in scope
3203 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003204 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003205 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003206 }
3207
Damien5ac1b2e2013-10-18 19:58:12 +01003208#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003209 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003210 scope_print_info(scope);
3211 }
Damien5ac1b2e2013-10-18 19:58:12 +01003212#endif
Damien429d7192013-10-04 19:53:11 +01003213
3214 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003215 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3216 assert(scope->kind == SCOPE_MODULE);
3217 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3218 compile_node(comp, pns->nodes[0]); // compile the expression
3219 EMIT(return_value);
3220 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003221 if (!comp->is_repl) {
3222 check_for_doc_string(comp, scope->pn);
3223 }
Damien429d7192013-10-04 19:53:11 +01003224 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003225 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003226 EMIT(return_value);
3227 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003228 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3229 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3230 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003231
3232 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003233 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003234 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003235 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003236 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003237 } else {
3238 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003239
Damien George2ac4af62014-08-15 16:45:41 +01003240 // argument annotations
3241 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3242
3243 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003244 mp_parse_node_t pn_annotation = pns->nodes[2];
3245 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3246 #if MICROPY_EMIT_NATIVE
3247 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3248 // nodes[2] can be null or a test-expr
3249 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3250 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3251 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3252 } else {
3253 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3254 }
Damien George2ac4af62014-08-15 16:45:41 +01003255 }
Damien Georgea5190a72014-08-15 22:39:08 +01003256 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003257 }
Damien George2ac4af62014-08-15 16:45:41 +01003258 }
Damien429d7192013-10-04 19:53:11 +01003259
3260 compile_node(comp, pns->nodes[3]); // 3 is function body
3261 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003262 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003263 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003264 EMIT(return_value);
3265 }
3266 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003267 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3268 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3269 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003270
3271 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003272 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003273 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003274 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003275 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3276 }
3277
3278 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003279
3280 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3281 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3282 EMIT(pop_top);
3283 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3284 }
Damien429d7192013-10-04 19:53:11 +01003285 EMIT(return_value);
3286 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3287 // a bit of a hack at the moment
3288
Damiend99b0522013-12-21 18:17:45 +00003289 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3290 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3291 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3292 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3293 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003294
Damien George708c0732014-04-27 19:23:46 +01003295 // We need a unique name for the comprehension argument (the iterator).
3296 // CPython uses .0, but we should be able to use anything that won't
3297 // clash with a user defined variable. Best to use an existing qstr,
3298 // so we use the blank qstr.
3299#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003300 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003301#else
3302 qstr qstr_arg = MP_QSTR_;
3303#endif
Damien George36db6bc2014-05-07 17:24:22 +01003304 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003305 bool added;
3306 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3307 assert(added);
3308 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003309 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003310 }
3311
3312 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003313 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003314 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003315 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003316 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003317 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003318 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003319 #endif
Damien429d7192013-10-04 19:53:11 +01003320 }
3321
Damien George6f355fd2014-04-10 14:11:31 +01003322 uint l_end = comp_next_label(comp);
3323 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003324 EMIT_ARG(load_id, qstr_arg);
3325 EMIT_ARG(label_assign, l_top);
3326 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003327 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3328 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003329 EMIT_ARG(jump, l_top);
3330 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003331 EMIT(for_iter_end);
3332
3333 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003334 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003335 }
3336 EMIT(return_value);
3337 } else {
3338 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003339 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3340 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3341 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003342
Damien George36db6bc2014-05-07 17:24:22 +01003343 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003344 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003345 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003346 assert(added);
3347 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003348 }
3349
Damien Georgeb9791222014-01-23 00:34:21 +00003350 EMIT_ARG(load_id, MP_QSTR___name__);
3351 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003352 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 +00003353 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003354
3355 check_for_doc_string(comp, pns->nodes[2]);
3356 compile_node(comp, pns->nodes[2]); // 2 is class body
3357
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003358 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003359 assert(id != NULL);
3360 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003361 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003362 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003363#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003364 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003365#else
Damien George2bf7c092014-04-09 15:26:46 +01003366 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003367#endif
Damien429d7192013-10-04 19:53:11 +01003368 }
3369 EMIT(return_value);
3370 }
3371
Damien415eb6f2013-10-05 12:19:06 +01003372 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003373
3374 // make sure we match all the exception levels
3375 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003376}
3377
Damien George094d4502014-04-02 17:31:27 +01003378#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003379// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003380STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003381 comp->pass = pass;
3382 comp->scope_cur = scope;
3383 comp->next_label = 1;
3384
3385 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003386 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003387 return;
3388 }
3389
Damien George36db6bc2014-05-07 17:24:22 +01003390 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003391 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003392 }
3393
Damien826005c2013-10-05 23:17:28 +01003394 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003395 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3396 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3397 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003398
Damiend99b0522013-12-21 18:17:45 +00003399 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003400
Damiena2f2f7d2013-10-06 00:14:13 +01003401 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003402 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003403 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003404 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003405 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003406 }
3407
Damiend99b0522013-12-21 18:17:45 +00003408 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003409
Damiend99b0522013-12-21 18:17:45 +00003410 mp_parse_node_t pn_body = pns->nodes[3]; // body
3411 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003412 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3413
Damien Georgecbd2f742014-01-19 11:48:48 +00003414 /*
Damien George36db6bc2014-05-07 17:24:22 +01003415 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003416 //printf("----\n");
3417 scope_print_info(scope);
3418 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003419 */
Damien826005c2013-10-05 23:17:28 +01003420
3421 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003422 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3423 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003424 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3425 // no instructions
3426 continue;
3427 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3428 // an instruction; fall through
3429 } else {
3430 // not an instruction; error
3431 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3432 return;
3433 }
Damiend99b0522013-12-21 18:17:45 +00003434 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3435 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3436 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3437 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3438 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3439 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3440 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3441 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3442 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3443 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003444 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3445
3446 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003447 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003448 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003449 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003450 return;
3451 }
Damien George6f355fd2014-04-10 14:11:31 +01003452 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003453 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003454 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003455 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003456 } else if (op == MP_QSTR_align) {
3457 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3458 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3459 return;
3460 }
Damien George36db6bc2014-05-07 17:24:22 +01003461 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003462 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3463 }
3464 } else if (op == MP_QSTR_data) {
3465 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3466 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3467 return;
3468 }
Damien George36db6bc2014-05-07 17:24:22 +01003469 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003470 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003471 for (uint i = 1; i < n_args; i++) {
3472 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3473 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3474 return;
3475 }
3476 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3477 }
3478 }
Damien826005c2013-10-05 23:17:28 +01003479 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003480 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003481 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003482 }
3483 }
3484 }
3485
Damien George36db6bc2014-05-07 17:24:22 +01003486 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003487 bool success = EMIT_INLINE_ASM(end_pass);
3488 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003489 // TODO get proper exception from inline assembler
3490 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003491 }
Damienb05d7072013-10-05 13:37:10 +01003492 }
Damien429d7192013-10-04 19:53:11 +01003493}
Damien George094d4502014-04-02 17:31:27 +01003494#endif
Damien429d7192013-10-04 19:53:11 +01003495
Damien George1463c1f2014-04-25 23:52:57 +01003496STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003497#if !MICROPY_EMIT_CPYTHON
3498 // in Micro Python we put the *x parameter after all other parameters (except **y)
3499 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3500 id_info_t *id_param = NULL;
3501 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3502 id_info_t *id = &scope->id_info[i];
3503 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3504 if (id_param != NULL) {
3505 // swap star param with last param
3506 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3507 }
3508 break;
3509 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3510 id_param = id;
3511 }
3512 }
3513 }
3514#endif
3515
Damien429d7192013-10-04 19:53:11 +01003516 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003517 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003518 scope->num_locals = 0;
3519 for (int i = 0; i < scope->id_info_len; i++) {
3520 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003521 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003522 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3523 continue;
3524 }
3525 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3526 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3527 }
Damien George2827d622014-04-27 15:50:52 +01003528 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003529 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003530 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003531 }
3532 }
3533
3534 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003535#if MICROPY_EMIT_CPYTHON
3536 int num_cell = 0;
3537#endif
Damien9ecbcff2013-12-11 00:41:43 +00003538 for (int i = 0; i < scope->id_info_len; i++) {
3539 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003540#if MICROPY_EMIT_CPYTHON
3541 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003542 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003543 id->local_num = num_cell;
3544 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003545 }
Damien George6baf76e2013-12-30 22:32:17 +00003546#else
3547 // in Micro Python the cells come right after the fast locals
3548 // parameters are not counted here, since they remain at the start
3549 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003550 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003551 id->local_num = scope->num_locals;
3552 scope->num_locals += 1;
3553 }
3554#endif
Damien9ecbcff2013-12-11 00:41:43 +00003555 }
Damien9ecbcff2013-12-11 00:41:43 +00003556
3557 // compute the index of free vars (freevars[idx] in CPython)
3558 // make sure they are in the order of the parent scope
3559 if (scope->parent != NULL) {
3560 int num_free = 0;
3561 for (int i = 0; i < scope->parent->id_info_len; i++) {
3562 id_info_t *id = &scope->parent->id_info[i];
3563 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3564 for (int j = 0; j < scope->id_info_len; j++) {
3565 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003566 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003567 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003568#if MICROPY_EMIT_CPYTHON
3569 // in CPython the frees are numbered after the cells
3570 id2->local_num = num_cell + num_free;
3571#else
3572 // in Micro Python the frees come first, before the params
3573 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003574#endif
3575 num_free += 1;
3576 }
3577 }
3578 }
Damien429d7192013-10-04 19:53:11 +01003579 }
Damien George6baf76e2013-12-30 22:32:17 +00003580#if !MICROPY_EMIT_CPYTHON
3581 // in Micro Python shift all other locals after the free locals
3582 if (num_free > 0) {
3583 for (int i = 0; i < scope->id_info_len; i++) {
3584 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003585 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003586 id->local_num += num_free;
3587 }
3588 }
Damien George2827d622014-04-27 15:50:52 +01003589 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 +00003590 scope->num_locals += num_free;
3591 }
3592#endif
Damien429d7192013-10-04 19:53:11 +01003593 }
3594
Damien George8725f8f2014-02-15 19:33:11 +00003595 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003596
3597#if MICROPY_EMIT_CPYTHON
3598 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003599 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) {
3600 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003601 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003602 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003603 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 +00003604 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003605 }
3606 }
Damien George882b3632014-04-02 15:56:31 +01003607#endif
3608
Damien429d7192013-10-04 19:53:11 +01003609 int num_free = 0;
3610 for (int i = 0; i < scope->id_info_len; i++) {
3611 id_info_t *id = &scope->id_info[i];
3612 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3613 num_free += 1;
3614 }
3615 }
3616 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003617 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003618 }
3619}
3620
Damien George65cad122014-04-06 11:48:15 +01003621mp_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 +01003622 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003623 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003624 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003625 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003626
Damien826005c2013-10-05 23:17:28 +01003627 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003628 mp_map_t consts;
3629 mp_map_init(&consts, 0);
3630 pn = fold_constants(comp, pn, &consts);
3631 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003632
3633 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003634 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003635
Damien826005c2013-10-05 23:17:28 +01003636 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003637 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003638 comp->emit_method_table = &emit_pass1_method_table;
3639 comp->emit_inline_asm = NULL;
3640 comp->emit_inline_asm_method_table = NULL;
3641 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003642 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003643 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003644#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003645 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003646 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003647#endif
Damien826005c2013-10-05 23:17:28 +01003648 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003649 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003650 }
3651
3652 // update maximim number of labels needed
3653 if (comp->next_label > max_num_labels) {
3654 max_num_labels = comp->next_label;
3655 }
Damien429d7192013-10-04 19:53:11 +01003656 }
3657
Damien826005c2013-10-05 23:17:28 +01003658 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003659 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003660 compile_scope_compute_things(comp, s);
3661 }
3662
Damien826005c2013-10-05 23:17:28 +01003663 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003664 emit_pass1_free(comp->emit);
3665
Damien826005c2013-10-05 23:17:28 +01003666 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003667#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003668 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003669#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003670 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003671#endif
Damien3ef4abb2013-10-12 16:53:13 +01003672#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003673 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003674#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003675#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003676 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003677 if (false) {
3678 // dummy
3679
Damien3ef4abb2013-10-12 16:53:13 +01003680#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003681 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003682 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003683 if (emit_inline_thumb == NULL) {
3684 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3685 }
3686 comp->emit = NULL;
3687 comp->emit_method_table = NULL;
3688 comp->emit_inline_asm = emit_inline_thumb;
3689 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003690 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003691 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003692 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003693 }
Damienc025ebb2013-10-12 14:30:21 +01003694#endif
3695
Damien826005c2013-10-05 23:17:28 +01003696 } else {
Damienc025ebb2013-10-12 14:30:21 +01003697
3698 // choose the emit type
3699
Damien3ef4abb2013-10-12 16:53:13 +01003700#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003701 comp->emit = emit_cpython_new(max_num_labels);
3702 comp->emit_method_table = &emit_cpython_method_table;
3703#else
Damien826005c2013-10-05 23:17:28 +01003704 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003705
3706#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003707 case MP_EMIT_OPT_NATIVE_PYTHON:
3708 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003709#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003710 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003711 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003712 }
Damien13ed3a62013-10-08 09:05:10 +01003713 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003714#elif MICROPY_EMIT_X86
3715 if (emit_native == NULL) {
3716 emit_native = emit_native_x86_new(max_num_labels);
3717 }
3718 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003719#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003720 if (emit_native == NULL) {
3721 emit_native = emit_native_thumb_new(max_num_labels);
3722 }
3723 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003724#elif MICROPY_EMIT_ARM
3725 if (emit_native == NULL) {
3726 emit_native = emit_native_arm_new(max_num_labels);
3727 }
3728 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003729#endif
3730 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003731 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien George36db6bc2014-05-07 17:24:22 +01003732
3733 // native emitters need an extra pass to compute stack size
3734 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3735
Damien7af3d192013-10-07 00:02:49 +01003736 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003737#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003738
Damien826005c2013-10-05 23:17:28 +01003739 default:
3740 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003741 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003742 }
3743 comp->emit = emit_bc;
3744 comp->emit_method_table = &emit_bc_method_table;
3745 break;
3746 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003747#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003748
Damien George36db6bc2014-05-07 17:24:22 +01003749 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003750 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003751 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3752 }
3753
3754 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003755 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003756 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003757 }
Damien6cdd3af2013-10-05 18:08:26 +01003758 }
Damien429d7192013-10-04 19:53:11 +01003759 }
3760
Damien George41d02b62014-01-24 22:42:28 +00003761 // free the emitters
3762#if !MICROPY_EMIT_CPYTHON
3763 if (emit_bc != NULL) {
3764 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003765 }
Damien George41d02b62014-01-24 22:42:28 +00003766#if MICROPY_EMIT_NATIVE
3767 if (emit_native != NULL) {
3768#if MICROPY_EMIT_X64
3769 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003770#elif MICROPY_EMIT_X86
3771 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003772#elif MICROPY_EMIT_THUMB
3773 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003774#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003775 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003776#endif
3777 }
3778#endif
3779#if MICROPY_EMIT_INLINE_THUMB
3780 if (emit_inline_thumb != NULL) {
3781 emit_inline_thumb_free(emit_inline_thumb);
3782 }
3783#endif
3784#endif // !MICROPY_EMIT_CPYTHON
3785
Damien George52b5d762014-09-23 15:31:56 +00003786 // free the parse tree
3787 mp_parse_node_free(pn);
3788
Damien George41d02b62014-01-24 22:42:28 +00003789 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003790 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003791 for (scope_t *s = module_scope; s;) {
3792 scope_t *next = s->next;
3793 scope_free(s);
3794 s = next;
3795 }
Damien5ac1b2e2013-10-18 19:58:12 +01003796
Damien George41d02b62014-01-24 22:42:28 +00003797 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003798 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003799 m_del_obj(compiler_t, comp);
3800
Damien Georgea91ac202014-10-05 19:01:34 +01003801 if (compile_error != MP_OBJ_NULL) {
3802 return compile_error;
Damien George1fb03172014-01-03 14:22:03 +00003803 } else {
3804#if MICROPY_EMIT_CPYTHON
3805 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003806 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003807 return mp_const_true;
3808#else
3809 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003810 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003811#endif
3812 }
Damien429d7192013-10-04 19:53:11 +01003813}