blob: b2ba01cc9d9352c99a59b8a6775791ed1c8598ba [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
Damien429d7192013-10-04 19:53:11 +010049} pn_kind_t;
50
Damien Georgeb9791222014-01-23 00:34:21 +000051#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
52#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
53#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
54#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 +010055
Damien Georgea91ac202014-10-05 19:01:34 +010056// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010057typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000058 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010059
Damien George78035b92014-04-09 12:27:39 +010060 uint8_t is_repl;
61 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010062 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010063 uint8_t have_star;
64
65 // try to keep compiler clean from nlr
66 // this is set to an exception object if we have a compile error
67 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010068
Damien George6f355fd2014-04-10 14:11:31 +010069 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010070
Damien George69b89d22014-04-11 13:38:30 +000071 uint16_t num_dict_params;
72 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010073
Damien Georgea91ac202014-10-05 19:01:34 +010074 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
75 uint16_t continue_label;
76 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000077 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010078
Damien429d7192013-10-04 19:53:11 +010079 scope_t *scope_head;
80 scope_t *scope_cur;
81
Damien6cdd3af2013-10-05 18:08:26 +010082 emit_t *emit; // current emitter
83 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010084
85 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
86 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 +010087} compiler_t;
88
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010089STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010090 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
91 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010092 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien Georgea91ac202014-10-05 19:01:34 +010093 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 +010094 } else {
Damien Georgea91ac202014-10-05 19:01:34 +010095 // we don't have a line number, so just pass 0
96 mp_obj_exception_add_traceback(exc, comp->source_file, 0, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010097 }
Damien Georgea91ac202014-10-05 19:01:34 +010098 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +000099}
100
Damien George57e99eb2014-04-10 22:42:11 +0100101STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300102 #if MICROPY_PY_UCTYPES
103 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
104 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100105 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100106 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100107};
108
109STATIC const mp_map_t mp_constants_map = {
110 .all_keys_are_qstrs = 1,
111 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200112 .used = MP_ARRAY_SIZE(mp_constants_table),
113 .alloc = MP_ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +0100114 .table = (mp_map_elem_t*)mp_constants_table,
115};
116
Damien Georgeffae48d2014-05-08 15:58:39 +0000117// this function is essentially a simple preprocessor
118STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
119 if (0) {
120 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100121#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000122 } else if (MP_PARSE_NODE_IS_ID(pn)) {
123 // lookup identifier in table of dynamic constants
124 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
125 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
126 if (elem != NULL) {
127 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
128 }
129#endif
130 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000131 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100132
Damien Georgeffae48d2014-05-08 15:58:39 +0000133 // fold some parse nodes before folding their arguments
134 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100135#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000136 case PN_expr_stmt:
137 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
138 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
139 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
140 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
141 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
142 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
143 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
144 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
145 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
146 ) {
147 // code to assign dynamic constants: id = const(value)
148
149 // get the id
150 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
151
152 // get the value
153 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
154 pn_value = fold_constants(comp, pn_value, consts);
155 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
156 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
157 break;
158 }
Damien George40f3c022014-07-03 13:25:24 +0100159 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000160
161 // store the value in the table of dynamic constants
162 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
163 if (elem->value != MP_OBJ_NULL) {
164 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
165 break;
166 }
167 elem->value = MP_OBJ_NEW_SMALL_INT(value);
168
169 // replace const(value) with value
170 pns1->nodes[0] = pn_value;
171
172 // finished folding this assignment
173 return pn;
174 }
175 }
176 }
177 break;
178#endif
Damien George5042bce2014-05-25 22:06:06 +0100179 case PN_string:
180 return pn;
Damien429d7192013-10-04 19:53:11 +0100181 }
182
Damien Georgeffae48d2014-05-08 15:58:39 +0000183 // fold arguments
184 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
185 for (int i = 0; i < n; i++) {
186 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
187 }
188
189 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000190 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100191 case PN_atom_paren:
192 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
193 // (int)
194 pn = pns->nodes[0];
195 }
196 break;
197
198 case PN_expr:
199 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
200 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100201 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
202 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100203 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
204 }
205 break;
206
207 case PN_and_expr:
208 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
209 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100210 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
211 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100212 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
213 }
214 break;
215
Damien429d7192013-10-04 19:53:11 +0100216 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000217 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 +0100218 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
219 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000220 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100221 // int << int
222 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
223 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
224 }
Damiend99b0522013-12-21 18:17:45 +0000225 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100226 // int >> int
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200227 if (arg1 >= BITS_PER_WORD) {
228 // Shifting to big amounts is underfined behavior
229 // in C and is CPU-dependent; propagate sign bit.
230 arg1 = BITS_PER_WORD - 1;
231 }
Damiend99b0522013-12-21 18:17:45 +0000232 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100233 } else {
234 // shouldn't happen
235 assert(0);
236 }
237 }
238 break;
239
240 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100241 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000242 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 +0100243 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
244 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000245 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100246 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000247 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000248 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100249 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000250 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100251 } else {
252 // shouldn't happen
253 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100254 }
Damien Georged1e355e2014-05-28 14:51:12 +0100255 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100256 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000257 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100258 }
259 }
260 break;
261
262 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000263 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 +0100264 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
265 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000266 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100267 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000268 if (!mp_small_int_mul_overflow(arg0, arg1)) {
269 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100270 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000271 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
272 }
273 }
Damiend99b0522013-12-21 18:17:45 +0000274 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100275 // int / int
276 // pass
Damiend99b0522013-12-21 18:17:45 +0000277 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100278 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000279 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000280 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300281 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100282 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000283 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 +0300284 }
Damien429d7192013-10-04 19:53:11 +0100285 } else {
286 // shouldn't happen
287 assert(0);
288 }
289 }
290 break;
291
292 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000293 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100294 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000295 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
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_MINUS)) {
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);
301 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100302 // ~int
Damiend99b0522013-12-21 18:17:45 +0000303 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100304 } else {
305 // shouldn't happen
306 assert(0);
307 }
308 }
309 break;
310
311 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100312 if (0) {
313#if MICROPY_EMIT_CPYTHON
314 } 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 +0100315 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100316 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000317 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
318 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200319 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100320 if (power >= 0) {
321 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200322 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100323 for (; power > 0; power--) {
324 ans *= base;
325 }
Damiend99b0522013-12-21 18:17:45 +0000326 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100327 }
328 }
Damien George57e99eb2014-04-10 22:42:11 +0100329#endif
330 } 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])) {
331 // id.id
332 // look it up in constant table, see if it can be replaced with an integer
333 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
334 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
335 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
336 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
337 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
338 if (elem != NULL) {
339 mp_obj_t dest[2];
340 mp_load_method_maybe(elem->value, q_attr, dest);
341 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100342 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georged1e355e2014-05-28 14:51:12 +0100343 if (MP_SMALL_INT_FITS(val)) {
Damien George57e99eb2014-04-10 22:42:11 +0100344 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
345 }
346 }
347 }
Damien429d7192013-10-04 19:53:11 +0100348 }
349 break;
350 }
351 }
352
353 return pn;
354}
355
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200356STATIC 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 +0100357STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000358STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100359
Damien George6f355fd2014-04-10 14:11:31 +0100360STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100361 return comp->next_label++;
362}
363
Damien George8dcc0c72014-03-27 10:55:21 +0000364STATIC void compile_increase_except_level(compiler_t *comp) {
365 comp->cur_except_level += 1;
366 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
367 comp->scope_cur->exc_stack_size = comp->cur_except_level;
368 }
369}
370
371STATIC void compile_decrease_except_level(compiler_t *comp) {
372 assert(comp->cur_except_level > 0);
373 comp->cur_except_level -= 1;
374}
375
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200376STATIC 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 +0100377 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100378 scope->parent = comp->scope_cur;
379 scope->next = NULL;
380 if (comp->scope_head == NULL) {
381 comp->scope_head = scope;
382 } else {
383 scope_t *s = comp->scope_head;
384 while (s->next != NULL) {
385 s = s->next;
386 }
387 s->next = scope;
388 }
389 return scope;
390}
391
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200392STATIC 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 +0000393 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
394 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
395 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100396 for (int i = 0; i < num_nodes; i++) {
397 f(comp, pns->nodes[i]);
398 }
Damiend99b0522013-12-21 18:17:45 +0000399 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100400 f(comp, pn);
401 }
402}
403
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200404STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000405 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100406 *nodes = NULL;
407 return 0;
Damiend99b0522013-12-21 18:17:45 +0000408 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100409 *nodes = pn;
410 return 1;
411 } else {
Damiend99b0522013-12-21 18:17:45 +0000412 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
413 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100414 *nodes = pn;
415 return 1;
416 } else {
417 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000418 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100419 }
420 }
421}
422
Damien George969a6b32014-12-10 22:07:04 +0000423STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000424 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100425 for (int i = 0; i < num_nodes; i++) {
426 compile_node(comp, pns->nodes[i]);
427 }
428}
429
Damien3ef4abb2013-10-12 16:53:13 +0100430#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200431STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100432 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
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 George5042bce2014-05-25 22:06:06 +0100483 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
484 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George40f3c022014-07-03 13:25:24 +0100485 cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false);
Damien George5042bce2014-05-25 22:06:06 +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");
Damien429d7192013-10-04 19:53:11 +01001990 return;
1991 }
1992 } else {
1993 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001994 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1995 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1996 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1997 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001998 // handler binds the exception to a local
1999 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002000 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002001 }
2002 }
2003 EMIT(dup_top);
2004 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002005 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00002006 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002007 }
2008
2009 EMIT(pop_top);
2010
2011 if (qstr_exception_local == 0) {
2012 EMIT(pop_top);
2013 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002014 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002015 }
2016
2017 EMIT(pop_top);
2018
Damien George6f355fd2014-04-10 14:11:31 +01002019 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002020 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002021 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002022 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002023 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002024 }
2025 compile_node(comp, pns_except->nodes[1]);
2026 if (qstr_exception_local != 0) {
2027 EMIT(pop_block);
2028 }
2029 EMIT(pop_except);
2030 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002031 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2032 EMIT_ARG(label_assign, l3);
2033 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2034 EMIT_ARG(store_id, qstr_exception_local);
2035 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002036
Damien George8dcc0c72014-03-27 10:55:21 +00002037 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002038 EMIT(end_finally);
2039 }
Damien Georgeb9791222014-01-23 00:34:21 +00002040 EMIT_ARG(jump, l2);
2041 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002042 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002043 }
2044
Damien George8dcc0c72014-03-27 10:55:21 +00002045 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002046 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002047 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002048
Damien Georgeb9791222014-01-23 00:34:21 +00002049 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002050 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002051 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002052}
2053
Damien George6be0b0a2014-08-15 14:30:52 +01002054STATIC 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 +01002055 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002056
Damien Georgeb9791222014-01-23 00:34:21 +00002057 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002058 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002059
Damien429d7192013-10-04 19:53:11 +01002060 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002061 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002062 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002063 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002064 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002065 } else {
2066 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2067 }
2068 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002069 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2070 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002071 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002072
Damien George8dcc0c72014-03-27 10:55:21 +00002073 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002074 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002075}
2076
Damien George969a6b32014-12-10 22:07:04 +00002077STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002078 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2079 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2080 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002081 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002082 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2083 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002084 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002085 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002086 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002087 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002088 // no finally
2089 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2090 } else {
2091 // have finally
Damiend99b0522013-12-21 18:17:45 +00002092 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 +01002093 }
2094 } else {
2095 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002096 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002097 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002098 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002099 }
2100 } else {
2101 // shouldn't happen
2102 assert(0);
2103 }
2104}
2105
Damien George6be0b0a2014-08-15 14:30:52 +01002106STATIC 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 +01002107 if (n == 0) {
2108 // no more pre-bits, compile the body of the with
2109 compile_node(comp, body);
2110 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002111 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002112 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002113 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002114 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002115 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002116 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002117 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2118 } else {
2119 // this pre-bit is just an expression
2120 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002121 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002122 EMIT(pop_top);
2123 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002124 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002125 // compile additional pre-bits and the body
2126 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2127 // finish this with block
2128 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002129 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2130 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002131 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002132 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002133 EMIT(end_finally);
2134 }
2135}
2136
Damien George969a6b32014-12-10 22:07:04 +00002137STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002138 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002139 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002140 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2141 assert(n > 0);
2142
2143 // compile in a nested fashion
2144 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2145}
2146
Damien George969a6b32014-12-10 22:07:04 +00002147STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002148 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002149 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2150 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002151 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002152 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002153 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002154 EMIT(pop_top);
2155
Damien429d7192013-10-04 19:53:11 +01002156 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002157 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002158 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2159 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002160 // do nothing with a lonely constant
2161 } else {
2162 compile_node(comp, pns->nodes[0]); // just an expression
2163 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2164 }
Damien429d7192013-10-04 19:53:11 +01002165 }
2166 } else {
Damiend99b0522013-12-21 18:17:45 +00002167 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2168 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002169 if (kind == PN_expr_stmt_augassign) {
2170 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2171 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002172 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002173 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002174 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002175 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2176 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2177 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2178 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2179 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2180 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2181 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2182 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2183 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2184 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2185 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2186 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2187 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002188 }
Damien George7e5fb242014-02-01 22:18:47 +00002189 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002190 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2191 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002192 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2193 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002194 // following CPython, we store left-most first
2195 if (rhs > 0) {
2196 EMIT(dup_top);
2197 }
2198 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2199 for (int i = 0; i < rhs; i++) {
2200 if (i + 1 < rhs) {
2201 EMIT(dup_top);
2202 }
Damiend99b0522013-12-21 18:17:45 +00002203 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002204 }
2205 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002206 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002207 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2208 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2209 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002210 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002211 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2212 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002213 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2214 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2215 // can't optimise when it's a star expression on the lhs
2216 goto no_optimisation;
2217 }
Damien429d7192013-10-04 19:53:11 +01002218 compile_node(comp, pns10->nodes[0]); // rhs
2219 compile_node(comp, pns10->nodes[1]); // rhs
2220 EMIT(rot_two);
2221 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2222 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002223 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2224 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2225 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2226 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002227 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002228 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2229 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002230 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2231 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2232 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2233 // can't optimise when it's a star expression on the lhs
2234 goto no_optimisation;
2235 }
Damien429d7192013-10-04 19:53:11 +01002236 compile_node(comp, pns10->nodes[0]); // rhs
2237 compile_node(comp, pns10->nodes[1]); // rhs
2238 compile_node(comp, pns10->nodes[2]); // rhs
2239 EMIT(rot_three);
2240 EMIT(rot_two);
2241 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2242 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2243 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2244 } else {
Damien George495d7812014-04-08 17:51:47 +01002245 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002246 compile_node(comp, pns1->nodes[0]); // rhs
2247 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2248 }
2249 } else {
2250 // shouldn't happen
2251 assert(0);
2252 }
2253 }
2254}
2255
Damien George6be0b0a2014-08-15 14:30:52 +01002256STATIC 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 +00002257 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002258 compile_node(comp, pns->nodes[0]);
2259 for (int i = 1; i < num_nodes; i += 1) {
2260 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002261 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002262 }
2263}
2264
Damien George969a6b32014-12-10 22:07:04 +00002265STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002266 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2267 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002268
Damien George6f355fd2014-04-10 14:11:31 +01002269 uint l_fail = comp_next_label(comp);
2270 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002271 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2272 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002273 EMIT_ARG(jump, l_end);
2274 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002275 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002276 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002277 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002278}
2279
Damien George969a6b32014-12-10 22:07:04 +00002280STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002281 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002282 //mp_parse_node_t pn_params = pns->nodes[0];
2283 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002284
Damien George36db6bc2014-05-07 17:24:22 +01002285 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002286 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002287 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 +01002288 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002289 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002290 }
2291
2292 // get the scope for this lambda
2293 scope_t *this_scope = (scope_t*)pns->nodes[2];
2294
2295 // make the lambda
2296 close_over_variables_etc(comp, this_scope, 0, 0);
2297}
2298
Damien George969a6b32014-12-10 22:07:04 +00002299STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002300 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002301 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002302 for (int i = 0; i < n; i += 1) {
2303 compile_node(comp, pns->nodes[i]);
2304 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002305 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002306 }
2307 }
Damien Georgeb9791222014-01-23 00:34:21 +00002308 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002309}
2310
Damien George969a6b32014-12-10 22:07:04 +00002311STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002312 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002313 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002314 for (int i = 0; i < n; i += 1) {
2315 compile_node(comp, pns->nodes[i]);
2316 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002317 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002318 }
2319 }
Damien Georgeb9791222014-01-23 00:34:21 +00002320 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002321}
2322
Damien George969a6b32014-12-10 22:07:04 +00002323STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002324 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002325 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002326}
2327
Damien George969a6b32014-12-10 22:07:04 +00002328STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002329 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002330 compile_node(comp, pns->nodes[0]);
2331 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002332 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002333 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002334 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002335 }
2336 for (int i = 1; i + 1 < num_nodes; i += 2) {
2337 compile_node(comp, pns->nodes[i + 1]);
2338 if (i + 2 < num_nodes) {
2339 EMIT(dup_top);
2340 EMIT(rot_three);
2341 }
Damien George7e5fb242014-02-01 22:18:47 +00002342 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002343 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002344 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002345 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2346 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2347 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2348 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2349 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2350 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2351 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2352 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002353 }
2354 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002355 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2356 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2357 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002358 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002359 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002360 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002361 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002362 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002363 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002364 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002365 }
2366 } else {
2367 // shouldn't happen
2368 assert(0);
2369 }
2370 } else {
2371 // shouldn't happen
2372 assert(0);
2373 }
2374 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002375 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002376 }
2377 }
2378 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002379 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002380 EMIT_ARG(jump, l_end);
2381 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002382 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002383 EMIT(rot_two);
2384 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002385 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002386 }
2387}
2388
Damien George969a6b32014-12-10 22:07:04 +00002389STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002390 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002391}
2392
Damien George969a6b32014-12-10 22:07:04 +00002393STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002394 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002395}
2396
Damien George969a6b32014-12-10 22:07:04 +00002397STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002398 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002399}
2400
Damien George969a6b32014-12-10 22:07:04 +00002401STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002402 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002403}
2404
Damien George969a6b32014-12-10 22:07:04 +00002405STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002406 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002407 compile_node(comp, pns->nodes[0]);
2408 for (int i = 1; i + 1 < num_nodes; i += 2) {
2409 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002410 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002411 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002412 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002413 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002414 } else {
2415 // shouldn't happen
2416 assert(0);
2417 }
2418 }
2419}
2420
Damien George969a6b32014-12-10 22:07:04 +00002421STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002422 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002423 compile_node(comp, pns->nodes[0]);
2424 for (int i = 1; i + 1 < num_nodes; i += 2) {
2425 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002426 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002427 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002428 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002429 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002430 } else {
2431 // shouldn't happen
2432 assert(0);
2433 }
2434 }
2435}
2436
Damien George969a6b32014-12-10 22:07:04 +00002437STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002438 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002439 compile_node(comp, pns->nodes[0]);
2440 for (int i = 1; i + 1 < num_nodes; i += 2) {
2441 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002442 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002443 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002444 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002445 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002446 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002447 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002448 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002449 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002450 } else {
2451 // shouldn't happen
2452 assert(0);
2453 }
2454 }
2455}
2456
Damien George969a6b32014-12-10 22:07:04 +00002457STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002458 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002459 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002460 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002461 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002462 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002463 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002464 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002465 } else {
2466 // shouldn't happen
2467 assert(0);
2468 }
2469}
2470
Damien George969a6b32014-12-10 22:07:04 +00002471STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002472 // this is to handle special super() call
2473 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2474
2475 compile_generic_all_nodes(comp, pns);
2476}
2477
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002478STATIC 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 +01002479 // function to call is on top of stack
2480
Damien George35e2a4e2014-02-05 00:51:47 +00002481#if !MICROPY_EMIT_CPYTHON
2482 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002483 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 +00002484 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002485 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002486 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002487 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002488 // first argument found; load it and call super
Damien George2bf7c092014-04-09 15:26:46 +01002489 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 +00002490 EMIT_ARG(call_function, 2, 0, 0);
2491 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002492 }
2493 }
Damien George01039b52014-12-21 17:44:27 +00002494 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002495 return;
2496 }
2497#endif
2498
Damien George2c0842b2014-04-27 16:46:51 +01002499 // get the list of arguments
2500 mp_parse_node_t *args;
2501 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002502
Damien George2c0842b2014-04-27 16:46:51 +01002503 // compile the arguments
2504 // Rather than calling compile_node on the list, we go through the list of args
2505 // explicitly here so that we can count the number of arguments and give sensible
2506 // error messages.
2507 int n_positional = n_positional_extra;
2508 uint n_keyword = 0;
2509 uint star_flags = 0;
2510 for (int i = 0; i < n_args; i++) {
2511 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2512 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2513 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2514 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2515 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2516 return;
2517 }
2518 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2519 compile_node(comp, pns_arg->nodes[0]);
2520 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2521 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2522 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2523 return;
2524 }
2525 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2526 compile_node(comp, pns_arg->nodes[0]);
2527 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2528 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2529 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2530 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2531 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2532 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2533 return;
2534 }
Damien George968bf342014-04-27 19:12:05 +01002535 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002536 compile_node(comp, pns2->nodes[0]);
2537 n_keyword += 1;
2538 } else {
2539 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2540 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2541 n_positional++;
2542 }
2543 } else {
2544 goto normal_argument;
2545 }
2546 } else {
2547 normal_argument:
2548 if (n_keyword > 0) {
2549 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2550 return;
2551 }
2552 compile_node(comp, args[i]);
2553 n_positional++;
2554 }
Damien429d7192013-10-04 19:53:11 +01002555 }
2556
Damien George2c0842b2014-04-27 16:46:51 +01002557 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002558 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002559 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002560 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002561 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002562 }
Damien429d7192013-10-04 19:53:11 +01002563}
2564
Damien George969a6b32014-12-10 22:07:04 +00002565STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002566 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002567 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002568 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 +01002569 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002570 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2571 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002572 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002573 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002574 i += 1;
2575 } else {
2576 compile_node(comp, pns->nodes[i]);
2577 }
Damien George35e2a4e2014-02-05 00:51:47 +00002578 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002579 }
2580}
2581
Damien George969a6b32014-12-10 22:07:04 +00002582STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002583 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002584 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002585}
2586
Damien George969a6b32014-12-10 22:07:04 +00002587STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002588 // a list of strings
Damien63321742013-12-10 17:41:49 +00002589
2590 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002591 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002592 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002593 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002594 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002595 int pn_kind;
2596 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2597 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2598 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2599 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2600 } else {
2601 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2602 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2603 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2604 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002605 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002606 }
Damien63321742013-12-10 17:41:49 +00002607 if (i == 0) {
2608 string_kind = pn_kind;
2609 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002610 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002611 return;
2612 }
Damien429d7192013-10-04 19:53:11 +01002613 }
Damien63321742013-12-10 17:41:49 +00002614
Damien63321742013-12-10 17:41:49 +00002615 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002616 byte *q_ptr;
2617 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002618 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002619 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002620 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002621 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2622 memcpy(s_dest, s, s_len);
2623 s_dest += s_len;
2624 } else {
2625 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002626 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2627 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002628 }
Damien63321742013-12-10 17:41:49 +00002629 }
Damien George55baff42014-01-21 21:40:13 +00002630 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002631
Damien Georgeb9791222014-01-23 00:34:21 +00002632 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002633}
2634
2635// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002636STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002637 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2638 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2639 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002640
Damien George36db6bc2014-05-07 17:24:22 +01002641 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002642 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002643 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 +01002644 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002645 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002646 }
2647
2648 // get the scope for this comprehension
2649 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2650
2651 // compile the comprehension
2652 close_over_variables_etc(comp, this_scope, 0, 0);
2653
2654 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2655 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002656 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002657}
2658
Damien George969a6b32014-12-10 22:07:04 +00002659STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002660 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002661 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002662 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2663 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2664 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2665 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2666 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2667 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2668 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002669 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002670 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002671 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002672 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002673 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002674 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002675 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002676 // generator expression
2677 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2678 } else {
2679 // tuple with 2 items
2680 goto tuple_with_2_items;
2681 }
2682 } else {
2683 // tuple with 2 items
2684 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002685 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002686 }
2687 } else {
2688 // parenthesis around a single item, is just that item
2689 compile_node(comp, pns->nodes[0]);
2690 }
2691}
2692
Damien George969a6b32014-12-10 22:07:04 +00002693STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002694 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002695 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002696 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002697 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2698 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2699 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2700 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2701 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002702 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002703 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002704 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002705 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002706 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002707 // list of many items
2708 compile_node(comp, pns2->nodes[0]);
2709 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002710 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002711 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002712 // list comprehension
2713 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2714 } else {
2715 // list with 2 items
2716 goto list_with_2_items;
2717 }
2718 } else {
2719 // list with 2 items
2720 list_with_2_items:
2721 compile_node(comp, pns2->nodes[0]);
2722 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002723 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002724 }
2725 } else {
2726 // list with 1 item
2727 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002728 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002729 }
2730}
2731
Damien George969a6b32014-12-10 22:07:04 +00002732STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002733 mp_parse_node_t pn = pns->nodes[0];
2734 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002735 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002736 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002737 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2738 pns = (mp_parse_node_struct_t*)pn;
2739 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002740 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002741 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002742 compile_node(comp, pn);
2743 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002744 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2745 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2746 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2747 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002748 // dict/set with multiple elements
2749
2750 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002751 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002752 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2753
2754 // first element sets whether it's a dict or set
2755 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002756 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002757 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002758 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002759 compile_node(comp, pns->nodes[0]);
2760 EMIT(store_map);
2761 is_dict = true;
2762 } else {
2763 // a set
2764 compile_node(comp, pns->nodes[0]); // 1st value of set
2765 is_dict = false;
2766 }
2767
2768 // process rest of elements
2769 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002770 mp_parse_node_t pn = nodes[i];
2771 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002772 compile_node(comp, pn);
2773 if (is_dict) {
2774 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002775 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002776 return;
2777 }
2778 EMIT(store_map);
2779 } else {
2780 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002781 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002782 return;
2783 }
2784 }
2785 }
2786
Damien Georgee37dcaa2014-12-27 17:07:16 +00002787 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002788 // if it's a set, build it
2789 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002790 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002791 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002792 #endif
Damiend99b0522013-12-21 18:17:45 +00002793 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002794 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002795 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002796 // a dictionary comprehension
2797 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2798 } else {
2799 // a set comprehension
2800 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2801 }
2802 } else {
2803 // shouldn't happen
2804 assert(0);
2805 }
2806 } else {
2807 // set with one element
2808 goto set_with_one_element;
2809 }
2810 } else {
2811 // set with one element
2812 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002813 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002814 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002815 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002816 #else
2817 assert(0);
2818 #endif
Damien429d7192013-10-04 19:53:11 +01002819 }
2820}
2821
Damien George969a6b32014-12-10 22:07:04 +00002822STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002823 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002824}
2825
Damien George969a6b32014-12-10 22:07:04 +00002826STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002827 // object who's index we want is on top of stack
2828 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002829 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002830}
2831
Damien George969a6b32014-12-10 22:07:04 +00002832STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002833 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002834 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002835}
2836
Damien George83204f32014-12-27 17:20:41 +00002837#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002838STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002839 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2840 mp_parse_node_t pn = pns->nodes[0];
2841 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002842 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002843 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2844 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002845 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2846 pns = (mp_parse_node_struct_t*)pn;
2847 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002848 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002849 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002850 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002851 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002852 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002853 } else {
2854 // [?::x]
2855 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002857 }
Damiend99b0522013-12-21 18:17:45 +00002858 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002859 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002860 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2861 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2862 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2863 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002864 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002865 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002866 } else {
2867 // [?:x:x]
2868 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002869 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002870 }
2871 } else {
2872 // [?:x]
2873 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002874 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002875 }
2876 } else {
2877 // [?:x]
2878 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002879 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002880 }
2881}
2882
Damien George969a6b32014-12-10 22:07:04 +00002883STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002884 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002885 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2886 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002887}
2888
Damien George969a6b32014-12-10 22:07:04 +00002889STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002890 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002891 compile_subscript_3_helper(comp, pns);
2892}
Damien George83204f32014-12-27 17:20:41 +00002893#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002894
Damien George969a6b32014-12-10 22:07:04 +00002895STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002896 // if this is called then we are compiling a dict key:value pair
2897 compile_node(comp, pns->nodes[1]); // value
2898 compile_node(comp, pns->nodes[0]); // key
2899}
2900
Damien George969a6b32014-12-10 22:07:04 +00002901STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002902 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002903 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002904 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002905}
2906
Damien George969a6b32014-12-10 22:07:04 +00002907STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002908 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002909 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002910 return;
2911 }
Damiend99b0522013-12-21 18:17:45 +00002912 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002913 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002914 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002915 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2916 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002917 compile_node(comp, pns->nodes[0]);
2918 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002919 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002920 EMIT(yield_from);
2921 } else {
2922 compile_node(comp, pns->nodes[0]);
2923 EMIT(yield_value);
2924 }
2925}
2926
Damiend99b0522013-12-21 18:17:45 +00002927typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002928STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002929#define nc NULL
2930#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002931#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002932#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002933#undef nc
2934#undef c
2935#undef DEF_RULE
2936};
2937
Damien George6be0b0a2014-08-15 14:30:52 +01002938STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002939 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002940 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002941 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002942 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002943 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002944 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002945 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002946 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002947 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002948 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2949 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2950 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2951 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002952 case MP_PARSE_NODE_TOKEN:
2953 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002954 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002955 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002956 // do nothing
2957 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002958 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002959 }
2960 break;
Damien429d7192013-10-04 19:53:11 +01002961 default: assert(0);
2962 }
2963 } else {
Damiend99b0522013-12-21 18:17:45 +00002964 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002965 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002966 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002967 EMIT_ARG(load_const_str, qstr_from_strn((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]), false);
Damien429d7192013-10-04 19:53:11 +01002968 } else {
Damien George5042bce2014-05-25 22:06:06 +01002969 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2970 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002971#if MICROPY_DEBUG_PRINTERS
Damien George01039b52014-12-21 17:44:27 +00002972 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien George5042bce2014-05-25 22:06:06 +01002973 mp_parse_node_print(pn, 0);
2974#endif
2975 compile_syntax_error(comp, pn, "internal compiler error");
2976 } else {
2977 f(comp, pns);
2978 }
Damien429d7192013-10-04 19:53:11 +01002979 }
2980 }
2981}
2982
Damien George2ac4af62014-08-15 16:45:41 +01002983STATIC 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 +01002984 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002985 qstr param_name = MP_QSTR_NULL;
2986 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002987 if (MP_PARSE_NODE_IS_ID(pn)) {
2988 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002989 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002990 // comes after a star, so counts as a keyword-only parameter
2991 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002992 } else {
Damien George2827d622014-04-27 15:50:52 +01002993 // comes before a star, so counts as a positional parameter
2994 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002995 }
Damienb14de212013-10-06 00:28:28 +01002996 } else {
Damiend99b0522013-12-21 18:17:45 +00002997 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2998 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2999 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3000 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003001 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003002 // comes after a star, so counts as a keyword-only parameter
3003 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003004 } else {
Damien George2827d622014-04-27 15:50:52 +01003005 // comes before a star, so counts as a positional parameter
3006 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003007 }
Damiend99b0522013-12-21 18:17:45 +00003008 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003009 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003010 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003011 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003012 // bare star
3013 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003014 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003015 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003016 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003017 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003018 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01003019 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01003020 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003021 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003022 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3023 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003024 } else {
3025 // shouldn't happen
3026 assert(0);
3027 }
Damiend99b0522013-12-21 18:17:45 +00003028 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
3029 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003030 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003031 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003032 } else {
Damienb14de212013-10-06 00:28:28 +01003033 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01003034 assert(0);
3035 }
Damien429d7192013-10-04 19:53:11 +01003036 }
3037
Damien George2827d622014-04-27 15:50:52 +01003038 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003039 bool added;
3040 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3041 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003042 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003043 return;
3044 }
Damien429d7192013-10-04 19:53:11 +01003045 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003046 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003047 }
3048}
3049
Damien George2827d622014-04-27 15:50:52 +01003050STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003051 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003052}
3053
Damien George2827d622014-04-27 15:50:52 +01003054STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003055 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3056}
3057
3058STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3059 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3060 // no annotation
3061 return;
3062 }
3063
3064 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3065 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3066 // named parameter with possible annotation
3067 // fallthrough
3068 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3069 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3070 // named star with possible annotation
3071 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3072 // fallthrough
3073 } else {
3074 // no annotation
3075 return;
3076 }
3077 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3078 // double star with possible annotation
3079 // fallthrough
3080 } else {
3081 // no annotation
3082 return;
3083 }
3084
3085 mp_parse_node_t pn_annotation = pns->nodes[1];
3086
3087 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3088 #if MICROPY_EMIT_NATIVE
3089 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3090 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3091 assert(id_info != NULL);
3092
3093 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3094 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3095 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3096 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3097 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003098 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003099 }
3100 }
3101 #endif // MICROPY_EMIT_NATIVE
3102 }
Damien429d7192013-10-04 19:53:11 +01003103}
3104
Damien George6be0b0a2014-08-15 14:30:52 +01003105STATIC 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 +01003106 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003107 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003108 // no more nested if/for; compile inner expression
3109 compile_node(comp, pn_inner_expr);
3110 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003111 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003112 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003113 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003114 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003115 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003116 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003117 #endif
Damien429d7192013-10-04 19:53:11 +01003118 } else {
3119 EMIT(yield_value);
3120 EMIT(pop_top);
3121 }
Damiend99b0522013-12-21 18:17:45 +00003122 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003123 // if condition
Damiend99b0522013-12-21 18:17:45 +00003124 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003125 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3126 pn_iter = pns_comp_if->nodes[1];
3127 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003128 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003129 // for loop
Damiend99b0522013-12-21 18:17:45 +00003130 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003131 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003132 uint l_end2 = comp_next_label(comp);
3133 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003134 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003135 EMIT_ARG(label_assign, l_top2);
3136 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003137 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3138 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 +00003139 EMIT_ARG(jump, l_top2);
3140 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003141 EMIT(for_iter_end);
3142 } else {
3143 // shouldn't happen
3144 assert(0);
3145 }
3146}
3147
Damien George1463c1f2014-04-25 23:52:57 +01003148STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3149#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003150 // see http://www.python.org/dev/peps/pep-0257/
3151
3152 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003153 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003154 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003155 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003156 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003157 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3158 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003159 for (int i = 0; i < num_nodes; i++) {
3160 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003161 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 +00003162 // not a newline, so this is the first statement; finish search
3163 break;
3164 }
3165 }
3166 // 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 +00003167 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003168 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003169 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003170 } else {
3171 return;
3172 }
3173
3174 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003175 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3176 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003177 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3178 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3179 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3180 // compile the doc string
3181 compile_node(comp, pns->nodes[0]);
3182 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003183 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003184 }
3185 }
Damien George1463c1f2014-04-25 23:52:57 +01003186#endif
Damien429d7192013-10-04 19:53:11 +01003187}
3188
Damien George1463c1f2014-04-25 23:52:57 +01003189STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003190 comp->pass = pass;
3191 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003192 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003193 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003194
Damien George36db6bc2014-05-07 17:24:22 +01003195 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003196 // reset maximum stack sizes in scope
3197 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003198 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003199 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003200 }
3201
Damien5ac1b2e2013-10-18 19:58:12 +01003202#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003203 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003204 scope_print_info(scope);
3205 }
Damien5ac1b2e2013-10-18 19:58:12 +01003206#endif
Damien429d7192013-10-04 19:53:11 +01003207
3208 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003209 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3210 assert(scope->kind == SCOPE_MODULE);
3211 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3212 compile_node(comp, pns->nodes[0]); // compile the expression
3213 EMIT(return_value);
3214 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003215 if (!comp->is_repl) {
3216 check_for_doc_string(comp, scope->pn);
3217 }
Damien429d7192013-10-04 19:53:11 +01003218 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003219 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003220 EMIT(return_value);
3221 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003222 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3223 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3224 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003225
3226 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003227 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003228 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003229 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003230 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003231 } else {
3232 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003233
Damien George2ac4af62014-08-15 16:45:41 +01003234 // argument annotations
3235 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3236
3237 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003238 mp_parse_node_t pn_annotation = pns->nodes[2];
3239 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3240 #if MICROPY_EMIT_NATIVE
3241 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3242 // nodes[2] can be null or a test-expr
3243 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3244 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3245 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3246 } else {
3247 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3248 }
Damien George2ac4af62014-08-15 16:45:41 +01003249 }
Damien Georgea5190a72014-08-15 22:39:08 +01003250 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003251 }
Damien George2ac4af62014-08-15 16:45:41 +01003252 }
Damien429d7192013-10-04 19:53:11 +01003253
3254 compile_node(comp, pns->nodes[3]); // 3 is function body
3255 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003256 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003257 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003258 EMIT(return_value);
3259 }
3260 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003261 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3262 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3263 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003264
3265 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003266 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003267 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003268 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003269 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3270 }
3271
3272 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003273
3274 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3275 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3276 EMIT(pop_top);
3277 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3278 }
Damien429d7192013-10-04 19:53:11 +01003279 EMIT(return_value);
3280 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3281 // a bit of a hack at the moment
3282
Damiend99b0522013-12-21 18:17:45 +00003283 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3284 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3285 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3286 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3287 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003288
Damien George708c0732014-04-27 19:23:46 +01003289 // We need a unique name for the comprehension argument (the iterator).
3290 // CPython uses .0, but we should be able to use anything that won't
3291 // clash with a user defined variable. Best to use an existing qstr,
3292 // so we use the blank qstr.
3293#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003294 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003295#else
3296 qstr qstr_arg = MP_QSTR_;
3297#endif
Damien George36db6bc2014-05-07 17:24:22 +01003298 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003299 bool added;
3300 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3301 assert(added);
3302 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003303 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003304 }
3305
3306 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003307 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003308 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003309 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003310 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003311 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003312 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003313 #endif
Damien429d7192013-10-04 19:53:11 +01003314 }
3315
Damien George6f355fd2014-04-10 14:11:31 +01003316 uint l_end = comp_next_label(comp);
3317 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003318 EMIT_ARG(load_id, qstr_arg);
3319 EMIT_ARG(label_assign, l_top);
3320 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003321 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3322 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003323 EMIT_ARG(jump, l_top);
3324 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003325 EMIT(for_iter_end);
3326
3327 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003328 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003329 }
3330 EMIT(return_value);
3331 } else {
3332 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003333 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3334 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3335 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003336
Damien George36db6bc2014-05-07 17:24:22 +01003337 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003338 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003339 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003340 assert(added);
3341 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003342 }
3343
Damien Georgeb9791222014-01-23 00:34:21 +00003344 EMIT_ARG(load_id, MP_QSTR___name__);
3345 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003346 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 +00003347 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003348
3349 check_for_doc_string(comp, pns->nodes[2]);
3350 compile_node(comp, pns->nodes[2]); // 2 is class body
3351
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003352 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003353 assert(id != NULL);
3354 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003355 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003356 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003357#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003358 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003359#else
Damien George2bf7c092014-04-09 15:26:46 +01003360 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003361#endif
Damien429d7192013-10-04 19:53:11 +01003362 }
3363 EMIT(return_value);
3364 }
3365
Damien415eb6f2013-10-05 12:19:06 +01003366 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003367
3368 // make sure we match all the exception levels
3369 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003370}
3371
Damien George094d4502014-04-02 17:31:27 +01003372#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003373// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003374STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003375 comp->pass = pass;
3376 comp->scope_cur = scope;
3377 comp->next_label = 1;
3378
3379 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003380 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003381 return;
3382 }
3383
Damien George36db6bc2014-05-07 17:24:22 +01003384 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003385 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003386 }
3387
Damien826005c2013-10-05 23:17:28 +01003388 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003389 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3390 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3391 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003392
Damiend99b0522013-12-21 18:17:45 +00003393 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003394
Damiena2f2f7d2013-10-06 00:14:13 +01003395 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003396 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003397 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003398 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003399 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003400 }
3401
Damiend99b0522013-12-21 18:17:45 +00003402 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003403
Damiend99b0522013-12-21 18:17:45 +00003404 mp_parse_node_t pn_body = pns->nodes[3]; // body
3405 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003406 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3407
Damien Georgecbd2f742014-01-19 11:48:48 +00003408 /*
Damien George36db6bc2014-05-07 17:24:22 +01003409 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003410 //printf("----\n");
3411 scope_print_info(scope);
3412 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003413 */
Damien826005c2013-10-05 23:17:28 +01003414
3415 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003416 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3417 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003418 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3419 // no instructions
3420 continue;
3421 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3422 // an instruction; fall through
3423 } else {
3424 // not an instruction; error
3425 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3426 return;
3427 }
Damiend99b0522013-12-21 18:17:45 +00003428 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3429 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3430 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3431 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3432 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3433 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3434 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3435 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3436 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3437 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003438 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3439
3440 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003441 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003442 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003443 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003444 return;
3445 }
Damien George6f355fd2014-04-10 14:11:31 +01003446 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003447 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003448 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003449 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003450 } else if (op == MP_QSTR_align) {
3451 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3452 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3453 return;
3454 }
Damien George36db6bc2014-05-07 17:24:22 +01003455 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003456 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3457 }
3458 } else if (op == MP_QSTR_data) {
3459 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3460 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3461 return;
3462 }
Damien George36db6bc2014-05-07 17:24:22 +01003463 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003464 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003465 for (uint i = 1; i < n_args; i++) {
3466 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3467 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3468 return;
3469 }
3470 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3471 }
3472 }
Damien826005c2013-10-05 23:17:28 +01003473 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003474 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003475 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003476 }
3477 }
3478 }
3479
Damien George36db6bc2014-05-07 17:24:22 +01003480 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003481 bool success = EMIT_INLINE_ASM(end_pass);
3482 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003483 // TODO get proper exception from inline assembler
3484 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003485 }
Damienb05d7072013-10-05 13:37:10 +01003486 }
Damien429d7192013-10-04 19:53:11 +01003487}
Damien George094d4502014-04-02 17:31:27 +01003488#endif
Damien429d7192013-10-04 19:53:11 +01003489
Damien George1463c1f2014-04-25 23:52:57 +01003490STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003491#if !MICROPY_EMIT_CPYTHON
3492 // in Micro Python we put the *x parameter after all other parameters (except **y)
3493 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3494 id_info_t *id_param = NULL;
3495 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3496 id_info_t *id = &scope->id_info[i];
3497 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3498 if (id_param != NULL) {
3499 // swap star param with last param
3500 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3501 }
3502 break;
3503 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3504 id_param = id;
3505 }
3506 }
3507 }
3508#endif
3509
Damien429d7192013-10-04 19:53:11 +01003510 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003511 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003512 scope->num_locals = 0;
3513 for (int i = 0; i < scope->id_info_len; i++) {
3514 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003515 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003516 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3517 continue;
3518 }
3519 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3520 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3521 }
Damien George2827d622014-04-27 15:50:52 +01003522 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003523 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003524 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003525 }
3526 }
3527
3528 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003529#if MICROPY_EMIT_CPYTHON
3530 int num_cell = 0;
3531#endif
Damien9ecbcff2013-12-11 00:41:43 +00003532 for (int i = 0; i < scope->id_info_len; i++) {
3533 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003534#if MICROPY_EMIT_CPYTHON
3535 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003536 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003537 id->local_num = num_cell;
3538 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003539 }
Damien George6baf76e2013-12-30 22:32:17 +00003540#else
3541 // in Micro Python the cells come right after the fast locals
3542 // parameters are not counted here, since they remain at the start
3543 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003544 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003545 id->local_num = scope->num_locals;
3546 scope->num_locals += 1;
3547 }
3548#endif
Damien9ecbcff2013-12-11 00:41:43 +00003549 }
Damien9ecbcff2013-12-11 00:41:43 +00003550
3551 // compute the index of free vars (freevars[idx] in CPython)
3552 // make sure they are in the order of the parent scope
3553 if (scope->parent != NULL) {
3554 int num_free = 0;
3555 for (int i = 0; i < scope->parent->id_info_len; i++) {
3556 id_info_t *id = &scope->parent->id_info[i];
3557 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3558 for (int j = 0; j < scope->id_info_len; j++) {
3559 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003560 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003561 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003562#if MICROPY_EMIT_CPYTHON
3563 // in CPython the frees are numbered after the cells
3564 id2->local_num = num_cell + num_free;
3565#else
3566 // in Micro Python the frees come first, before the params
3567 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003568#endif
3569 num_free += 1;
3570 }
3571 }
3572 }
Damien429d7192013-10-04 19:53:11 +01003573 }
Damien George6baf76e2013-12-30 22:32:17 +00003574#if !MICROPY_EMIT_CPYTHON
3575 // in Micro Python shift all other locals after the free locals
3576 if (num_free > 0) {
3577 for (int i = 0; i < scope->id_info_len; i++) {
3578 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003579 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003580 id->local_num += num_free;
3581 }
3582 }
Damien George2827d622014-04-27 15:50:52 +01003583 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 +00003584 scope->num_locals += num_free;
3585 }
3586#endif
Damien429d7192013-10-04 19:53:11 +01003587 }
3588
Damien George8725f8f2014-02-15 19:33:11 +00003589 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003590
3591#if MICROPY_EMIT_CPYTHON
3592 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003593 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) {
3594 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003595 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003596 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003597 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 +00003598 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003599 }
3600 }
Damien George882b3632014-04-02 15:56:31 +01003601#endif
3602
Damien429d7192013-10-04 19:53:11 +01003603 int num_free = 0;
3604 for (int i = 0; i < scope->id_info_len; i++) {
3605 id_info_t *id = &scope->id_info[i];
3606 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3607 num_free += 1;
3608 }
3609 }
3610 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003611 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003612 }
3613}
3614
Damien George65cad122014-04-06 11:48:15 +01003615mp_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 +01003616 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003617 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003618 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003619 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003620
Damien826005c2013-10-05 23:17:28 +01003621 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003622 mp_map_t consts;
3623 mp_map_init(&consts, 0);
3624 pn = fold_constants(comp, pn, &consts);
3625 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003626
3627 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003628 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003629
Damien826005c2013-10-05 23:17:28 +01003630 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003631 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003632 comp->emit_method_table = &emit_pass1_method_table;
3633 comp->emit_inline_asm = NULL;
3634 comp->emit_inline_asm_method_table = NULL;
3635 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003636 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003637 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003638#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003639 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003640 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003641#endif
Damien826005c2013-10-05 23:17:28 +01003642 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003643 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003644 }
3645
3646 // update maximim number of labels needed
3647 if (comp->next_label > max_num_labels) {
3648 max_num_labels = comp->next_label;
3649 }
Damien429d7192013-10-04 19:53:11 +01003650 }
3651
Damien826005c2013-10-05 23:17:28 +01003652 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003653 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003654 compile_scope_compute_things(comp, s);
3655 }
3656
Damien826005c2013-10-05 23:17:28 +01003657 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003658 emit_pass1_free(comp->emit);
3659
Damien826005c2013-10-05 23:17:28 +01003660 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003661#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003662 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003663#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003664 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003665#endif
Damien3ef4abb2013-10-12 16:53:13 +01003666#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003667 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003668#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003669#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003670 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003671 if (false) {
3672 // dummy
3673
Damien3ef4abb2013-10-12 16:53:13 +01003674#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003675 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003676 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003677 if (emit_inline_thumb == NULL) {
3678 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3679 }
3680 comp->emit = NULL;
3681 comp->emit_method_table = NULL;
3682 comp->emit_inline_asm = emit_inline_thumb;
3683 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003684 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003685 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003686 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003687 }
Damienc025ebb2013-10-12 14:30:21 +01003688#endif
3689
Damien826005c2013-10-05 23:17:28 +01003690 } else {
Damienc025ebb2013-10-12 14:30:21 +01003691
3692 // choose the emit type
3693
Damien3ef4abb2013-10-12 16:53:13 +01003694#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003695 comp->emit = emit_cpython_new(max_num_labels);
3696 comp->emit_method_table = &emit_cpython_method_table;
3697#else
Damien826005c2013-10-05 23:17:28 +01003698 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003699
3700#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003701 case MP_EMIT_OPT_NATIVE_PYTHON:
3702 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003703#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003704 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003705 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003706 }
Damien13ed3a62013-10-08 09:05:10 +01003707 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003708#elif MICROPY_EMIT_X86
3709 if (emit_native == NULL) {
3710 emit_native = emit_native_x86_new(max_num_labels);
3711 }
3712 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003713#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003714 if (emit_native == NULL) {
3715 emit_native = emit_native_thumb_new(max_num_labels);
3716 }
3717 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003718#elif MICROPY_EMIT_ARM
3719 if (emit_native == NULL) {
3720 emit_native = emit_native_arm_new(max_num_labels);
3721 }
3722 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003723#endif
3724 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003725 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 +01003726
3727 // native emitters need an extra pass to compute stack size
3728 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3729
Damien7af3d192013-10-07 00:02:49 +01003730 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003731#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003732
Damien826005c2013-10-05 23:17:28 +01003733 default:
3734 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003735 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003736 }
3737 comp->emit = emit_bc;
3738 comp->emit_method_table = &emit_bc_method_table;
3739 break;
3740 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003741#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003742
Damien George36db6bc2014-05-07 17:24:22 +01003743 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003744 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003745 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3746 }
3747
3748 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003749 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003750 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003751 }
Damien6cdd3af2013-10-05 18:08:26 +01003752 }
Damien429d7192013-10-04 19:53:11 +01003753 }
3754
Damien George41d02b62014-01-24 22:42:28 +00003755 // free the emitters
3756#if !MICROPY_EMIT_CPYTHON
3757 if (emit_bc != NULL) {
3758 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003759 }
Damien George41d02b62014-01-24 22:42:28 +00003760#if MICROPY_EMIT_NATIVE
3761 if (emit_native != NULL) {
3762#if MICROPY_EMIT_X64
3763 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003764#elif MICROPY_EMIT_X86
3765 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003766#elif MICROPY_EMIT_THUMB
3767 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003768#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003769 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003770#endif
3771 }
3772#endif
3773#if MICROPY_EMIT_INLINE_THUMB
3774 if (emit_inline_thumb != NULL) {
3775 emit_inline_thumb_free(emit_inline_thumb);
3776 }
3777#endif
3778#endif // !MICROPY_EMIT_CPYTHON
3779
Damien George52b5d762014-09-23 15:31:56 +00003780 // free the parse tree
3781 mp_parse_node_free(pn);
3782
Damien George41d02b62014-01-24 22:42:28 +00003783 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003784 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003785 for (scope_t *s = module_scope; s;) {
3786 scope_t *next = s->next;
3787 scope_free(s);
3788 s = next;
3789 }
Damien5ac1b2e2013-10-18 19:58:12 +01003790
Damien George41d02b62014-01-24 22:42:28 +00003791 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003792 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003793 m_del_obj(compiler_t, comp);
3794
Damien Georgea91ac202014-10-05 19:01:34 +01003795 if (compile_error != MP_OBJ_NULL) {
3796 return compile_error;
Damien George1fb03172014-01-03 14:22:03 +00003797 } else {
3798#if MICROPY_EMIT_CPYTHON
3799 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003800 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003801 return mp_const_true;
3802#else
3803 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003804 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003805#endif
3806 }
Damien429d7192013-10-04 19:53:11 +01003807}