blob: 921de9b0d5acfc53eaad332abb1fadcf6b6c8aa8 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/scope.h"
35#include "py/emit.h"
36#include "py/compile.h"
37#include "py/smallint.h"
38#include "py/runtime.h"
39#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010040
41// TODO need to mangle __attr names
42
43typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000044#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000045#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010046#undef DEF_RULE
47 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010048 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000049 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000050 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010051} pn_kind_t;
52
Damien Georgeb9791222014-01-23 00:34:21 +000053#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
54#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
55#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
56#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 +010057
Damien Georgea91ac202014-10-05 19:01:34 +010058// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010059typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000060 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010061
Damien George78035b92014-04-09 12:27:39 +010062 uint8_t is_repl;
63 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010064 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010065 uint8_t have_star;
66
67 // try to keep compiler clean from nlr
68 // this is set to an exception object if we have a compile error
69 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010070
Damien George6f355fd2014-04-10 14:11:31 +010071 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010072
Damien George69b89d22014-04-11 13:38:30 +000073 uint16_t num_dict_params;
74 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010075
Damien Georgea91ac202014-10-05 19:01:34 +010076 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
77 uint16_t continue_label;
78 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000079 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010080
Damien429d7192013-10-04 19:53:11 +010081 scope_t *scope_head;
82 scope_t *scope_cur;
83
Damien6cdd3af2013-10-05 18:08:26 +010084 emit_t *emit; // current emitter
85 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010086
87 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
88 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 +010089} compiler_t;
90
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010091STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010092 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
93 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010094 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George8dfbd2d2015-02-13 01:00:51 +000095 mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010096 } else {
Damien Georgea91ac202014-10-05 19:01:34 +010097 // we don't have a line number, so just pass 0
Damien George8dfbd2d2015-02-13 01:00:51 +000098 mp_obj_exception_add_traceback(exc, comp->source_file, 0, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010099 }
Damien Georgea91ac202014-10-05 19:01:34 +0100100 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000101}
102
Damien Georgeddd1e182015-01-10 14:07:24 +0000103#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100104STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300105 #if MICROPY_PY_UCTYPES
106 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
107 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100108 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100109 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100110};
Damien Georgeddd1e182015-01-10 14:07:24 +0000111STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
112#endif
Damien George57e99eb2014-04-10 22:42:11 +0100113
Damien Georgeffae48d2014-05-08 15:58:39 +0000114// this function is essentially a simple preprocessor
115STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
116 if (0) {
117 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100118#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000119 } else if (MP_PARSE_NODE_IS_ID(pn)) {
120 // lookup identifier in table of dynamic constants
121 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
122 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
123 if (elem != NULL) {
124 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
125 }
126#endif
127 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000128 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100129
Damien Georgeffae48d2014-05-08 15:58:39 +0000130 // fold some parse nodes before folding their arguments
131 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100132#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000133 case PN_expr_stmt:
134 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
135 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
136 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
137 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
138 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
139 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
140 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
141 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
142 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
143 ) {
144 // code to assign dynamic constants: id = const(value)
145
146 // get the id
147 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
148
149 // get the value
150 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
151 pn_value = fold_constants(comp, pn_value, consts);
152 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
153 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
154 break;
155 }
Damien George40f3c022014-07-03 13:25:24 +0100156 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000157
158 // store the value in the table of dynamic constants
159 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
160 if (elem->value != MP_OBJ_NULL) {
161 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
162 break;
163 }
164 elem->value = MP_OBJ_NEW_SMALL_INT(value);
165
166 // replace const(value) with value
167 pns1->nodes[0] = pn_value;
168
169 // finished folding this assignment
170 return pn;
171 }
172 }
173 }
174 break;
175#endif
Damien George5042bce2014-05-25 22:06:06 +0100176 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000177 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000178 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100179 return pn;
Damien429d7192013-10-04 19:53:11 +0100180 }
181
Damien Georgeffae48d2014-05-08 15:58:39 +0000182 // fold arguments
183 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
184 for (int i = 0; i < n; i++) {
185 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
186 }
187
188 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000189 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100190 case PN_atom_paren:
191 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
192 // (int)
193 pn = pns->nodes[0];
194 }
195 break;
196
197 case PN_expr:
198 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
199 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100200 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
201 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100202 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
203 }
204 break;
205
206 case PN_and_expr:
207 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
208 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100209 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
210 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100211 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
212 }
213 break;
214
Damien429d7192013-10-04 19:53:11 +0100215 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000216 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 +0100217 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
218 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000219 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100220 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000221 if (!(arg1 >= (mp_int_t)BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
Damien Georgea26dc502014-04-12 17:54:52 +0100222 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
223 }
Damiend99b0522013-12-21 18:17:45 +0000224 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100225 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000226 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200227 // Shifting to big amounts is underfined behavior
228 // in C and is CPU-dependent; propagate sign bit.
229 arg1 = BITS_PER_WORD - 1;
230 }
Damiend99b0522013-12-21 18:17:45 +0000231 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100232 } else {
233 // shouldn't happen
234 assert(0);
235 }
236 }
237 break;
238
239 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100240 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000241 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 +0100242 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
243 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000244 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100245 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000246 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000247 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100248 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000249 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100250 } else {
251 // shouldn't happen
252 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100253 }
Damien Georged1e355e2014-05-28 14:51:12 +0100254 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100255 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000256 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100257 }
258 }
259 break;
260
261 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000262 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 +0100263 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
264 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000265 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100266 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000267 if (!mp_small_int_mul_overflow(arg0, arg1)) {
268 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100269 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000270 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
271 }
272 }
Damiend99b0522013-12-21 18:17:45 +0000273 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100274 // int / int
275 // pass
Damiend99b0522013-12-21 18:17:45 +0000276 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100277 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000278 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000279 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300280 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100281 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000282 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 +0300283 }
Damien429d7192013-10-04 19:53:11 +0100284 } else {
285 // shouldn't happen
286 assert(0);
287 }
288 }
289 break;
290
291 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000292 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100293 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000294 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100295 // +int
Damiend99b0522013-12-21 18:17:45 +0000296 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
297 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100298 // -int
Damiend99b0522013-12-21 18:17:45 +0000299 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
300 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100301 // ~int
Damiend99b0522013-12-21 18:17:45 +0000302 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100303 } else {
304 // shouldn't happen
305 assert(0);
306 }
307 }
308 break;
309
310 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100311 if (0) {
312#if MICROPY_EMIT_CPYTHON
313 } 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 +0100314 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100315 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000316 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
317 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200318 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100319 if (power >= 0) {
320 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200321 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100322 for (; power > 0; power--) {
323 ans *= base;
324 }
Damiend99b0522013-12-21 18:17:45 +0000325 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100326 }
327 }
Damien George57e99eb2014-04-10 22:42:11 +0100328#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000329#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100330 } 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 Georgeddd1e182015-01-10 14:07:24 +0000343 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100344 }
345 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000346#endif
Damien429d7192013-10-04 19:53:11 +0100347 }
348 break;
349 }
350 }
351
352 return pn;
353}
354
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200355STATIC 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 +0100356STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000357STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100358
Damien George6f355fd2014-04-10 14:11:31 +0100359STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100360 return comp->next_label++;
361}
362
Damien George8dcc0c72014-03-27 10:55:21 +0000363STATIC void compile_increase_except_level(compiler_t *comp) {
364 comp->cur_except_level += 1;
365 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
366 comp->scope_cur->exc_stack_size = comp->cur_except_level;
367 }
368}
369
370STATIC void compile_decrease_except_level(compiler_t *comp) {
371 assert(comp->cur_except_level > 0);
372 comp->cur_except_level -= 1;
373}
374
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200375STATIC 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 +0100376 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100377 scope->parent = comp->scope_cur;
378 scope->next = NULL;
379 if (comp->scope_head == NULL) {
380 comp->scope_head = scope;
381 } else {
382 scope_t *s = comp->scope_head;
383 while (s->next != NULL) {
384 s = s->next;
385 }
386 s->next = scope;
387 }
388 return scope;
389}
390
Damien George963a5a32015-01-16 17:47:07 +0000391STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
392 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000393 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
394 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100395 for (int i = 0; i < num_nodes; i++) {
396 f(comp, pns->nodes[i]);
397 }
Damiend99b0522013-12-21 18:17:45 +0000398 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100399 f(comp, pn);
400 }
401}
402
Damien George969a6b32014-12-10 22:07:04 +0000403STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000404 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100405 for (int i = 0; i < num_nodes; i++) {
406 compile_node(comp, pns->nodes[i]);
407 }
408}
409
Damien3ef4abb2013-10-12 16:53:13 +0100410#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200411STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100412 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
413 return true;
414 }
Damien George4c81ba82015-01-13 16:21:23 +0000415 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
416 return true;
417 }
Damien George7d414a12015-02-08 01:57:40 +0000418 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
419 return true;
420 }
Damiend99b0522013-12-21 18:17:45 +0000421 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100422 return false;
423 }
Damiend99b0522013-12-21 18:17:45 +0000424 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100425 return false;
426 }
427 return true;
428}
429
Damien George5042bce2014-05-25 22:06:06 +0100430STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000431 bool has_single_quote = false;
432 bool has_double_quote = false;
433 for (int i = 0; i < len; i++) {
434 if (str[i] == '\'') {
435 has_single_quote = true;
436 } else if (str[i] == '"') {
437 has_double_quote = true;
438 }
439 }
440 if (bytes) {
441 vstr_printf(vstr, "b");
442 }
443 bool quote_single = false;
444 if (has_single_quote && !has_double_quote) {
445 vstr_printf(vstr, "\"");
446 } else {
447 quote_single = true;
448 vstr_printf(vstr, "'");
449 }
450 for (int i = 0; i < len; i++) {
451 if (str[i] == '\n') {
452 vstr_printf(vstr, "\\n");
453 } else if (str[i] == '\\') {
454 vstr_printf(vstr, "\\\\");
455 } else if (str[i] == '\'' && quote_single) {
456 vstr_printf(vstr, "\\'");
457 } else {
458 vstr_printf(vstr, "%c", str[i]);
459 }
460 }
461 if (has_single_quote && !has_double_quote) {
462 vstr_printf(vstr, "\"");
463 } else {
464 vstr_printf(vstr, "'");
465 }
466}
467
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200468STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000469 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string) || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
Damien George5042bce2014-05-25 22:06:06 +0100470 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000471 cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes));
Damien George5042bce2014-05-25 22:06:06 +0100472 return;
473 }
474
Damien George7d414a12015-02-08 01:57:40 +0000475 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
476 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
477 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
478 return;
479 }
480
Damiend99b0522013-12-21 18:17:45 +0000481 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200482 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
483 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
484 return;
485 }
486
Damien George42f3de92014-10-03 17:44:14 +0000487 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000488 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
489 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100490 case MP_PARSE_NODE_STRING:
491 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100492 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100493 const byte *str = qstr_data(arg, &len);
494 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
495 break;
496 }
Damiend99b0522013-12-21 18:17:45 +0000497 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100498 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000499 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
500 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
501 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100502 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100503 }
504 break;
505 default: assert(0);
506 }
507}
508
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200509STATIC 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 +0100510 int n = 0;
511 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000512 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100513 }
514 int total = n;
515 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000516 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100517 total += 1;
Damien3a205172013-10-12 15:01:56 +0100518 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100519 is_const = false;
520 }
521 }
522 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100523 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100524 is_const = false;
525 break;
526 }
527 }
528 if (total > 0 && is_const) {
529 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000530 vstr_t *vstr = vstr_new();
531 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000532 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000533 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100534 need_comma = true;
535 }
536 for (int i = 0; i < n; i++) {
537 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000538 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100539 }
Damien02f89412013-12-12 15:13:36 +0000540 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100541 need_comma = true;
542 }
543 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000544 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100545 } else {
Damien02f89412013-12-12 15:13:36 +0000546 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100547 }
Damien George0d3cb672015-01-28 23:43:01 +0000548 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000549 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100550 } else {
Damiend99b0522013-12-21 18:17:45 +0000551 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100552 compile_node(comp, pn);
553 }
554 for (int i = 0; i < n; i++) {
555 compile_node(comp, pns_list->nodes[i]);
556 }
Damien Georgeb9791222014-01-23 00:34:21 +0000557 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100558 }
559}
Damien3a205172013-10-12 15:01:56 +0100560#endif
561
562// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100563STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100564#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100565 cpython_c_tuple(comp, pn, pns_list);
566#else
567 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000568 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100569 compile_node(comp, pn);
570 total += 1;
571 }
572 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000573 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100574 for (int i = 0; i < n; i++) {
575 compile_node(comp, pns_list->nodes[i]);
576 }
577 total += n;
578 }
Damien Georgeb9791222014-01-23 00:34:21 +0000579 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100580#endif
581}
Damien429d7192013-10-04 19:53:11 +0100582
Damien George969a6b32014-12-10 22:07:04 +0000583STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100584 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000585 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100586}
587
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200588STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000589 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
590 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100591}
592
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200593STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000594 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
595 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100596}
597
Damien3ef4abb2013-10-12 16:53:13 +0100598#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100599// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200600STATIC 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 +0100601 if (node_is_const_false(pn)) {
602 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000603 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100604 }
605 return;
606 } else if (node_is_const_true(pn)) {
607 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000608 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100609 }
610 return;
Damiend99b0522013-12-21 18:17:45 +0000611 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
612 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
613 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
614 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100615 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100616 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100617 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100618 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100619 }
Damien3a205172013-10-12 15:01:56 +0100620 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000621 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100622 } else {
623 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100624 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100625 }
626 }
627 return;
Damiend99b0522013-12-21 18:17:45 +0000628 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100629 if (jump_if == false) {
630 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100631 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100632 }
633 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100634 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100635 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100636 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100637 }
Damien3a205172013-10-12 15:01:56 +0100638 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000639 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100640 }
641 return;
Damiend99b0522013-12-21 18:17:45 +0000642 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100643 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100644 return;
645 }
646 }
647
648 // nothing special, fall back to default compiling for node and jump
649 compile_node(comp, pn);
650 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000651 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100652 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000653 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100654 }
655}
Damien3a205172013-10-12 15:01:56 +0100656#endif
Damien429d7192013-10-04 19:53:11 +0100657
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200658STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100659#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100660 cpython_c_if_cond(comp, pn, jump_if, label, false);
661#else
662 if (node_is_const_false(pn)) {
663 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000664 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100665 }
666 return;
667 } else if (node_is_const_true(pn)) {
668 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000669 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100670 }
671 return;
Damiend99b0522013-12-21 18:17:45 +0000672 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
673 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
674 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
675 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100676 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100677 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100678 for (int i = 0; i < n - 1; i++) {
679 c_if_cond(comp, pns->nodes[i], true, label2);
680 }
681 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000682 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100683 } else {
684 for (int i = 0; i < n; i++) {
685 c_if_cond(comp, pns->nodes[i], true, label);
686 }
687 }
688 return;
Damiend99b0522013-12-21 18:17:45 +0000689 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100690 if (jump_if == false) {
691 for (int i = 0; i < n; i++) {
692 c_if_cond(comp, pns->nodes[i], false, label);
693 }
694 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100695 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100696 for (int i = 0; i < n - 1; i++) {
697 c_if_cond(comp, pns->nodes[i], false, label2);
698 }
699 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000700 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100701 }
702 return;
Damiend99b0522013-12-21 18:17:45 +0000703 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100704 c_if_cond(comp, pns->nodes[0], !jump_if, label);
705 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100706 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
707 // cond is something in parenthesis
708 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
709 // empty tuple, acts as false for the condition
710 if (jump_if == false) {
711 EMIT_ARG(jump, label);
712 }
713 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
714 // non-empty tuple, acts as true for the condition
715 if (jump_if == true) {
716 EMIT_ARG(jump, label);
717 }
718 } else {
719 // parenthesis around 1 item, is just that item
720 c_if_cond(comp, pns->nodes[0], jump_if, label);
721 }
722 return;
Damien3a205172013-10-12 15:01:56 +0100723 }
724 }
725
726 // nothing special, fall back to default compiling for node and jump
727 compile_node(comp, pn);
728 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000729 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100730 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000731 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100732 }
733#endif
Damien429d7192013-10-04 19:53:11 +0100734}
735
736typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100737STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100738
Damien George6be0b0a2014-08-15 14:30:52 +0100739STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100740 if (assign_kind != ASSIGN_AUG_STORE) {
741 compile_node(comp, pns->nodes[0]);
742 }
743
Damiend99b0522013-12-21 18:17:45 +0000744 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
745 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
746 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
747 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100748 if (assign_kind != ASSIGN_AUG_STORE) {
749 for (int i = 0; i < n - 1; i++) {
750 compile_node(comp, pns1->nodes[i]);
751 }
752 }
Damiend99b0522013-12-21 18:17:45 +0000753 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
754 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100755 }
Damiend99b0522013-12-21 18:17:45 +0000756 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100757 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000758 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100759 if (assign_kind == ASSIGN_AUG_STORE) {
760 EMIT(rot_three);
761 EMIT(store_subscr);
762 } else {
763 compile_node(comp, pns1->nodes[0]);
764 if (assign_kind == ASSIGN_AUG_LOAD) {
765 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100766 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100767 } else {
768 EMIT(store_subscr);
769 }
770 }
Damiend99b0522013-12-21 18:17:45 +0000771 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
772 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100773 if (assign_kind == ASSIGN_AUG_LOAD) {
774 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000775 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100776 } else {
777 if (assign_kind == ASSIGN_AUG_STORE) {
778 EMIT(rot_two);
779 }
Damien Georgeb9791222014-01-23 00:34:21 +0000780 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100781 }
782 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100783 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100784 }
785 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100786 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100787 }
788
Damiend99b0522013-12-21 18:17:45 +0000789 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100790 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100791 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100792
793 return;
794
795cannot_assign:
796 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100797}
798
Damien George0288cf02014-04-11 11:53:00 +0000799// 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 +0100800STATIC 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 +0000801 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
802
803 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000804 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000805 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
806 EMIT_ARG(unpack_ex, 0, num_tail);
807 have_star_index = 0;
808 }
Damien George963a5a32015-01-16 17:47:07 +0000809 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000810 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000811 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000812 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
813 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100814 } else {
Damien George9d181f62014-04-27 16:55:27 +0100815 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100816 return;
817 }
818 }
819 }
Damien George963a5a32015-01-16 17:47:07 +0000820 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000821 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100822 }
Damien George0288cf02014-04-11 11:53:00 +0000823 if (num_head != 0) {
824 if (0 == have_star_index) {
825 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100826 } else {
Damien George0288cf02014-04-11 11:53:00 +0000827 c_assign(comp, node_head, ASSIGN_STORE);
828 }
829 }
Damien George963a5a32015-01-16 17:47:07 +0000830 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000831 if (num_head + i == have_star_index) {
832 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
833 } else {
834 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100835 }
836 }
837}
838
839// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100840STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100841 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000842 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100843 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000844 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
845 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000846 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100847 switch (assign_kind) {
848 case ASSIGN_STORE:
849 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000850 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100851 break;
852 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000853 default:
Damien Georgeb9791222014-01-23 00:34:21 +0000854 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100855 break;
856 }
857 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100858 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100859 return;
860 }
861 } else {
Damiend99b0522013-12-21 18:17:45 +0000862 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
863 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100864 case PN_power:
865 // lhs is an index or attribute
866 c_assign_power(comp, pns, assign_kind);
867 break;
868
869 case PN_testlist_star_expr:
870 case PN_exprlist:
871 // lhs is a tuple
872 if (assign_kind != ASSIGN_STORE) {
873 goto bad_aug;
874 }
Damien George0288cf02014-04-11 11:53:00 +0000875 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100876 break;
877
878 case PN_atom_paren:
879 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000880 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100881 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100882 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000883 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
884 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100885 goto testlist_comp;
886 } else {
887 // parenthesis around 1 item, is just that item
888 pn = pns->nodes[0];
889 goto tail_recursion;
890 }
891 break;
892
893 case PN_atom_bracket:
894 // lhs is something in brackets
895 if (assign_kind != ASSIGN_STORE) {
896 goto bad_aug;
897 }
Damiend99b0522013-12-21 18:17:45 +0000898 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100899 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000900 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000901 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
902 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100903 goto testlist_comp;
904 } else {
905 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000906 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100907 }
908 break;
909
910 default:
Damien George9d181f62014-04-27 16:55:27 +0100911 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100912 }
913 return;
914
915 testlist_comp:
916 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000917 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
918 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
919 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100920 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000921 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000922 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000923 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100924 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000925 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
926 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000927 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100928 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100929 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100930 } else {
931 // sequence with 2 items
932 goto sequence_with_2_items;
933 }
934 } else {
935 // sequence with 2 items
936 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000937 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100938 }
939 return;
940 }
941 return;
942
Damien George9d181f62014-04-27 16:55:27 +0100943 cannot_assign:
944 compile_syntax_error(comp, pn, "can't assign to expression");
945 return;
946
Damien429d7192013-10-04 19:53:11 +0100947 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100948 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100949}
950
951// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100952// if we are not in CPython compatibility mode then:
953// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
954// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
955// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100956STATIC 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 +0100957 assert(n_pos_defaults >= 0);
958 assert(n_kw_defaults >= 0);
959
Damien429d7192013-10-04 19:53:11 +0100960 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000961 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100962 int nfree = 0;
963 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000964 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
965 id_info_t *id = &comp->scope_cur->id_info[i];
966 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
967 for (int j = 0; j < this_scope->id_info_len; j++) {
968 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100969 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000970#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100971 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000972#else
973 // in Micro Python we load closures using LOAD_FAST
Damien George0abb5602015-01-16 12:24:49 +0000974 EMIT_ARG(load_fast, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000975#endif
Damien318aec62013-12-10 18:28:17 +0000976 nfree += 1;
977 }
978 }
Damien429d7192013-10-04 19:53:11 +0100979 }
980 }
981 }
Damien429d7192013-10-04 19:53:11 +0100982
983 // make the function/closure
984 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100985 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100986 } else {
Damien George3558f622014-04-20 17:50:40 +0100987 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100988 }
989}
990
Damien George6be0b0a2014-08-15 14:30:52 +0100991STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000992 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100993 comp->have_star = true;
994 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000995 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
996 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100997 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100998 } else {
999 // named star
Damien429d7192013-10-04 19:53:11 +01001000 }
Damien George8b19db02014-04-11 23:25:34 +01001001 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001002
1003 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001004 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001005 // TODO do we need to do anything with this?
1006
1007 } else {
1008 mp_parse_node_t pn_id;
1009 mp_parse_node_t pn_colon;
1010 mp_parse_node_t pn_equal;
1011 if (MP_PARSE_NODE_IS_ID(pn)) {
1012 // this parameter is just an id
1013
1014 pn_id = pn;
1015 pn_colon = MP_PARSE_NODE_NULL;
1016 pn_equal = MP_PARSE_NODE_NULL;
1017
1018 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1019 // this parameter has a colon and/or equal specifier
1020
1021 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1022 pn_id = pns->nodes[0];
1023 pn_colon = pns->nodes[1];
1024 pn_equal = pns->nodes[2];
1025
1026 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001027 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001028 assert(0);
1029 return;
1030 }
1031
1032 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1033 // this parameter does not have a default value
1034
1035 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001036 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001037 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001038 return;
1039 }
1040
1041 } else {
1042 // this parameter has a default value
1043 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1044
Damien George8b19db02014-04-11 23:25:34 +01001045 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001046 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001047#if MICROPY_EMIT_CPYTHON
1048 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1049 compile_node(comp, pn_equal);
1050#else
Damien George69b89d22014-04-11 13:38:30 +00001051 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1052 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001053 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1054 // we need to do this here before we start building the map for the default keywords
1055 if (comp->num_default_params > 0) {
1056 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001057 } else {
1058 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001059 }
Damien George69b89d22014-04-11 13:38:30 +00001060 // first default dict param, so make the map
1061 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001062 }
Damien Georgef0778a72014-06-07 22:01:00 +01001063
1064 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001065 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001066 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001067 EMIT(store_map);
1068#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001069 } else {
Damien George69b89d22014-04-11 13:38:30 +00001070 comp->num_default_params += 1;
1071 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001072 }
1073 }
1074
1075 // TODO pn_colon not implemented
1076 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001077 }
1078}
1079
1080// leaves function object on stack
1081// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001082STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001083 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001084 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001085 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001086 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001087 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001088 }
1089
1090 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001091 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001092 uint old_num_dict_params = comp->num_dict_params;
1093 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001094
1095 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001096 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001097 comp->num_dict_params = 0;
1098 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001099 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001100
Damien Georgea91ac202014-10-05 19:01:34 +01001101 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001102 return MP_QSTR_NULL;
1103 }
1104
Damien Georgee337f1e2014-03-31 15:18:37 +01001105#if !MICROPY_EMIT_CPYTHON
1106 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001107 // the default keywords args may have already made the tuple; if not, do it now
1108 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001109 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001110 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001111 }
1112#endif
1113
Damien429d7192013-10-04 19:53:11 +01001114 // get the scope for this function
1115 scope_t *fscope = (scope_t*)pns->nodes[4];
1116
1117 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001118 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001119
1120 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001121 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001122 comp->num_dict_params = old_num_dict_params;
1123 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001124
1125 // return its name (the 'f' in "def f(...):")
1126 return fscope->simple_name;
1127}
1128
1129// leaves class object on stack
1130// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001131STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001132 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001133 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001134 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001135 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001136 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001137 }
1138
1139 EMIT(load_build_class);
1140
1141 // scope for this class
1142 scope_t *cscope = (scope_t*)pns->nodes[3];
1143
1144 // compile the class
1145 close_over_variables_etc(comp, cscope, 0, 0);
1146
1147 // get its name
Damien George968bf342014-04-27 19:12:05 +01001148 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001149
1150 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001151 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1152 mp_parse_node_t parents = pns->nodes[1];
1153 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1154 parents = MP_PARSE_NODE_NULL;
1155 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001156 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001157 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001158
1159 // return its name (the 'C' in class C(...):")
1160 return cscope->simple_name;
1161}
1162
Damien6cdd3af2013-10-05 18:08:26 +01001163// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001164STATIC 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 +00001165 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001166 return false;
1167 }
1168
1169 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001170 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001171 return true;
1172 }
1173
Damiend99b0522013-12-21 18:17:45 +00001174 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001175 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001176 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001177#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001178 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001179 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001180 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001181 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001182#endif
Damien3ef4abb2013-10-12 16:53:13 +01001183#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001184 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001185 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001186#endif
Damien6cdd3af2013-10-05 18:08:26 +01001187 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001188 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001189 }
1190
1191 return true;
1192}
1193
Damien George969a6b32014-12-10 22:07:04 +00001194STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001195 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001196 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001197 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001198
Damien6cdd3af2013-10-05 18:08:26 +01001199 // inherit emit options for this function/class definition
1200 uint emit_options = comp->scope_cur->emit_options;
1201
1202 // compile each decorator
1203 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001204 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001205 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1206 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001207
1208 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001209 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001210 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001211
1212 // check for built-in decorators
1213 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1214 // this was a built-in
1215 num_built_in_decorators += 1;
1216
1217 } else {
1218 // not a built-in, compile normally
1219
1220 // compile the decorator function
1221 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001222 for (int j = 1; j < name_len; j++) {
1223 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1224 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001225 }
1226
1227 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001228 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001229 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001230 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001231 compile_node(comp, pns_decorator->nodes[1]);
1232 }
Damien429d7192013-10-04 19:53:11 +01001233 }
1234 }
1235
1236 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001237 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001238 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001239 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001240 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001241 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001242 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001243 } else {
1244 // shouldn't happen
1245 assert(0);
1246 }
1247
1248 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001249 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001250 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001251 }
1252
1253 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001254 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001255}
1256
Damien George969a6b32014-12-10 22:07:04 +00001257STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001258 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001259 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001260 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001261}
1262
Damien George6be0b0a2014-08-15 14:30:52 +01001263STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001264 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001265 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001266 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1267 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001268
1269 compile_node(comp, pns->nodes[0]); // base of the power node
1270
Damiend99b0522013-12-21 18:17:45 +00001271 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1272 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1273 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1274 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001275 for (int i = 0; i < n - 1; i++) {
1276 compile_node(comp, pns1->nodes[i]);
1277 }
Damiend99b0522013-12-21 18:17:45 +00001278 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1279 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001280 }
Damiend99b0522013-12-21 18:17:45 +00001281 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001282 // can't delete function calls
1283 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001284 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001285 compile_node(comp, pns1->nodes[0]);
1286 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001287 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1288 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001289 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001290 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001291 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001292 }
1293 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001294 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001295 }
1296
Damiend99b0522013-12-21 18:17:45 +00001297 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001298 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001299 }
Damiend99b0522013-12-21 18:17:45 +00001300 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1301 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1302 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1303 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001304 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1305
Damiend99b0522013-12-21 18:17:45 +00001306 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1307 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1308 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001309 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001310 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001311 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001312 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001313 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001314 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001315 c_del_stmt(comp, pns->nodes[0]);
1316 for (int i = 0; i < n; i++) {
1317 c_del_stmt(comp, pns1->nodes[i]);
1318 }
Damiend99b0522013-12-21 18:17:45 +00001319 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001320 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001321 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001322 } else {
1323 // sequence with 2 items
1324 goto sequence_with_2_items;
1325 }
1326 } else {
1327 // sequence with 2 items
1328 sequence_with_2_items:
1329 c_del_stmt(comp, pns->nodes[0]);
1330 c_del_stmt(comp, pns->nodes[1]);
1331 }
1332 } else {
1333 // tuple with 1 element
1334 c_del_stmt(comp, pn);
1335 }
1336 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001337 // TODO is there anything else to implement?
1338 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001339 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001340
1341 return;
1342
1343cannot_delete:
1344 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001345}
1346
Damien George969a6b32014-12-10 22:07:04 +00001347STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001348 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1349}
1350
Damien George969a6b32014-12-10 22:07:04 +00001351STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001352 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001353 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001354 }
Damien George090c9232014-10-17 14:08:49 +00001355 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001356 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001357}
1358
Damien George969a6b32014-12-10 22:07:04 +00001359STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001360 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001361 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001362 }
Damien George090c9232014-10-17 14:08:49 +00001363 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001364 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001365}
1366
Damien George969a6b32014-12-10 22:07:04 +00001367STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001368 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001369 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001370 return;
1371 }
Damiend99b0522013-12-21 18:17:45 +00001372 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001373 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001374 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001375 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001376 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001377 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1378 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 +01001379
Damien George6f355fd2014-04-10 14:11:31 +01001380 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001381 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1382 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1383 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001384 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001385 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1386 } else {
1387 compile_node(comp, pns->nodes[0]);
1388 }
1389 EMIT(return_value);
1390}
1391
Damien George969a6b32014-12-10 22:07:04 +00001392STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001393 compile_node(comp, pns->nodes[0]);
1394 EMIT(pop_top);
1395}
1396
Damien George969a6b32014-12-10 22:07:04 +00001397STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001398 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001399 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001400 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001401 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001402 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001403 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001404 compile_node(comp, pns->nodes[0]);
1405 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001406 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001407 } else {
1408 // raise x
1409 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001410 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001411 }
1412}
1413
Damien George635543c2014-04-10 12:56:52 +01001414// q_base holds the base of the name
1415// eg a -> q_base=a
1416// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001417STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001418 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001419 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1420 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001421 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001422 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001423 pn = pns->nodes[0];
1424 is_as = true;
1425 }
Damien George635543c2014-04-10 12:56:52 +01001426 if (MP_PARSE_NODE_IS_NULL(pn)) {
1427 // empty name (eg, from . import x)
1428 *q_base = MP_QSTR_;
1429 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1430 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001431 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001432 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001433 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001434 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001435 }
Damien George635543c2014-04-10 12:56:52 +01001436 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001437 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1438 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1439 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001440 // a name of the form a.b.c
1441 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001442 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001443 }
Damiend99b0522013-12-21 18:17:45 +00001444 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001445 int len = n - 1;
1446 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001447 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001448 }
Damien George55baff42014-01-21 21:40:13 +00001449 byte *q_ptr;
1450 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001451 for (int i = 0; i < n; i++) {
1452 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001453 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001454 }
Damien George39dc1452014-10-03 19:52:22 +01001455 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001456 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001457 memcpy(str_dest, str_src, str_src_len);
1458 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001459 }
Damien George635543c2014-04-10 12:56:52 +01001460 qstr q_full = qstr_build_end(q_ptr);
1461 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001462 if (is_as) {
1463 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001464 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001465 }
1466 }
1467 } else {
Damien George635543c2014-04-10 12:56:52 +01001468 // shouldn't happen
1469 assert(0);
Damien429d7192013-10-04 19:53:11 +01001470 }
1471 } else {
Damien George635543c2014-04-10 12:56:52 +01001472 // shouldn't happen
1473 assert(0);
Damien429d7192013-10-04 19:53:11 +01001474 }
1475}
1476
Damien George6be0b0a2014-08-15 14:30:52 +01001477STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001478 EMIT_ARG(load_const_small_int, 0); // level 0 import
1479 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1480 qstr q_base;
1481 do_import_name(comp, pn, &q_base);
1482 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001483}
1484
Damien George969a6b32014-12-10 22:07:04 +00001485STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001486 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1487}
1488
Damien George969a6b32014-12-10 22:07:04 +00001489STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001490 mp_parse_node_t pn_import_source = pns->nodes[0];
1491
1492 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1493 uint import_level = 0;
1494 do {
1495 mp_parse_node_t pn_rel;
1496 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)) {
1497 // This covers relative imports with dots only like "from .. import"
1498 pn_rel = pn_import_source;
1499 pn_import_source = MP_PARSE_NODE_NULL;
1500 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1501 // This covers relative imports starting with dot(s) like "from .foo import"
1502 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1503 pn_rel = pns_2b->nodes[0];
1504 pn_import_source = pns_2b->nodes[1];
1505 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1506 } else {
1507 // Not a relative import
1508 break;
1509 }
1510
1511 // get the list of . and/or ...'s
1512 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001513 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001514
1515 // count the total number of .'s
1516 for (int i = 0; i < n; i++) {
1517 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1518 import_level++;
1519 } else {
1520 // should be an MP_TOKEN_ELLIPSIS
1521 import_level += 3;
1522 }
1523 }
1524 } while (0);
1525
Damiend99b0522013-12-21 18:17:45 +00001526 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001527 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001528
1529 // build the "fromlist" tuple
1530#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001531 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001532#else
Damien George708c0732014-04-27 19:23:46 +01001533 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001534 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001535#endif
1536
1537 // do the import
Damien George635543c2014-04-10 12:56:52 +01001538 qstr dummy_q;
1539 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001540 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001541
Damien429d7192013-10-04 19:53:11 +01001542 } else {
Damien George635543c2014-04-10 12:56:52 +01001543 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001544
1545 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001546 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001547 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001548#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001549 {
1550 vstr_t *vstr = vstr_new();
1551 vstr_printf(vstr, "(");
1552 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001553 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1554 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1555 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001556 if (i > 0) {
1557 vstr_printf(vstr, ", ");
1558 }
1559 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001560 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001561 const byte *str = qstr_data(id2, &len);
1562 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001563 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001564 }
Damien02f89412013-12-12 15:13:36 +00001565 if (n == 1) {
1566 vstr_printf(vstr, ",");
1567 }
1568 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001569 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001570 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001571 }
Damiendb4c3612013-12-10 17:27:24 +00001572#else
1573 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001574 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1575 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1576 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001577 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001578 }
Damien Georgeb9791222014-01-23 00:34:21 +00001579 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001580#endif
1581
1582 // do the import
Damien George635543c2014-04-10 12:56:52 +01001583 qstr dummy_q;
1584 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001585 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001586 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1587 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1588 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001589 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001590 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001591 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001592 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001593 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001594 }
1595 }
1596 EMIT(pop_top);
1597 }
1598}
1599
Damien George584ba672014-12-21 17:26:45 +00001600STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1601 bool added;
1602 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1603 if (!added) {
1604 compile_syntax_error(comp, pn, "identifier already used");
1605 return;
1606 }
1607 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1608
1609 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1610 id_info = scope_find_global(comp->scope_cur, qst);
1611 if (id_info != NULL) {
1612 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1613 }
1614}
1615
Damien George969a6b32014-12-10 22:07:04 +00001616STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001617 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001618 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001619 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001620 for (int i = 0; i < n; i++) {
1621 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001622 }
1623 }
1624}
1625
Damien George584ba672014-12-21 17:26:45 +00001626STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1627 bool added;
1628 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1629 if (!added) {
1630 compile_syntax_error(comp, pn, "identifier already used");
1631 return;
1632 }
1633 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1634 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)) {
1635 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1636 return;
1637 }
1638 id_info->kind = ID_INFO_KIND_FREE;
1639 scope_close_over_in_parents(comp->scope_cur, qst);
1640}
1641
Damien George969a6b32014-12-10 22:07:04 +00001642STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001643 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001644 if (comp->scope_cur->kind == SCOPE_MODULE) {
1645 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1646 return;
1647 }
1648 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001649 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001650 for (int i = 0; i < n; i++) {
1651 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001652 }
1653 }
1654}
1655
Damien George969a6b32014-12-10 22:07:04 +00001656STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001657 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001658 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001659 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 +00001660 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001661 // assertion message
1662 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001663 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001664 }
Damien Georgeb9791222014-01-23 00:34:21 +00001665 EMIT_ARG(raise_varargs, 1);
1666 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001667}
1668
Damien George969a6b32014-12-10 22:07:04 +00001669STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001670 // TODO proper and/or short circuiting
1671
Damien George6f355fd2014-04-10 14:11:31 +01001672 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001673
Damien George391db862014-10-17 17:57:33 +00001674 // optimisation: don't emit anything when "if False" (not in CPython)
1675 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1676 uint l_fail = comp_next_label(comp);
1677 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001678
Damien George391db862014-10-17 17:57:33 +00001679 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001680
Damien George391db862014-10-17 17:57:33 +00001681 // optimisation: skip everything else when "if True" (not in CPython)
1682 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1683 goto done;
1684 }
1685
1686 if (
1687 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1688 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1689 // optimisation: don't jump if last instruction was return
1690 && !EMIT(last_emit_was_return_value)
1691 ) {
1692 // jump over elif/else blocks
1693 EMIT_ARG(jump, l_end);
1694 }
1695
1696 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001697 }
1698
Damien George235f9b32014-10-17 17:30:16 +00001699 // compile elif blocks (if any)
1700 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001701 int n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
Damien George235f9b32014-10-17 17:30:16 +00001702 for (int i = 0; i < n_elif; i++) {
1703 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1704 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001705
Damien George391db862014-10-17 17:57:33 +00001706 // optimisation: don't emit anything when "if False" (not in CPython)
1707 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1708 uint l_fail = comp_next_label(comp);
1709 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001710
Damien George391db862014-10-17 17:57:33 +00001711 compile_node(comp, pns_elif->nodes[1]); // elif block
1712
1713 // optimisation: skip everything else when "elif True" (not in CPython)
1714 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1715 goto done;
1716 }
1717
1718 // optimisation: don't jump if last instruction was return
1719 if (!EMIT(last_emit_was_return_value)) {
1720 EMIT_ARG(jump, l_end);
1721 }
1722 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001723 }
1724 }
1725
1726 // compile else block
1727 compile_node(comp, pns->nodes[3]); // can be null
1728
Damien George391db862014-10-17 17:57:33 +00001729done:
Damien Georgeb9791222014-01-23 00:34:21 +00001730 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001731}
1732
Damien Georgecbddb272014-02-01 20:08:18 +00001733#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001734 uint16_t old_break_label = comp->break_label; \
1735 uint16_t old_continue_label = comp->continue_label; \
1736 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001737 uint break_label = comp_next_label(comp); \
1738 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001739 comp->break_label = break_label; \
1740 comp->continue_label = continue_label; \
1741 comp->break_continue_except_level = comp->cur_except_level;
1742
1743#define END_BREAK_CONTINUE_BLOCK \
1744 comp->break_label = old_break_label; \
1745 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001746 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001747
Damien George969a6b32014-12-10 22:07:04 +00001748STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001749 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001750
Damience89a212013-10-15 22:25:17 +01001751 // compared to CPython, we have an optimised version of while loops
1752#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001753 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001754 EMIT_ARG(setup_loop, break_label);
1755 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001756 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1757 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001758 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001759 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001760 }
Damien Georgeb9791222014-01-23 00:34:21 +00001761 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001762 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1763 // this is a small hack to agree with CPython
1764 if (!node_is_const_true(pns->nodes[0])) {
1765 EMIT(pop_block);
1766 }
Damience89a212013-10-15 22:25:17 +01001767#else
Damien George391db862014-10-17 17:57:33 +00001768 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1769 uint top_label = comp_next_label(comp);
1770 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1771 EMIT_ARG(jump, continue_label);
1772 }
1773 EMIT_ARG(label_assign, top_label);
1774 compile_node(comp, pns->nodes[1]); // body
1775 EMIT_ARG(label_assign, continue_label);
1776 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1777 }
Damience89a212013-10-15 22:25:17 +01001778#endif
1779
1780 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001781 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001782
1783 compile_node(comp, pns->nodes[2]); // else
1784
Damien Georgeb9791222014-01-23 00:34:21 +00001785 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001786}
1787
Damien George2ac4af62014-08-15 16:45:41 +01001788#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001789// This function compiles an optimised for-loop of the form:
1790// for <var> in range(<start>, <end>, <step>):
1791// <body>
1792// else:
1793// <else>
1794// <var> must be an identifier and <step> must be a small-int.
1795//
1796// Semantics of for-loop require:
1797// - final failing value should not be stored in the loop variable
1798// - if the loop never runs, the loop variable should never be assigned
1799// - assignments to <var>, <end> or <step> in the body do not alter the loop
1800// (<step> is a constant for us, so no need to worry about it changing)
1801//
1802// If <end> is a small-int, then the stack during the for-loop contains just
1803// the current value of <var>. Otherwise, the stack contains <end> then the
1804// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001805STATIC 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 +00001806 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001807
Damien George6f355fd2014-04-10 14:11:31 +01001808 uint top_label = comp_next_label(comp);
1809 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001810
Damien Georgee181c0d2014-12-12 17:19:56 +00001811 // put the end value on the stack if it's not a small-int constant
1812 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1813 if (end_on_stack) {
1814 compile_node(comp, pn_end);
1815 }
1816
1817 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001818 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001819
Damien Georgeb9791222014-01-23 00:34:21 +00001820 EMIT_ARG(jump, entry_label);
1821 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001822
Damien Georgec33ce602014-12-11 17:35:23 +00001823 // duplicate next value and store it to var
1824 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001825 c_assign(comp, pn_var, ASSIGN_STORE);
1826
Damienf3822fc2013-11-09 20:12:03 +00001827 // compile body
1828 compile_node(comp, pn_body);
1829
Damien Georgeb9791222014-01-23 00:34:21 +00001830 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001831
Damien Georgee181c0d2014-12-12 17:19:56 +00001832 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001833 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001834 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001835
Damien Georgeb9791222014-01-23 00:34:21 +00001836 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001837
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001838 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001839 if (end_on_stack) {
1840 EMIT(dup_top_two);
1841 EMIT(rot_two);
1842 } else {
1843 EMIT(dup_top);
1844 compile_node(comp, pn_end);
1845 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001846 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1847 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001848 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001849 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001850 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001851 }
Damien Georgeb9791222014-01-23 00:34:21 +00001852 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001853
1854 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001855 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001856
1857 compile_node(comp, pn_else);
1858
Damien Georgeb9791222014-01-23 00:34:21 +00001859 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001860
1861 // discard final value of var that failed the loop condition
1862 EMIT(pop_top);
1863
1864 // discard <end> value if it's on the stack
1865 if (end_on_stack) {
1866 EMIT(pop_top);
1867 }
Damienf72fd0e2013-11-06 20:20:49 +00001868}
Damien George2ac4af62014-08-15 16:45:41 +01001869#endif
Damienf72fd0e2013-11-06 20:20:49 +00001870
Damien George969a6b32014-12-10 22:07:04 +00001871STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001872#if !MICROPY_EMIT_CPYTHON
1873 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1874 // this is actually slower, but uses no heap memory
1875 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001876 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 +00001877 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001878 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1879 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1880 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1881 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001882 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1883 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001884 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001885 mp_parse_node_t pn_range_start;
1886 mp_parse_node_t pn_range_end;
1887 mp_parse_node_t pn_range_step;
1888 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001889 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001890 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001891 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001892 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001893 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001894 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001895 } else if (n_args == 2) {
1896 pn_range_start = args[0];
1897 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001898 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001899 } else {
1900 pn_range_start = args[0];
1901 pn_range_end = args[1];
1902 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001903 // We need to know sign of step. This is possible only if it's constant
1904 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1905 optimize = false;
1906 }
Damienf72fd0e2013-11-06 20:20:49 +00001907 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001908 }
1909 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001910 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1911 return;
1912 }
1913 }
1914 }
1915#endif
1916
Damien Georgecbddb272014-02-01 20:08:18 +00001917 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001918 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001919
Damien George6f355fd2014-04-10 14:11:31 +01001920 uint pop_label = comp_next_label(comp);
1921 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001922
Damience89a212013-10-15 22:25:17 +01001923 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1924#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001925 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001926#endif
1927
Damien429d7192013-10-04 19:53:11 +01001928 compile_node(comp, pns->nodes[1]); // iterator
1929 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001930 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001931 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001932 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1933 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001934 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001935 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001936 }
Damien Georgeb9791222014-01-23 00:34:21 +00001937 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001938 EMIT(for_iter_end);
1939
1940 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001941 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001942
Damience89a212013-10-15 22:25:17 +01001943#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001944 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001945#endif
Damien429d7192013-10-04 19:53:11 +01001946
1947 compile_node(comp, pns->nodes[3]); // else (not tested)
1948
Damien Georgeb9791222014-01-23 00:34:21 +00001949 EMIT_ARG(label_assign, break_label);
1950 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001951}
1952
Damien George6be0b0a2014-08-15 14:30:52 +01001953STATIC 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 +01001954 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001955 uint l1 = comp_next_label(comp);
1956 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001957
Damien Georgeb9791222014-01-23 00:34:21 +00001958 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001959 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001960
Damien429d7192013-10-04 19:53:11 +01001961 compile_node(comp, pn_body); // body
1962 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001963 EMIT_ARG(jump, success_label); // jump over exception handler
1964
1965 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001966 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001967
Damien George6f355fd2014-04-10 14:11:31 +01001968 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001969
1970 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001971 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1972 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001973
1974 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001975 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001976
Damiend99b0522013-12-21 18:17:45 +00001977 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001978 // this is a catch all exception handler
1979 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001980 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001981 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001982 return;
1983 }
1984 } else {
1985 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001986 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1987 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1988 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1989 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001990 // handler binds the exception to a local
1991 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001992 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001993 }
1994 }
1995 EMIT(dup_top);
1996 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001997 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001998 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001999 }
2000
2001 EMIT(pop_top);
2002
2003 if (qstr_exception_local == 0) {
2004 EMIT(pop_top);
2005 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002006 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002007 }
2008
2009 EMIT(pop_top);
2010
Damien George6f355fd2014-04-10 14:11:31 +01002011 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002012 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002013 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002014 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002015 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002016 }
2017 compile_node(comp, pns_except->nodes[1]);
2018 if (qstr_exception_local != 0) {
2019 EMIT(pop_block);
2020 }
2021 EMIT(pop_except);
2022 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002023 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2024 EMIT_ARG(label_assign, l3);
2025 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2026 EMIT_ARG(store_id, qstr_exception_local);
2027 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002028
Damien George8dcc0c72014-03-27 10:55:21 +00002029 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002030 EMIT(end_finally);
2031 }
Damien Georgeb9791222014-01-23 00:34:21 +00002032 EMIT_ARG(jump, l2);
2033 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002034 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002035 }
2036
Damien George8dcc0c72014-03-27 10:55:21 +00002037 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002038 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002039 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002040
Damien Georgeb9791222014-01-23 00:34:21 +00002041 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002042 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002043 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002044}
2045
Damien George6be0b0a2014-08-15 14:30:52 +01002046STATIC 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 +01002047 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002048
Damien Georgeb9791222014-01-23 00:34:21 +00002049 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002050 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002051
Damien429d7192013-10-04 19:53:11 +01002052 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002053 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002054 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002055 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002056 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002057 } else {
2058 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2059 }
2060 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002061 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2062 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002063 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002064
Damien George8dcc0c72014-03-27 10:55:21 +00002065 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002066 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002067}
2068
Damien George969a6b32014-12-10 22:07:04 +00002069STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002070 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2071 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2072 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002073 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002074 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2075 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002076 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002077 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002078 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002079 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002080 // no finally
2081 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2082 } else {
2083 // have finally
Damiend99b0522013-12-21 18:17:45 +00002084 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 +01002085 }
2086 } else {
2087 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002088 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002089 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002090 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002091 }
2092 } else {
2093 // shouldn't happen
2094 assert(0);
2095 }
2096}
2097
Damien George6be0b0a2014-08-15 14:30:52 +01002098STATIC 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 +01002099 if (n == 0) {
2100 // no more pre-bits, compile the body of the with
2101 compile_node(comp, body);
2102 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002103 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002104 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002105 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002106 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002107 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002108 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002109 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2110 } else {
2111 // this pre-bit is just an expression
2112 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002113 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002114 EMIT(pop_top);
2115 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002116 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002117 // compile additional pre-bits and the body
2118 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2119 // finish this with block
2120 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002121 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2122 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002123 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002124 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002125 EMIT(end_finally);
2126 }
2127}
2128
Damien George969a6b32014-12-10 22:07:04 +00002129STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002130 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002131 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002132 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002133 assert(n > 0);
2134
2135 // compile in a nested fashion
2136 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2137}
2138
Damien George969a6b32014-12-10 22:07:04 +00002139STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002140 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002141 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2142 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002143 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002144 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002145 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002146 EMIT(pop_top);
2147
Damien429d7192013-10-04 19:53:11 +01002148 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002149 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002150 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002151 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002152 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2153 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002154 // do nothing with a lonely constant
2155 } else {
2156 compile_node(comp, pns->nodes[0]); // just an expression
2157 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2158 }
Damien429d7192013-10-04 19:53:11 +01002159 }
2160 } else {
Damiend99b0522013-12-21 18:17:45 +00002161 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2162 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002163 if (kind == PN_expr_stmt_augassign) {
2164 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2165 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002166 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002167 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002168 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002169 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2170 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2171 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2172 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2173 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2174 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2175 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2176 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2177 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2178 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2179 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002180 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002181 }
Damien George7e5fb242014-02-01 22:18:47 +00002182 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002183 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2184 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002185 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2186 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002187 // following CPython, we store left-most first
2188 if (rhs > 0) {
2189 EMIT(dup_top);
2190 }
2191 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2192 for (int i = 0; i < rhs; i++) {
2193 if (i + 1 < rhs) {
2194 EMIT(dup_top);
2195 }
Damiend99b0522013-12-21 18:17:45 +00002196 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002197 }
2198 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002199 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002200 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2201 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2202 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002203 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002204 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2205 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002206 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2207 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2208 // can't optimise when it's a star expression on the lhs
2209 goto no_optimisation;
2210 }
Damien429d7192013-10-04 19:53:11 +01002211 compile_node(comp, pns10->nodes[0]); // rhs
2212 compile_node(comp, pns10->nodes[1]); // rhs
2213 EMIT(rot_two);
2214 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2215 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002216 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2217 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2218 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2219 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002220 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002221 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2222 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002223 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2224 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2225 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2226 // can't optimise when it's a star expression on the lhs
2227 goto no_optimisation;
2228 }
Damien429d7192013-10-04 19:53:11 +01002229 compile_node(comp, pns10->nodes[0]); // rhs
2230 compile_node(comp, pns10->nodes[1]); // rhs
2231 compile_node(comp, pns10->nodes[2]); // rhs
2232 EMIT(rot_three);
2233 EMIT(rot_two);
2234 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2235 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2236 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2237 } else {
Damien George495d7812014-04-08 17:51:47 +01002238 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002239 compile_node(comp, pns1->nodes[0]); // rhs
2240 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2241 }
2242 } else {
2243 // shouldn't happen
2244 assert(0);
2245 }
2246 }
2247}
2248
Damien George6be0b0a2014-08-15 14:30:52 +01002249STATIC 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 +00002250 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002251 compile_node(comp, pns->nodes[0]);
2252 for (int i = 1; i < num_nodes; i += 1) {
2253 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002254 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002255 }
2256}
2257
Damien George969a6b32014-12-10 22:07:04 +00002258STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002259 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2260 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002261
Damien George6f355fd2014-04-10 14:11:31 +01002262 uint l_fail = comp_next_label(comp);
2263 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002264 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2265 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002266 EMIT_ARG(jump, l_end);
2267 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002268 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002269 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002270 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002271}
2272
Damien George969a6b32014-12-10 22:07:04 +00002273STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002274 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002275 //mp_parse_node_t pn_params = pns->nodes[0];
2276 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002277
Damien George36db6bc2014-05-07 17:24:22 +01002278 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002279 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002280 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 +01002281 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002282 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002283 }
2284
2285 // get the scope for this lambda
2286 scope_t *this_scope = (scope_t*)pns->nodes[2];
2287
2288 // make the lambda
2289 close_over_variables_etc(comp, this_scope, 0, 0);
2290}
2291
Damien George969a6b32014-12-10 22:07:04 +00002292STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002293 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002294 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002295 for (int i = 0; i < n; i += 1) {
2296 compile_node(comp, pns->nodes[i]);
2297 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002298 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002299 }
2300 }
Damien Georgeb9791222014-01-23 00:34:21 +00002301 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002302}
2303
Damien George969a6b32014-12-10 22:07:04 +00002304STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002305 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002306 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002307 for (int i = 0; i < n; i += 1) {
2308 compile_node(comp, pns->nodes[i]);
2309 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002310 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002311 }
2312 }
Damien Georgeb9791222014-01-23 00:34:21 +00002313 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002314}
2315
Damien George969a6b32014-12-10 22:07:04 +00002316STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002317 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002318 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002319}
2320
Damien George969a6b32014-12-10 22:07:04 +00002321STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002322 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002323 compile_node(comp, pns->nodes[0]);
2324 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002325 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002326 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002327 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002328 }
2329 for (int i = 1; i + 1 < num_nodes; i += 2) {
2330 compile_node(comp, pns->nodes[i + 1]);
2331 if (i + 2 < num_nodes) {
2332 EMIT(dup_top);
2333 EMIT(rot_three);
2334 }
Damien George7e5fb242014-02-01 22:18:47 +00002335 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002336 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002337 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002338 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2339 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2340 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2341 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2342 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2343 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002344 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002345 }
2346 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002347 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2348 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2349 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002350 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002351 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002352 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002353 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002354 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002355 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002356 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002357 }
2358 } else {
2359 // shouldn't happen
2360 assert(0);
2361 }
2362 } else {
2363 // shouldn't happen
2364 assert(0);
2365 }
2366 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002367 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002368 }
2369 }
2370 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002371 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002372 EMIT_ARG(jump, l_end);
2373 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002374 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002375 EMIT(rot_two);
2376 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002377 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002378 }
2379}
2380
Damien George969a6b32014-12-10 22:07:04 +00002381STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002382 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002383}
2384
Damien George969a6b32014-12-10 22:07:04 +00002385STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002386 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002387}
2388
Damien George969a6b32014-12-10 22:07:04 +00002389STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002390 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002391}
2392
Damien George969a6b32014-12-10 22:07:04 +00002393STATIC void compile_and_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_AND);
Damien429d7192013-10-04 19:53:11 +01002395}
2396
Damien George969a6b32014-12-10 22:07:04 +00002397STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002398 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002399 compile_node(comp, pns->nodes[0]);
2400 for (int i = 1; i + 1 < num_nodes; i += 2) {
2401 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002402 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002403 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002404 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002405 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002406 } else {
2407 // shouldn't happen
2408 assert(0);
2409 }
2410 }
2411}
2412
Damien George969a6b32014-12-10 22:07:04 +00002413STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002414 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002415 compile_node(comp, pns->nodes[0]);
2416 for (int i = 1; i + 1 < num_nodes; i += 2) {
2417 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002418 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002419 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002420 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002421 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002422 } else {
2423 // shouldn't happen
2424 assert(0);
2425 }
2426 }
2427}
2428
Damien George969a6b32014-12-10 22:07:04 +00002429STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002430 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002431 compile_node(comp, pns->nodes[0]);
2432 for (int i = 1; i + 1 < num_nodes; i += 2) {
2433 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002434 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002435 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002436 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002437 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002438 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002439 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002440 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002441 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002442 } else {
2443 // shouldn't happen
2444 assert(0);
2445 }
2446 }
2447}
2448
Damien George969a6b32014-12-10 22:07:04 +00002449STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002450 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002451 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002452 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002453 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002454 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002455 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002456 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002457 } else {
2458 // shouldn't happen
2459 assert(0);
2460 }
2461}
2462
Damien George969a6b32014-12-10 22:07:04 +00002463STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002464 // this is to handle special super() call
2465 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2466
2467 compile_generic_all_nodes(comp, pns);
2468}
2469
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002470STATIC 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 +01002471 // function to call is on top of stack
2472
Damien George35e2a4e2014-02-05 00:51:47 +00002473#if !MICROPY_EMIT_CPYTHON
2474 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002475 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 +00002476 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002477 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002478 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002479 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002480 // first argument found; load it and call super
Damien George0abb5602015-01-16 12:24:49 +00002481 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002482 EMIT_ARG(call_function, 2, 0, 0);
2483 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002484 }
2485 }
Damien George01039b52014-12-21 17:44:27 +00002486 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002487 return;
2488 }
2489#endif
2490
Damien George2c0842b2014-04-27 16:46:51 +01002491 // get the list of arguments
2492 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002493 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002494
Damien George2c0842b2014-04-27 16:46:51 +01002495 // compile the arguments
2496 // Rather than calling compile_node on the list, we go through the list of args
2497 // explicitly here so that we can count the number of arguments and give sensible
2498 // error messages.
2499 int n_positional = n_positional_extra;
2500 uint n_keyword = 0;
2501 uint star_flags = 0;
2502 for (int i = 0; i < n_args; i++) {
2503 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2504 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2505 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2506 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2507 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2508 return;
2509 }
2510 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2511 compile_node(comp, pns_arg->nodes[0]);
2512 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2513 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2514 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2515 return;
2516 }
2517 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2518 compile_node(comp, pns_arg->nodes[0]);
2519 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2520 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2521 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2522 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2523 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2524 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2525 return;
2526 }
Damien George968bf342014-04-27 19:12:05 +01002527 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002528 compile_node(comp, pns2->nodes[0]);
2529 n_keyword += 1;
2530 } else {
2531 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2532 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2533 n_positional++;
2534 }
2535 } else {
2536 goto normal_argument;
2537 }
2538 } else {
2539 normal_argument:
2540 if (n_keyword > 0) {
2541 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2542 return;
2543 }
2544 compile_node(comp, args[i]);
2545 n_positional++;
2546 }
Damien429d7192013-10-04 19:53:11 +01002547 }
2548
Damien George2c0842b2014-04-27 16:46:51 +01002549 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002550 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002551 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002552 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002553 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002554 }
Damien429d7192013-10-04 19:53:11 +01002555}
2556
Damien George969a6b32014-12-10 22:07:04 +00002557STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002558 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002559 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002560 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 +01002561 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002562 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2563 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002564 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002565 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002566 i += 1;
2567 } else {
2568 compile_node(comp, pns->nodes[i]);
2569 }
Damien George35e2a4e2014-02-05 00:51:47 +00002570 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002571 }
2572}
2573
Damien George969a6b32014-12-10 22:07:04 +00002574STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002575 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002576 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002577}
2578
Damien George969a6b32014-12-10 22:07:04 +00002579STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002580 // a list of strings
Damien63321742013-12-10 17:41:49 +00002581
2582 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002583 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002584 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002585 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002586 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002587 int pn_kind;
2588 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2589 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2590 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2591 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2592 } else {
2593 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2594 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002595 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2596 pn_kind = MP_PARSE_NODE_STRING;
2597 } else {
2598 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2599 pn_kind = MP_PARSE_NODE_BYTES;
2600 }
Damien George40f3c022014-07-03 13:25:24 +01002601 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002602 }
Damien63321742013-12-10 17:41:49 +00002603 if (i == 0) {
2604 string_kind = pn_kind;
2605 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002606 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002607 return;
2608 }
Damien429d7192013-10-04 19:53:11 +01002609 }
Damien63321742013-12-10 17:41:49 +00002610
Damien George65ef6b72015-01-14 21:17:27 +00002611 // if we are not in the last pass, just load a dummy object
2612 if (comp->pass != MP_PASS_EMIT) {
2613 EMIT_ARG(load_const_obj, mp_const_none);
2614 return;
2615 }
2616
Damien63321742013-12-10 17:41:49 +00002617 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002618 vstr_t vstr;
2619 vstr_init_len(&vstr, n_bytes);
2620 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002621 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002622 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002623 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002624 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2625 memcpy(s_dest, s, s_len);
2626 s_dest += s_len;
2627 } else {
2628 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002629 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2630 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002631 }
Damien63321742013-12-10 17:41:49 +00002632 }
Damien George65ef6b72015-01-14 21:17:27 +00002633
2634 // load the object
Damien George05005f62015-01-21 22:48:37 +00002635 EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr));
Damien429d7192013-10-04 19:53:11 +01002636}
2637
2638// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002639STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002640 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2641 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2642 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002643
Damien George36db6bc2014-05-07 17:24:22 +01002644 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002645 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002646 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 +01002647 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002648 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002649 }
2650
2651 // get the scope for this comprehension
2652 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2653
2654 // compile the comprehension
2655 close_over_variables_etc(comp, this_scope, 0, 0);
2656
2657 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2658 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002659 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002660}
2661
Damien George969a6b32014-12-10 22:07:04 +00002662STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002663 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002664 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002665 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2666 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2667 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2668 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2669 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2670 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2671 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002672 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002673 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002674 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002675 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002676 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002677 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002678 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002679 // generator expression
2680 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2681 } else {
2682 // tuple with 2 items
2683 goto tuple_with_2_items;
2684 }
2685 } else {
2686 // tuple with 2 items
2687 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002688 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002689 }
2690 } else {
2691 // parenthesis around a single item, is just that item
2692 compile_node(comp, pns->nodes[0]);
2693 }
2694}
2695
Damien George969a6b32014-12-10 22:07:04 +00002696STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002697 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002698 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002699 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002700 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2701 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2702 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2703 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2704 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002705 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002706 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002707 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002708 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002709 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002710 // list of many items
2711 compile_node(comp, pns2->nodes[0]);
2712 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002713 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002714 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002715 // list comprehension
2716 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2717 } else {
2718 // list with 2 items
2719 goto list_with_2_items;
2720 }
2721 } else {
2722 // list with 2 items
2723 list_with_2_items:
2724 compile_node(comp, pns2->nodes[0]);
2725 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002726 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002727 }
2728 } else {
2729 // list with 1 item
2730 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002731 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002732 }
2733}
2734
Damien George969a6b32014-12-10 22:07:04 +00002735STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002736 mp_parse_node_t pn = pns->nodes[0];
2737 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002738 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002739 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002740 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2741 pns = (mp_parse_node_struct_t*)pn;
2742 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002743 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002744 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002745 compile_node(comp, pn);
2746 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002747 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2748 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2749 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2750 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002751 // dict/set with multiple elements
2752
2753 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002754 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002755 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002756
2757 // first element sets whether it's a dict or set
2758 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002759 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002760 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002761 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002762 compile_node(comp, pns->nodes[0]);
2763 EMIT(store_map);
2764 is_dict = true;
2765 } else {
2766 // a set
2767 compile_node(comp, pns->nodes[0]); // 1st value of set
2768 is_dict = false;
2769 }
2770
2771 // process rest of elements
2772 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002773 mp_parse_node_t pn_i = nodes[i];
2774 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2775 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002776 if (is_dict) {
2777 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002778 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002779 return;
2780 }
2781 EMIT(store_map);
2782 } else {
2783 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002784 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002785 return;
2786 }
2787 }
2788 }
2789
Damien Georgee37dcaa2014-12-27 17:07:16 +00002790 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002791 // if it's a set, build it
2792 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002793 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002794 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002795 #endif
Damiend99b0522013-12-21 18:17:45 +00002796 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002797 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002798 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002799 // a dictionary comprehension
2800 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2801 } else {
2802 // a set comprehension
2803 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2804 }
2805 } else {
2806 // shouldn't happen
2807 assert(0);
2808 }
2809 } else {
2810 // set with one element
2811 goto set_with_one_element;
2812 }
2813 } else {
2814 // set with one element
2815 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002816 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002817 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002818 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002819 #else
2820 assert(0);
2821 #endif
Damien429d7192013-10-04 19:53:11 +01002822 }
2823}
2824
Damien George969a6b32014-12-10 22:07:04 +00002825STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002826 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002827}
2828
Damien George969a6b32014-12-10 22:07:04 +00002829STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002830 // object who's index we want is on top of stack
2831 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002832 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002833}
2834
Damien George969a6b32014-12-10 22:07:04 +00002835STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002836 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002837 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002838}
2839
Damien George83204f32014-12-27 17:20:41 +00002840#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002841STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002842 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2843 mp_parse_node_t pn = pns->nodes[0];
2844 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002845 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002846 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2847 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002848 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2849 pns = (mp_parse_node_struct_t*)pn;
2850 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002851 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002852 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002853 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002854 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002855 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002856 } else {
2857 // [?::x]
2858 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002859 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002860 }
Damiend99b0522013-12-21 18:17:45 +00002861 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002862 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002863 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2864 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2865 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2866 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002867 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002868 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002869 } else {
2870 // [?:x:x]
2871 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002872 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002873 }
2874 } else {
2875 // [?:x]
2876 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002877 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002878 }
2879 } else {
2880 // [?:x]
2881 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002882 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002883 }
2884}
2885
Damien George969a6b32014-12-10 22:07:04 +00002886STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002887 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002888 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2889 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002890}
2891
Damien George969a6b32014-12-10 22:07:04 +00002892STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002893 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002894 compile_subscript_3_helper(comp, pns);
2895}
Damien George83204f32014-12-27 17:20:41 +00002896#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002897
Damien George969a6b32014-12-10 22:07:04 +00002898STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002899 // if this is called then we are compiling a dict key:value pair
2900 compile_node(comp, pns->nodes[1]); // value
2901 compile_node(comp, pns->nodes[0]); // key
2902}
2903
Damien George969a6b32014-12-10 22:07:04 +00002904STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002905 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002906 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002907 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002908}
2909
Damien George969a6b32014-12-10 22:07:04 +00002910STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002911 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002912 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002913 return;
2914 }
Damiend99b0522013-12-21 18:17:45 +00002915 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002916 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002917 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002918 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2919 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002920 compile_node(comp, pns->nodes[0]);
2921 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002922 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002923 EMIT(yield_from);
2924 } else {
2925 compile_node(comp, pns->nodes[0]);
2926 EMIT(yield_value);
2927 }
2928}
2929
Damien George65ef6b72015-01-14 21:17:27 +00002930STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2931 // only create and load the actual str object on the last pass
2932 if (comp->pass != MP_PASS_EMIT) {
2933 EMIT_ARG(load_const_obj, mp_const_none);
2934 } else {
2935 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2936 }
2937}
2938
2939STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2940 // only create and load the actual bytes object on the last pass
2941 if (comp->pass != MP_PASS_EMIT) {
2942 EMIT_ARG(load_const_obj, mp_const_none);
2943 } else {
2944 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2945 }
2946}
2947
Damien George7d414a12015-02-08 01:57:40 +00002948STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2949 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2950}
2951
Damiend99b0522013-12-21 18:17:45 +00002952typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002953STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002954#define nc NULL
2955#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002956#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002957#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002958#undef nc
2959#undef c
2960#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002961 NULL,
2962 compile_string,
2963 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002964 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002965};
2966
Damien George6be0b0a2014-08-15 14:30:52 +01002967STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002968 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002969 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002970 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002971 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002972 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002973 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002974 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002975 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002976 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002977 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2978 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damien Georged2d64f02015-01-14 21:32:42 +00002979 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002980 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002981 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002982 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002983 // do nothing
2984 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002985 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002986 }
2987 break;
Damien429d7192013-10-04 19:53:11 +01002988 }
2989 } else {
Damiend99b0522013-12-21 18:17:45 +00002990 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002991 EMIT_ARG(set_line_number, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002992 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2993 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002994#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002995 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2996 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002997#endif
Damien George65ef6b72015-01-14 21:17:27 +00002998 compile_syntax_error(comp, pn, "internal compiler error");
2999 } else {
3000 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01003001 }
3002 }
3003}
3004
Damien George2ac4af62014-08-15 16:45:41 +01003005STATIC 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 +01003006 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01003007 qstr param_name = MP_QSTR_NULL;
3008 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003009 if (MP_PARSE_NODE_IS_ID(pn)) {
3010 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01003011 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003012 // comes after a star, so counts as a keyword-only parameter
3013 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01003014 } else {
Damien George2827d622014-04-27 15:50:52 +01003015 // comes before a star, so counts as a positional parameter
3016 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003017 }
Damienb14de212013-10-06 00:28:28 +01003018 } else {
Damiend99b0522013-12-21 18:17:45 +00003019 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3020 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3021 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3022 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003023 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003024 // comes after a star, so counts as a keyword-only parameter
3025 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003026 } else {
Damien George2827d622014-04-27 15:50:52 +01003027 // comes before a star, so counts as a positional parameter
3028 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003029 }
Damiend99b0522013-12-21 18:17:45 +00003030 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003031 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003032 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003033 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003034 // bare star
3035 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003036 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003037 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003038 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003039 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003040 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01003041 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01003042 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003043 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003044 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3045 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003046 } else {
3047 // shouldn't happen
3048 assert(0);
3049 }
Damiend99b0522013-12-21 18:17:45 +00003050 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
3051 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003052 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003053 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003054 } else {
Damienb14de212013-10-06 00:28:28 +01003055 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01003056 assert(0);
3057 }
Damien429d7192013-10-04 19:53:11 +01003058 }
3059
Damien George2827d622014-04-27 15:50:52 +01003060 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003061 bool added;
3062 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3063 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003064 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003065 return;
3066 }
Damien429d7192013-10-04 19:53:11 +01003067 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003068 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003069 }
3070}
3071
Damien George2827d622014-04-27 15:50:52 +01003072STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003073 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003074}
3075
Damien George2827d622014-04-27 15:50:52 +01003076STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003077 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3078}
3079
3080STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3081 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3082 // no annotation
3083 return;
3084 }
3085
3086 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3087 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3088 // named parameter with possible annotation
3089 // fallthrough
3090 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3091 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3092 // named star with possible annotation
3093 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3094 // fallthrough
3095 } else {
3096 // no annotation
3097 return;
3098 }
3099 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3100 // double star with possible annotation
3101 // fallthrough
3102 } else {
3103 // no annotation
3104 return;
3105 }
3106
3107 mp_parse_node_t pn_annotation = pns->nodes[1];
3108
3109 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3110 #if MICROPY_EMIT_NATIVE
3111 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3112 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3113 assert(id_info != NULL);
3114
3115 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3116 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3117 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3118 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3119 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003120 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003121 }
3122 }
3123 #endif // MICROPY_EMIT_NATIVE
3124 }
Damien429d7192013-10-04 19:53:11 +01003125}
3126
Damien George6be0b0a2014-08-15 14:30:52 +01003127STATIC 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 +01003128 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003129 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003130 // no more nested if/for; compile inner expression
3131 compile_node(comp, pn_inner_expr);
3132 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003133 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003134 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003135 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003136 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003137 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003138 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003139 #endif
Damien429d7192013-10-04 19:53:11 +01003140 } else {
3141 EMIT(yield_value);
3142 EMIT(pop_top);
3143 }
Damiend99b0522013-12-21 18:17:45 +00003144 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003145 // if condition
Damiend99b0522013-12-21 18:17:45 +00003146 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003147 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3148 pn_iter = pns_comp_if->nodes[1];
3149 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003150 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003151 // for loop
Damiend99b0522013-12-21 18:17:45 +00003152 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003153 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003154 uint l_end2 = comp_next_label(comp);
3155 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003156 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003157 EMIT_ARG(label_assign, l_top2);
3158 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003159 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3160 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 +00003161 EMIT_ARG(jump, l_top2);
3162 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003163 EMIT(for_iter_end);
3164 } else {
3165 // shouldn't happen
3166 assert(0);
3167 }
3168}
3169
Damien George1463c1f2014-04-25 23:52:57 +01003170STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3171#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003172 // see http://www.python.org/dev/peps/pep-0257/
3173
3174 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003175 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003176 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003177 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003178 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003179 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3180 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003181 for (int i = 0; i < num_nodes; i++) {
3182 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003183 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 +00003184 // not a newline, so this is the first statement; finish search
3185 break;
3186 }
3187 }
3188 // 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 +00003189 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003190 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003191 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003192 } else {
3193 return;
3194 }
3195
3196 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003197 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3198 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003199 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3200 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3201 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3202 // compile the doc string
3203 compile_node(comp, pns->nodes[0]);
3204 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003205 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003206 }
3207 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003208#else
3209 (void)comp;
3210 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003211#endif
Damien429d7192013-10-04 19:53:11 +01003212}
3213
Damien George1463c1f2014-04-25 23:52:57 +01003214STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003215 comp->pass = pass;
3216 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003217 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003218 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003219
Damien George36db6bc2014-05-07 17:24:22 +01003220 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003221 // reset maximum stack sizes in scope
3222 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003223 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003224 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003225 }
3226
Damien5ac1b2e2013-10-18 19:58:12 +01003227#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003228 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003229 scope_print_info(scope);
3230 }
Damien5ac1b2e2013-10-18 19:58:12 +01003231#endif
Damien429d7192013-10-04 19:53:11 +01003232
3233 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003234 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3235 assert(scope->kind == SCOPE_MODULE);
3236 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3237 compile_node(comp, pns->nodes[0]); // compile the expression
3238 EMIT(return_value);
3239 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003240 if (!comp->is_repl) {
3241 check_for_doc_string(comp, scope->pn);
3242 }
Damien429d7192013-10-04 19:53:11 +01003243 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003244 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003245 EMIT(return_value);
3246 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003247 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3248 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3249 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003250
3251 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003252 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003253 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003254 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003255 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003256 } else {
3257 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003258
Damien George2ac4af62014-08-15 16:45:41 +01003259 // argument annotations
3260 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3261
3262 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003263 mp_parse_node_t pn_annotation = pns->nodes[2];
3264 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3265 #if MICROPY_EMIT_NATIVE
3266 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3267 // nodes[2] can be null or a test-expr
3268 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3269 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3270 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3271 } else {
3272 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3273 }
Damien George2ac4af62014-08-15 16:45:41 +01003274 }
Damien Georgea5190a72014-08-15 22:39:08 +01003275 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003276 }
Damien George2ac4af62014-08-15 16:45:41 +01003277 }
Damien429d7192013-10-04 19:53:11 +01003278
3279 compile_node(comp, pns->nodes[3]); // 3 is function body
3280 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003281 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003282 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003283 EMIT(return_value);
3284 }
3285 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003286 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3287 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3288 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003289
3290 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003291 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003292 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003293 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003294 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3295 }
3296
3297 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003298
3299 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3300 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3301 EMIT(pop_top);
3302 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3303 }
Damien429d7192013-10-04 19:53:11 +01003304 EMIT(return_value);
3305 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3306 // a bit of a hack at the moment
3307
Damiend99b0522013-12-21 18:17:45 +00003308 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3309 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3310 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3311 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3312 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003313
Damien George708c0732014-04-27 19:23:46 +01003314 // We need a unique name for the comprehension argument (the iterator).
3315 // CPython uses .0, but we should be able to use anything that won't
3316 // clash with a user defined variable. Best to use an existing qstr,
3317 // so we use the blank qstr.
3318#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003319 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003320#else
3321 qstr qstr_arg = MP_QSTR_;
3322#endif
Damien George36db6bc2014-05-07 17:24:22 +01003323 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003324 bool added;
3325 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3326 assert(added);
3327 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003328 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003329 }
3330
3331 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003332 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003333 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003334 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003335 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003336 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003337 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003338 #endif
Damien429d7192013-10-04 19:53:11 +01003339 }
3340
Damien George6f355fd2014-04-10 14:11:31 +01003341 uint l_end = comp_next_label(comp);
3342 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003343 EMIT_ARG(load_id, qstr_arg);
3344 EMIT_ARG(label_assign, l_top);
3345 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003346 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3347 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003348 EMIT_ARG(jump, l_top);
3349 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003350 EMIT(for_iter_end);
3351
3352 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003353 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003354 }
3355 EMIT(return_value);
3356 } else {
3357 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003358 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3359 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3360 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003361
Damien George36db6bc2014-05-07 17:24:22 +01003362 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003363 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003364 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003365 assert(added);
3366 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003367 }
3368
Damien Georgeb9791222014-01-23 00:34:21 +00003369 EMIT_ARG(load_id, MP_QSTR___name__);
3370 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003371 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 +00003372 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003373
3374 check_for_doc_string(comp, pns->nodes[2]);
3375 compile_node(comp, pns->nodes[2]); // 2 is class body
3376
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003377 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003378 assert(id != NULL);
3379 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003380 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003381 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003382#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003383 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003384#else
Damien George0abb5602015-01-16 12:24:49 +00003385 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003386#endif
Damien429d7192013-10-04 19:53:11 +01003387 }
3388 EMIT(return_value);
3389 }
3390
Damien415eb6f2013-10-05 12:19:06 +01003391 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003392
3393 // make sure we match all the exception levels
3394 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003395}
3396
Damien George094d4502014-04-02 17:31:27 +01003397#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003398// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003399STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003400 comp->pass = pass;
3401 comp->scope_cur = scope;
3402 comp->next_label = 1;
3403
3404 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003405 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003406 return;
3407 }
3408
Damien George36db6bc2014-05-07 17:24:22 +01003409 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003410 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003411 }
3412
Damien826005c2013-10-05 23:17:28 +01003413 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003414 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3415 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3416 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003417
Damiend99b0522013-12-21 18:17:45 +00003418 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003419
Damiena2f2f7d2013-10-06 00:14:13 +01003420 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003421 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003422 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003423 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003424 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003425 if (comp->compile_error != MP_OBJ_NULL) {
3426 goto inline_asm_error;
3427 }
Damiena2f2f7d2013-10-06 00:14:13 +01003428 }
3429
Damiend99b0522013-12-21 18:17:45 +00003430 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003431
Damiend99b0522013-12-21 18:17:45 +00003432 mp_parse_node_t pn_body = pns->nodes[3]; // body
3433 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003434 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003435
Damien Georgecbd2f742014-01-19 11:48:48 +00003436 /*
Damien George36db6bc2014-05-07 17:24:22 +01003437 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003438 //printf("----\n");
3439 scope_print_info(scope);
3440 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003441 */
Damien826005c2013-10-05 23:17:28 +01003442
3443 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003444 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3445 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003446 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3447 // no instructions
3448 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003449 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003450 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003451 not_an_instruction:
Damien Georgea26dc502014-04-12 17:54:52 +01003452 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3453 return;
3454 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003455
3456 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003457 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003458 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3459 goto not_an_instruction;
3460 }
Damiend99b0522013-12-21 18:17:45 +00003461 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003462 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3463 goto not_an_instruction;
3464 }
3465 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3466 goto not_an_instruction;
3467 }
3468 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3469 goto not_an_instruction;
3470 }
Damiend99b0522013-12-21 18:17:45 +00003471 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003472
3473 // parse node looks like an instruction
3474 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003475 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3476 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3477 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003478 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003479
3480 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003481 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003482 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003483 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003484 return;
3485 }
Damien George6f355fd2014-04-10 14:11:31 +01003486 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003487 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003488 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003489 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003490 } else if (op == MP_QSTR_align) {
3491 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3492 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3493 return;
3494 }
Damien George36db6bc2014-05-07 17:24:22 +01003495 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003496 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3497 }
3498 } else if (op == MP_QSTR_data) {
3499 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3500 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3501 return;
3502 }
Damien George36db6bc2014-05-07 17:24:22 +01003503 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003504 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003505 for (uint j = 1; j < n_args; j++) {
3506 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003507 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3508 return;
3509 }
Damien George50912e72015-01-20 11:55:10 +00003510 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003511 }
3512 }
Damien826005c2013-10-05 23:17:28 +01003513 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003514 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003515 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003516 }
3517 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003518
3519 if (comp->compile_error != MP_OBJ_NULL) {
3520 pns = pns2; // this is the parse node that had the error
3521 goto inline_asm_error;
3522 }
Damien826005c2013-10-05 23:17:28 +01003523 }
3524
Damien George36db6bc2014-05-07 17:24:22 +01003525 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003526 EMIT_INLINE_ASM(end_pass);
3527 }
3528
3529 if (comp->compile_error != MP_OBJ_NULL) {
3530 // inline assembler had an error; add traceback to its exception
3531 inline_asm_error:
3532 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, (mp_uint_t)pns->source_line, comp->scope_cur->simple_name);
Damienb05d7072013-10-05 13:37:10 +01003533 }
Damien429d7192013-10-04 19:53:11 +01003534}
Damien George094d4502014-04-02 17:31:27 +01003535#endif
Damien429d7192013-10-04 19:53:11 +01003536
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003537STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003538#if !MICROPY_EMIT_CPYTHON
3539 // in Micro Python we put the *x parameter after all other parameters (except **y)
3540 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3541 id_info_t *id_param = NULL;
3542 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3543 id_info_t *id = &scope->id_info[i];
3544 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3545 if (id_param != NULL) {
3546 // swap star param with last param
3547 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3548 }
3549 break;
3550 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3551 id_param = id;
3552 }
3553 }
3554 }
3555#endif
3556
Damien429d7192013-10-04 19:53:11 +01003557 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003558 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003559 scope->num_locals = 0;
3560 for (int i = 0; i < scope->id_info_len; i++) {
3561 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003562 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003563 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3564 continue;
3565 }
3566 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3567 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3568 }
Damien George2827d622014-04-27 15:50:52 +01003569 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003570 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003571 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003572 }
3573 }
3574
3575 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003576#if MICROPY_EMIT_CPYTHON
3577 int num_cell = 0;
3578#endif
Damien9ecbcff2013-12-11 00:41:43 +00003579 for (int i = 0; i < scope->id_info_len; i++) {
3580 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003581#if MICROPY_EMIT_CPYTHON
3582 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003583 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003584 id->local_num = num_cell;
3585 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003586 }
Damien George6baf76e2013-12-30 22:32:17 +00003587#else
3588 // in Micro Python the cells come right after the fast locals
3589 // parameters are not counted here, since they remain at the start
3590 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003591 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003592 id->local_num = scope->num_locals;
3593 scope->num_locals += 1;
3594 }
3595#endif
Damien9ecbcff2013-12-11 00:41:43 +00003596 }
Damien9ecbcff2013-12-11 00:41:43 +00003597
3598 // compute the index of free vars (freevars[idx] in CPython)
3599 // make sure they are in the order of the parent scope
3600 if (scope->parent != NULL) {
3601 int num_free = 0;
3602 for (int i = 0; i < scope->parent->id_info_len; i++) {
3603 id_info_t *id = &scope->parent->id_info[i];
3604 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3605 for (int j = 0; j < scope->id_info_len; j++) {
3606 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003607 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003608 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003609#if MICROPY_EMIT_CPYTHON
3610 // in CPython the frees are numbered after the cells
3611 id2->local_num = num_cell + num_free;
3612#else
3613 // in Micro Python the frees come first, before the params
3614 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003615#endif
3616 num_free += 1;
3617 }
3618 }
3619 }
Damien429d7192013-10-04 19:53:11 +01003620 }
Damien George6baf76e2013-12-30 22:32:17 +00003621#if !MICROPY_EMIT_CPYTHON
3622 // in Micro Python shift all other locals after the free locals
3623 if (num_free > 0) {
3624 for (int i = 0; i < scope->id_info_len; i++) {
3625 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003626 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003627 id->local_num += num_free;
3628 }
3629 }
Damien George2827d622014-04-27 15:50:52 +01003630 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 +00003631 scope->num_locals += num_free;
3632 }
3633#endif
Damien429d7192013-10-04 19:53:11 +01003634 }
3635
Damien George8725f8f2014-02-15 19:33:11 +00003636 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003637
3638#if MICROPY_EMIT_CPYTHON
3639 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003640 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) {
3641 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003642 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003643 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003644 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 +00003645 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003646 }
3647 }
Damien George882b3632014-04-02 15:56:31 +01003648#endif
3649
Damien429d7192013-10-04 19:53:11 +01003650 int num_free = 0;
3651 for (int i = 0; i < scope->id_info_len; i++) {
3652 id_info_t *id = &scope->id_info[i];
3653 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3654 num_free += 1;
3655 }
3656 }
3657 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003658 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003659 }
3660}
3661
Damien George65cad122014-04-06 11:48:15 +01003662mp_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 +01003663 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003664 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003665 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003666 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003667
Damien826005c2013-10-05 23:17:28 +01003668 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003669 mp_map_t consts;
3670 mp_map_init(&consts, 0);
3671 pn = fold_constants(comp, pn, &consts);
3672 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003673
3674 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003675 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003676
Damien826005c2013-10-05 23:17:28 +01003677 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003678 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003679 comp->emit_method_table = &emit_pass1_method_table;
3680 comp->emit_inline_asm = NULL;
3681 comp->emit_inline_asm_method_table = NULL;
3682 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003683 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003684 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003685#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003686 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003687 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003688#endif
Damien826005c2013-10-05 23:17:28 +01003689 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003690 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003691 }
3692
3693 // update maximim number of labels needed
3694 if (comp->next_label > max_num_labels) {
3695 max_num_labels = comp->next_label;
3696 }
Damien429d7192013-10-04 19:53:11 +01003697 }
3698
Damien826005c2013-10-05 23:17:28 +01003699 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003700 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003701 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003702 }
3703
Damien826005c2013-10-05 23:17:28 +01003704 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003705 emit_pass1_free(comp->emit);
3706
Damien826005c2013-10-05 23:17:28 +01003707 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003708#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003709 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003710#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003711 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003712#endif
Damien3ef4abb2013-10-12 16:53:13 +01003713#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003714 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003715#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003716#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003717 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003718 if (false) {
3719 // dummy
3720
Damien3ef4abb2013-10-12 16:53:13 +01003721#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003722 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003723 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003724 if (emit_inline_thumb == NULL) {
3725 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3726 }
3727 comp->emit = NULL;
3728 comp->emit_method_table = NULL;
3729 comp->emit_inline_asm = emit_inline_thumb;
3730 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003731 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003732 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003733 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003734 }
Damienc025ebb2013-10-12 14:30:21 +01003735#endif
3736
Damien826005c2013-10-05 23:17:28 +01003737 } else {
Damienc025ebb2013-10-12 14:30:21 +01003738
3739 // choose the emit type
3740
Damien3ef4abb2013-10-12 16:53:13 +01003741#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003742 comp->emit = emit_cpython_new(max_num_labels);
3743 comp->emit_method_table = &emit_cpython_method_table;
3744#else
Damien826005c2013-10-05 23:17:28 +01003745 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003746
3747#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003748 case MP_EMIT_OPT_NATIVE_PYTHON:
3749 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003750#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003751 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003752 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003753 }
Damien13ed3a62013-10-08 09:05:10 +01003754 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003755#elif MICROPY_EMIT_X86
3756 if (emit_native == NULL) {
3757 emit_native = emit_native_x86_new(max_num_labels);
3758 }
3759 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003760#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003761 if (emit_native == NULL) {
3762 emit_native = emit_native_thumb_new(max_num_labels);
3763 }
3764 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003765#elif MICROPY_EMIT_ARM
3766 if (emit_native == NULL) {
3767 emit_native = emit_native_arm_new(max_num_labels);
3768 }
3769 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003770#endif
3771 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003772 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003773 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003774#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003775
Damien826005c2013-10-05 23:17:28 +01003776 default:
3777 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003778 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003779 }
3780 comp->emit = emit_bc;
3781 comp->emit_method_table = &emit_bc_method_table;
3782 break;
3783 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003784#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003785
Damien George1e1779e2015-01-14 00:20:28 +00003786 // need a pass to compute stack size
3787 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3788
Damien George36db6bc2014-05-07 17:24:22 +01003789 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003790 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003791 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3792 }
3793
3794 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003795 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003796 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003797 }
Damien6cdd3af2013-10-05 18:08:26 +01003798 }
Damien429d7192013-10-04 19:53:11 +01003799 }
3800
Damien George41d02b62014-01-24 22:42:28 +00003801 // free the emitters
3802#if !MICROPY_EMIT_CPYTHON
3803 if (emit_bc != NULL) {
3804 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003805 }
Damien George41d02b62014-01-24 22:42:28 +00003806#if MICROPY_EMIT_NATIVE
3807 if (emit_native != NULL) {
3808#if MICROPY_EMIT_X64
3809 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003810#elif MICROPY_EMIT_X86
3811 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003812#elif MICROPY_EMIT_THUMB
3813 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003814#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003815 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003816#endif
3817 }
3818#endif
3819#if MICROPY_EMIT_INLINE_THUMB
3820 if (emit_inline_thumb != NULL) {
3821 emit_inline_thumb_free(emit_inline_thumb);
3822 }
3823#endif
3824#endif // !MICROPY_EMIT_CPYTHON
3825
Damien George52b5d762014-09-23 15:31:56 +00003826 // free the parse tree
3827 mp_parse_node_free(pn);
3828
Damien George41d02b62014-01-24 22:42:28 +00003829 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003830 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003831 for (scope_t *s = module_scope; s;) {
3832 scope_t *next = s->next;
3833 scope_free(s);
3834 s = next;
3835 }
Damien5ac1b2e2013-10-18 19:58:12 +01003836
Damien George41d02b62014-01-24 22:42:28 +00003837 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003838 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003839 m_del_obj(compiler_t, comp);
3840
Damien Georgea91ac202014-10-05 19:01:34 +01003841 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003842 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003843 } else {
3844#if MICROPY_EMIT_CPYTHON
3845 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003846 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003847 return mp_const_true;
3848#else
3849 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003850 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003851#endif
3852 }
Damien429d7192013-10-04 19:53:11 +01003853}