blob: 554d74b30c3aed2d9e5413d5bac391841e479566 [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>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
36#include "py/smallint.h"
37#include "py/runtime.h"
38#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010039
40// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien Georgeb9791222014-01-23 00:34:21 +000052#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
53#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
54#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
55#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010056
Damien Georgea91ac202014-10-05 19:01:34 +010057// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010058typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000059 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010060
Damien George78035b92014-04-09 12:27:39 +010061 uint8_t is_repl;
62 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010063 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010064 uint8_t have_star;
65
66 // try to keep compiler clean from nlr
67 // this is set to an exception object if we have a compile error
68 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010069
Damien George6f355fd2014-04-10 14:11:31 +010070 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010071
Damien George69b89d22014-04-11 13:38:30 +000072 uint16_t num_dict_params;
73 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010074
Damien Georgea91ac202014-10-05 19:01:34 +010075 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
76 uint16_t continue_label;
77 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000078 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010079
Damien429d7192013-10-04 19:53:11 +010080 scope_t *scope_head;
81 scope_t *scope_cur;
82
Damien6cdd3af2013-10-05 18:08:26 +010083 emit_t *emit; // current emitter
84 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010085
Damien Georgea77ffe62015-03-14 12:59:31 +000086 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +010087 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
Damien Georgea77ffe62015-03-14 12:59:31 +000089 #endif
Damien429d7192013-10-04 19:53:11 +010090} compiler_t;
91
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010092STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010093 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
94 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010095 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George8dfbd2d2015-02-13 01:00:51 +000096 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 +010097 } else {
Damien Georgea91ac202014-10-05 19:01:34 +010098 // we don't have a line number, so just pass 0
Damien George8dfbd2d2015-02-13 01:00:51 +000099 mp_obj_exception_add_traceback(exc, comp->source_file, 0, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100100 }
Damien Georgea91ac202014-10-05 19:01:34 +0100101 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000102}
103
Damien Georgeddd1e182015-01-10 14:07:24 +0000104#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100105STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300106 #if MICROPY_PY_UCTYPES
107 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
108 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100109 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100110 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100111};
Damien Georgeddd1e182015-01-10 14:07:24 +0000112STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
113#endif
Damien George57e99eb2014-04-10 22:42:11 +0100114
Damien Georgeffae48d2014-05-08 15:58:39 +0000115// this function is essentially a simple preprocessor
116STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
117 if (0) {
118 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100119#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000120 } else if (MP_PARSE_NODE_IS_ID(pn)) {
121 // lookup identifier in table of dynamic constants
122 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
123 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
124 if (elem != NULL) {
125 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
126 }
127#endif
128 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000129 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100130
Damien Georgeffae48d2014-05-08 15:58:39 +0000131 // fold some parse nodes before folding their arguments
132 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100133#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000134 case PN_expr_stmt:
135 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
136 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
137 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
138 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
139 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
140 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
141 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
142 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
143 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
144 ) {
145 // code to assign dynamic constants: id = const(value)
146
147 // get the id
148 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
149
150 // get the value
151 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
152 pn_value = fold_constants(comp, pn_value, consts);
153 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
154 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
155 break;
156 }
Damien George40f3c022014-07-03 13:25:24 +0100157 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000158
159 // store the value in the table of dynamic constants
160 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
161 if (elem->value != MP_OBJ_NULL) {
162 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
163 break;
164 }
165 elem->value = MP_OBJ_NEW_SMALL_INT(value);
166
167 // replace const(value) with value
168 pns1->nodes[0] = pn_value;
169
170 // finished folding this assignment
171 return pn;
172 }
173 }
174 }
175 break;
176#endif
Damien George5042bce2014-05-25 22:06:06 +0100177 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000178 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000179 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100180 return pn;
Damien429d7192013-10-04 19:53:11 +0100181 }
182
Damien Georgeffae48d2014-05-08 15:58:39 +0000183 // fold arguments
184 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
185 for (int i = 0; i < n; i++) {
186 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
187 }
188
189 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000190 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100191 case PN_atom_paren:
192 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
193 // (int)
194 pn = pns->nodes[0];
195 }
196 break;
197
198 case PN_expr:
199 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
200 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100201 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
202 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100203 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
204 }
205 break;
206
207 case PN_and_expr:
208 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
209 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100210 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
211 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100212 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
213 }
214 break;
215
Damien429d7192013-10-04 19:53:11 +0100216 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000217 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100218 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
219 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000220 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100221 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000222 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 +0100223 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
224 }
Damien George0bb97132015-02-27 14:25:47 +0000225 } else {
226 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100227 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000228 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200229 // Shifting to big amounts is underfined behavior
230 // in C and is CPU-dependent; propagate sign bit.
231 arg1 = BITS_PER_WORD - 1;
232 }
Damiend99b0522013-12-21 18:17:45 +0000233 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100234 }
235 }
236 break;
237
238 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100239 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000240 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 +0100241 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
242 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000243 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100244 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000245 arg0 += arg1;
Damien George0bb97132015-02-27 14:25:47 +0000246 } else {
247 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100248 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000249 arg0 -= arg1;
Damien0efb3a12013-10-12 16:16:56 +0100250 }
Damien Georged1e355e2014-05-28 14:51:12 +0100251 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100252 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000253 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100254 }
255 }
256 break;
257
258 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000259 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 +0100260 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
261 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000262 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100263 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000264 if (!mp_small_int_mul_overflow(arg0, arg1)) {
265 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100266 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000267 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
268 }
269 }
Damiend99b0522013-12-21 18:17:45 +0000270 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100271 // int / int
272 // pass
Damiend99b0522013-12-21 18:17:45 +0000273 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100274 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000275 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damien George0bb97132015-02-27 14:25:47 +0000276 } else {
277 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300278 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100279 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000280 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300281 }
Damien429d7192013-10-04 19:53:11 +0100282 }
283 }
284 break;
285
286 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000287 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100288 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000289 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100290 // +int
Damiend99b0522013-12-21 18:17:45 +0000291 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
292 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100293 // -int
Damiend99b0522013-12-21 18:17:45 +0000294 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
Damien George0bb97132015-02-27 14:25:47 +0000295 } else {
296 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100297 // ~int
Damiend99b0522013-12-21 18:17:45 +0000298 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100299 }
300 }
301 break;
302
303 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100304 if (0) {
305#if MICROPY_EMIT_CPYTHON
306 } 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 +0100307 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100308 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000309 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
310 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200311 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100312 if (power >= 0) {
313 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200314 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100315 for (; power > 0; power--) {
316 ans *= base;
317 }
Damiend99b0522013-12-21 18:17:45 +0000318 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100319 }
320 }
Damien George57e99eb2014-04-10 22:42:11 +0100321#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000322#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100323 } 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])) {
324 // id.id
325 // look it up in constant table, see if it can be replaced with an integer
326 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
327 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
328 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
329 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
330 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
331 if (elem != NULL) {
332 mp_obj_t dest[2];
333 mp_load_method_maybe(elem->value, q_attr, dest);
334 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100335 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000336 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100337 }
338 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000339#endif
Damien429d7192013-10-04 19:53:11 +0100340 }
341 break;
342 }
343 }
344
345 return pn;
346}
347
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200348STATIC 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 +0100349STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000350STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100351
Damien George6f355fd2014-04-10 14:11:31 +0100352STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100353 return comp->next_label++;
354}
355
Damien George8dcc0c72014-03-27 10:55:21 +0000356STATIC void compile_increase_except_level(compiler_t *comp) {
357 comp->cur_except_level += 1;
358 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
359 comp->scope_cur->exc_stack_size = comp->cur_except_level;
360 }
361}
362
363STATIC void compile_decrease_except_level(compiler_t *comp) {
364 assert(comp->cur_except_level > 0);
365 comp->cur_except_level -= 1;
366}
367
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200368STATIC 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 +0100369 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100370 scope->parent = comp->scope_cur;
371 scope->next = NULL;
372 if (comp->scope_head == NULL) {
373 comp->scope_head = scope;
374 } else {
375 scope_t *s = comp->scope_head;
376 while (s->next != NULL) {
377 s = s->next;
378 }
379 s->next = scope;
380 }
381 return scope;
382}
383
Damien George963a5a32015-01-16 17:47:07 +0000384STATIC 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)) {
385 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000386 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
387 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100388 for (int i = 0; i < num_nodes; i++) {
389 f(comp, pns->nodes[i]);
390 }
Damiend99b0522013-12-21 18:17:45 +0000391 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100392 f(comp, pn);
393 }
394}
395
Damien George969a6b32014-12-10 22:07:04 +0000396STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000397 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100398 for (int i = 0; i < num_nodes; i++) {
399 compile_node(comp, pns->nodes[i]);
400 }
401}
402
Damien3ef4abb2013-10-12 16:53:13 +0100403#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200404STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100405 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
406 return true;
407 }
Damien George4c81ba82015-01-13 16:21:23 +0000408 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
409 return true;
410 }
Damien George7d414a12015-02-08 01:57:40 +0000411 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
412 return true;
413 }
Damiend99b0522013-12-21 18:17:45 +0000414 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100415 return false;
416 }
Damiend99b0522013-12-21 18:17:45 +0000417 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100418 return false;
419 }
420 return true;
421}
422
Damien George5042bce2014-05-25 22:06:06 +0100423STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000424 bool has_single_quote = false;
425 bool has_double_quote = false;
426 for (int i = 0; i < len; i++) {
427 if (str[i] == '\'') {
428 has_single_quote = true;
429 } else if (str[i] == '"') {
430 has_double_quote = true;
431 }
432 }
433 if (bytes) {
434 vstr_printf(vstr, "b");
435 }
436 bool quote_single = false;
437 if (has_single_quote && !has_double_quote) {
438 vstr_printf(vstr, "\"");
439 } else {
440 quote_single = true;
441 vstr_printf(vstr, "'");
442 }
443 for (int i = 0; i < len; i++) {
444 if (str[i] == '\n') {
445 vstr_printf(vstr, "\\n");
446 } else if (str[i] == '\\') {
447 vstr_printf(vstr, "\\\\");
448 } else if (str[i] == '\'' && quote_single) {
449 vstr_printf(vstr, "\\'");
450 } else {
451 vstr_printf(vstr, "%c", str[i]);
452 }
453 }
454 if (has_single_quote && !has_double_quote) {
455 vstr_printf(vstr, "\"");
456 } else {
457 vstr_printf(vstr, "'");
458 }
459}
460
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200461STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000462 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 +0100463 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000464 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 +0100465 return;
466 }
467
Damien George7d414a12015-02-08 01:57:40 +0000468 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
469 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
470 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
471 return;
472 }
473
Damiend99b0522013-12-21 18:17:45 +0000474 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200475 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
476 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
477 return;
478 }
479
Damien George42f3de92014-10-03 17:44:14 +0000480 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000481 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
482 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100483 case MP_PARSE_NODE_STRING:
484 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100485 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100486 const byte *str = qstr_data(arg, &len);
487 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
488 break;
489 }
Damiend99b0522013-12-21 18:17:45 +0000490 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100491 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000492 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
493 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
494 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100495 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100496 }
497 break;
498 default: assert(0);
499 }
500}
501
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200502STATIC 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 +0100503 int n = 0;
504 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000505 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100506 }
507 int total = n;
508 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000509 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100510 total += 1;
Damien3a205172013-10-12 15:01:56 +0100511 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100512 is_const = false;
513 }
514 }
515 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100516 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100517 is_const = false;
518 break;
519 }
520 }
521 if (total > 0 && is_const) {
522 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000523 vstr_t *vstr = vstr_new();
524 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000525 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000526 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100527 need_comma = true;
528 }
529 for (int i = 0; i < n; i++) {
530 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000531 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100532 }
Damien02f89412013-12-12 15:13:36 +0000533 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100534 need_comma = true;
535 }
536 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000537 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100538 } else {
Damien02f89412013-12-12 15:13:36 +0000539 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100540 }
Damien George0d3cb672015-01-28 23:43:01 +0000541 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000542 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100543 } else {
Damiend99b0522013-12-21 18:17:45 +0000544 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100545 compile_node(comp, pn);
546 }
547 for (int i = 0; i < n; i++) {
548 compile_node(comp, pns_list->nodes[i]);
549 }
Damien Georgeb9791222014-01-23 00:34:21 +0000550 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100551 }
552}
Damien3a205172013-10-12 15:01:56 +0100553#endif
554
555// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100556STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100557#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100558 cpython_c_tuple(comp, pn, pns_list);
559#else
560 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000561 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100562 compile_node(comp, pn);
563 total += 1;
564 }
565 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000566 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100567 for (int i = 0; i < n; i++) {
568 compile_node(comp, pns_list->nodes[i]);
569 }
570 total += n;
571 }
Damien Georgeb9791222014-01-23 00:34:21 +0000572 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100573#endif
574}
Damien429d7192013-10-04 19:53:11 +0100575
Damien George969a6b32014-12-10 22:07:04 +0000576STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100577 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000578 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100579}
580
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200581STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000582 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
583 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100584}
585
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200586STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000587 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
588 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100589}
590
Damien3ef4abb2013-10-12 16:53:13 +0100591#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100592// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200593STATIC 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 +0100594 if (node_is_const_false(pn)) {
595 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000596 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100597 }
598 return;
599 } else if (node_is_const_true(pn)) {
600 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000601 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100602 }
603 return;
Damiend99b0522013-12-21 18:17:45 +0000604 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
605 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
606 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
607 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100608 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100609 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100610 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100611 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100612 }
Damien3a205172013-10-12 15:01:56 +0100613 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000614 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100615 } else {
616 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100617 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100618 }
619 }
620 return;
Damiend99b0522013-12-21 18:17:45 +0000621 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100622 if (jump_if == false) {
623 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100624 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100625 }
626 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100627 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100628 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100629 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100630 }
Damien3a205172013-10-12 15:01:56 +0100631 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000632 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100633 }
634 return;
Damiend99b0522013-12-21 18:17:45 +0000635 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100636 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100637 return;
638 }
639 }
640
641 // nothing special, fall back to default compiling for node and jump
642 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000643 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100644}
Damien3a205172013-10-12 15:01:56 +0100645#endif
Damien429d7192013-10-04 19:53:11 +0100646
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200647STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100648#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100649 cpython_c_if_cond(comp, pn, jump_if, label, false);
650#else
651 if (node_is_const_false(pn)) {
652 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000653 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100654 }
655 return;
656 } else if (node_is_const_true(pn)) {
657 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000658 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100659 }
660 return;
Damiend99b0522013-12-21 18:17:45 +0000661 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
662 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
663 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
664 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100665 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000666 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100667 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100668 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000669 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100670 }
Damien George0b2fd912015-02-28 14:37:54 +0000671 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000672 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100673 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000674 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100675 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000676 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100677 }
678 }
679 return;
Damiend99b0522013-12-21 18:17:45 +0000680 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100681 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000682 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100683 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000684 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100685 }
Damiend99b0522013-12-21 18:17:45 +0000686 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100687 c_if_cond(comp, pns->nodes[0], !jump_if, label);
688 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100689 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
690 // cond is something in parenthesis
691 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
692 // empty tuple, acts as false for the condition
693 if (jump_if == false) {
694 EMIT_ARG(jump, label);
695 }
696 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
697 // non-empty tuple, acts as true for the condition
698 if (jump_if == true) {
699 EMIT_ARG(jump, label);
700 }
701 } else {
702 // parenthesis around 1 item, is just that item
703 c_if_cond(comp, pns->nodes[0], jump_if, label);
704 }
705 return;
Damien3a205172013-10-12 15:01:56 +0100706 }
707 }
708
709 // nothing special, fall back to default compiling for node and jump
710 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000711 EMIT_ARG(pop_jump_if, jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100712#endif
Damien429d7192013-10-04 19:53:11 +0100713}
714
715typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100716STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100717
Damien George6be0b0a2014-08-15 14:30:52 +0100718STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100719 if (assign_kind != ASSIGN_AUG_STORE) {
720 compile_node(comp, pns->nodes[0]);
721 }
722
Damiend99b0522013-12-21 18:17:45 +0000723 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
724 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
725 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
726 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100727 if (assign_kind != ASSIGN_AUG_STORE) {
728 for (int i = 0; i < n - 1; i++) {
729 compile_node(comp, pns1->nodes[i]);
730 }
731 }
Damiend99b0522013-12-21 18:17:45 +0000732 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
733 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100734 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000735 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100736 if (assign_kind == ASSIGN_AUG_STORE) {
737 EMIT(rot_three);
738 EMIT(store_subscr);
739 } else {
740 compile_node(comp, pns1->nodes[0]);
741 if (assign_kind == ASSIGN_AUG_LOAD) {
742 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100743 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100744 } else {
745 EMIT(store_subscr);
746 }
747 }
Damiend99b0522013-12-21 18:17:45 +0000748 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
749 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100750 if (assign_kind == ASSIGN_AUG_LOAD) {
751 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000752 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100753 } else {
754 if (assign_kind == ASSIGN_AUG_STORE) {
755 EMIT(rot_two);
756 }
Damien Georgeb9791222014-01-23 00:34:21 +0000757 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100758 }
759 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100760 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100761 }
762 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100763 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100764 }
765
Damiend99b0522013-12-21 18:17:45 +0000766 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100767 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100768 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100769
770 return;
771
772cannot_assign:
773 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100774}
775
Damien George0288cf02014-04-11 11:53:00 +0000776// 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 +0100777STATIC 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 +0000778 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
779
780 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000781 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000782 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
783 EMIT_ARG(unpack_ex, 0, num_tail);
784 have_star_index = 0;
785 }
Damien George963a5a32015-01-16 17:47:07 +0000786 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000787 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000788 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000789 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
790 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100791 } else {
Damien George9d181f62014-04-27 16:55:27 +0100792 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100793 return;
794 }
795 }
796 }
Damien George963a5a32015-01-16 17:47:07 +0000797 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000798 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100799 }
Damien George0288cf02014-04-11 11:53:00 +0000800 if (num_head != 0) {
801 if (0 == have_star_index) {
802 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100803 } else {
Damien George0288cf02014-04-11 11:53:00 +0000804 c_assign(comp, node_head, ASSIGN_STORE);
805 }
806 }
Damien George963a5a32015-01-16 17:47:07 +0000807 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000808 if (num_head + i == have_star_index) {
809 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
810 } else {
811 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100812 }
813 }
814}
815
816// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100817STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100818 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000819 assert(!MP_PARSE_NODE_IS_NULL(pn));
820 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000821 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000822 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100823 switch (assign_kind) {
824 case ASSIGN_STORE:
825 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000826 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100827 break;
828 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000829 default:
Damien Georgeb9791222014-01-23 00:34:21 +0000830 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100831 break;
832 }
833 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100834 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100835 return;
836 }
837 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000838 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000839 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
840 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100841 case PN_power:
842 // lhs is an index or attribute
843 c_assign_power(comp, pns, assign_kind);
844 break;
845
846 case PN_testlist_star_expr:
847 case PN_exprlist:
848 // lhs is a tuple
849 if (assign_kind != ASSIGN_STORE) {
850 goto bad_aug;
851 }
Damien George0288cf02014-04-11 11:53:00 +0000852 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100853 break;
854
855 case PN_atom_paren:
856 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000857 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100858 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100859 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000860 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000861 if (assign_kind != ASSIGN_STORE) {
862 goto bad_aug;
863 }
Damiend99b0522013-12-21 18:17:45 +0000864 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100865 goto testlist_comp;
866 } else {
867 // parenthesis around 1 item, is just that item
868 pn = pns->nodes[0];
869 goto tail_recursion;
870 }
871 break;
872
873 case PN_atom_bracket:
874 // lhs is something in brackets
875 if (assign_kind != ASSIGN_STORE) {
876 goto bad_aug;
877 }
Damiend99b0522013-12-21 18:17:45 +0000878 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100879 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000880 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000881 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
882 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100883 goto testlist_comp;
884 } else {
885 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000886 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100887 }
888 break;
889
890 default:
Damien George9d181f62014-04-27 16:55:27 +0100891 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100892 }
893 return;
894
895 testlist_comp:
896 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000897 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
898 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
899 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100900 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000901 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000902 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000903 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100904 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000905 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
906 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000907 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100908 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100909 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100910 } else {
911 // sequence with 2 items
912 goto sequence_with_2_items;
913 }
914 } else {
915 // sequence with 2 items
916 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000917 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100918 }
919 return;
920 }
921 return;
922
Damien George9d181f62014-04-27 16:55:27 +0100923 cannot_assign:
924 compile_syntax_error(comp, pn, "can't assign to expression");
925 return;
926
Damien429d7192013-10-04 19:53:11 +0100927 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100928 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100929}
930
931// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100932// if we are not in CPython compatibility mode then:
933// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
934// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
935// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100936STATIC 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 +0100937 assert(n_pos_defaults >= 0);
938 assert(n_kw_defaults >= 0);
939
Damien429d7192013-10-04 19:53:11 +0100940 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000941 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100942 int nfree = 0;
943 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000944 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
945 id_info_t *id = &comp->scope_cur->id_info[i];
946 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
947 for (int j = 0; j < this_scope->id_info_len; j++) {
948 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100949 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000950#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100951 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000952#else
953 // in Micro Python we load closures using LOAD_FAST
Damien George0abb5602015-01-16 12:24:49 +0000954 EMIT_ARG(load_fast, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000955#endif
Damien318aec62013-12-10 18:28:17 +0000956 nfree += 1;
957 }
958 }
Damien429d7192013-10-04 19:53:11 +0100959 }
960 }
961 }
Damien429d7192013-10-04 19:53:11 +0100962
963 // make the function/closure
964 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100965 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100966 } else {
Damien George3558f622014-04-20 17:50:40 +0100967 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100968 }
969}
970
Damien George6be0b0a2014-08-15 14:30:52 +0100971STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000972 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100973 comp->have_star = true;
974 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000975 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
976 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100977 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100978 } else {
979 // named star
Damien429d7192013-10-04 19:53:11 +0100980 }
Damien George8b19db02014-04-11 23:25:34 +0100981 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000982
983 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100984 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000985 // TODO do we need to do anything with this?
986
987 } else {
988 mp_parse_node_t pn_id;
989 mp_parse_node_t pn_colon;
990 mp_parse_node_t pn_equal;
991 if (MP_PARSE_NODE_IS_ID(pn)) {
992 // this parameter is just an id
993
994 pn_id = pn;
995 pn_colon = MP_PARSE_NODE_NULL;
996 pn_equal = MP_PARSE_NODE_NULL;
997
Damien George0bb97132015-02-27 14:25:47 +0000998 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000999 // this parameter has a colon and/or equal specifier
1000
Damien George0bb97132015-02-27 14:25:47 +00001001 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1002
Damien Georgef41fdd02014-03-03 23:19:11 +00001003 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1004 pn_id = pns->nodes[0];
1005 pn_colon = pns->nodes[1];
1006 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +00001007 }
1008
1009 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1010 // this parameter does not have a default value
1011
1012 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001013 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001014 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001015 return;
1016 }
1017
1018 } else {
1019 // this parameter has a default value
1020 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1021
Damien George8b19db02014-04-11 23:25:34 +01001022 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001023 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001024#if MICROPY_EMIT_CPYTHON
1025 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1026 compile_node(comp, pn_equal);
1027#else
Damien George69b89d22014-04-11 13:38:30 +00001028 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1029 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001030 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1031 // we need to do this here before we start building the map for the default keywords
1032 if (comp->num_default_params > 0) {
1033 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001034 } else {
1035 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001036 }
Damien George69b89d22014-04-11 13:38:30 +00001037 // first default dict param, so make the map
1038 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001039 }
Damien Georgef0778a72014-06-07 22:01:00 +01001040
1041 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001042 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001043 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001044 EMIT(store_map);
1045#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001046 } else {
Damien George69b89d22014-04-11 13:38:30 +00001047 comp->num_default_params += 1;
1048 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001049 }
1050 }
1051
1052 // TODO pn_colon not implemented
1053 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001054 }
1055}
1056
1057// leaves function object on stack
1058// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001059STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001060 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001061 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001062 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001063 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001064 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001065 }
1066
1067 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001068 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001069 uint old_num_dict_params = comp->num_dict_params;
1070 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001071
1072 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001073 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001074 comp->num_dict_params = 0;
1075 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001076 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001077
Damien Georgea91ac202014-10-05 19:01:34 +01001078 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001079 return MP_QSTR_NULL;
1080 }
1081
Damien Georgee337f1e2014-03-31 15:18:37 +01001082#if !MICROPY_EMIT_CPYTHON
1083 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001084 // the default keywords args may have already made the tuple; if not, do it now
1085 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001086 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001087 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001088 }
1089#endif
1090
Damien429d7192013-10-04 19:53:11 +01001091 // get the scope for this function
1092 scope_t *fscope = (scope_t*)pns->nodes[4];
1093
1094 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001095 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001096
1097 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001098 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001099 comp->num_dict_params = old_num_dict_params;
1100 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001101
1102 // return its name (the 'f' in "def f(...):")
1103 return fscope->simple_name;
1104}
1105
1106// leaves class object on stack
1107// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001108STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001109 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001110 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001111 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001112 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001113 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001114 }
1115
1116 EMIT(load_build_class);
1117
1118 // scope for this class
1119 scope_t *cscope = (scope_t*)pns->nodes[3];
1120
1121 // compile the class
1122 close_over_variables_etc(comp, cscope, 0, 0);
1123
1124 // get its name
Damien George968bf342014-04-27 19:12:05 +01001125 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001126
1127 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001128 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1129 mp_parse_node_t parents = pns->nodes[1];
1130 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1131 parents = MP_PARSE_NODE_NULL;
1132 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001133 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001134 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001135
1136 // return its name (the 'C' in class C(...):")
1137 return cscope->simple_name;
1138}
1139
Damien6cdd3af2013-10-05 18:08:26 +01001140// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001141STATIC 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 +00001142 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001143 return false;
1144 }
1145
1146 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001147 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001148 return true;
1149 }
1150
Damiend99b0522013-12-21 18:17:45 +00001151 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001152 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001153 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001154#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001155 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001156 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001157 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001158 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001159#endif
Damien3ef4abb2013-10-12 16:53:13 +01001160#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001161 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001162 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001163#endif
Damien6cdd3af2013-10-05 18:08:26 +01001164 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001165 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001166 }
1167
1168 return true;
1169}
1170
Damien George969a6b32014-12-10 22:07:04 +00001171STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001172 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001173 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001174 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001175
Damien6cdd3af2013-10-05 18:08:26 +01001176 // inherit emit options for this function/class definition
1177 uint emit_options = comp->scope_cur->emit_options;
1178
1179 // compile each decorator
1180 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001181 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001182 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1183 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001184
1185 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001186 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001187 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001188
1189 // check for built-in decorators
1190 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1191 // this was a built-in
1192 num_built_in_decorators += 1;
1193
1194 } else {
1195 // not a built-in, compile normally
1196
1197 // compile the decorator function
1198 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001199 for (int j = 1; j < name_len; j++) {
1200 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1201 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001202 }
1203
1204 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001205 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001206 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001207 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001208 compile_node(comp, pns_decorator->nodes[1]);
1209 }
Damien429d7192013-10-04 19:53:11 +01001210 }
1211 }
1212
1213 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001214 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001215 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001216 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001217 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001218 } else {
Damien George0bb97132015-02-27 14:25:47 +00001219 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1220 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001221 }
1222
1223 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001224 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001225 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001226 }
1227
1228 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001229 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001230}
1231
Damien George969a6b32014-12-10 22:07:04 +00001232STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001233 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001234 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001235 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001236}
1237
Damien George6be0b0a2014-08-15 14:30:52 +01001238STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001239 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001240 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001241 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1242 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001243
1244 compile_node(comp, pns->nodes[0]); // base of the power node
1245
Damiend99b0522013-12-21 18:17:45 +00001246 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1247 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1248 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1249 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001250 for (int i = 0; i < n - 1; i++) {
1251 compile_node(comp, pns1->nodes[i]);
1252 }
Damiend99b0522013-12-21 18:17:45 +00001253 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1254 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001255 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001256 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001257 compile_node(comp, pns1->nodes[0]);
1258 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001259 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1260 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001261 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001262 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001263 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001264 }
1265 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001266 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001267 }
1268
Damiend99b0522013-12-21 18:17:45 +00001269 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001270 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001271 }
Damiend99b0522013-12-21 18:17:45 +00001272 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1273 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1274 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1275 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001276 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1277
Damiend99b0522013-12-21 18:17:45 +00001278 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1279 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1280 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001281 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001282 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001283 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001284 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001285 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001286 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001287 c_del_stmt(comp, pns->nodes[0]);
1288 for (int i = 0; i < n; i++) {
1289 c_del_stmt(comp, pns1->nodes[i]);
1290 }
Damiend99b0522013-12-21 18:17:45 +00001291 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001292 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001293 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001294 } else {
1295 // sequence with 2 items
1296 goto sequence_with_2_items;
1297 }
1298 } else {
1299 // sequence with 2 items
1300 sequence_with_2_items:
1301 c_del_stmt(comp, pns->nodes[0]);
1302 c_del_stmt(comp, pns->nodes[1]);
1303 }
1304 } else {
1305 // tuple with 1 element
1306 c_del_stmt(comp, pn);
1307 }
1308 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001309 // TODO is there anything else to implement?
1310 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001311 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001312
1313 return;
1314
1315cannot_delete:
1316 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001317}
1318
Damien George969a6b32014-12-10 22:07:04 +00001319STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001320 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1321}
1322
Damien George969a6b32014-12-10 22:07:04 +00001323STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001324 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001325 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001326 }
Damien George090c9232014-10-17 14:08:49 +00001327 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001328 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001329}
1330
Damien George969a6b32014-12-10 22:07:04 +00001331STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001332 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001333 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001334 }
Damien George090c9232014-10-17 14:08:49 +00001335 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001336 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001337}
1338
Damien George969a6b32014-12-10 22:07:04 +00001339STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001340 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001341 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001342 return;
1343 }
Damiend99b0522013-12-21 18:17:45 +00001344 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001345 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001346 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001347 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001348 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001349 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1350 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 +01001351
Damien George6f355fd2014-04-10 14:11:31 +01001352 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001353 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1354 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1355 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001356 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001357 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1358 } else {
1359 compile_node(comp, pns->nodes[0]);
1360 }
1361 EMIT(return_value);
1362}
1363
Damien George969a6b32014-12-10 22:07:04 +00001364STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001365 compile_node(comp, pns->nodes[0]);
1366 EMIT(pop_top);
1367}
1368
Damien George969a6b32014-12-10 22:07:04 +00001369STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001370 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001371 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001372 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001373 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001374 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001375 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001376 compile_node(comp, pns->nodes[0]);
1377 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001378 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001379 } else {
1380 // raise x
1381 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001382 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001383 }
1384}
1385
Damien George635543c2014-04-10 12:56:52 +01001386// q_base holds the base of the name
1387// eg a -> q_base=a
1388// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001389STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001390 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001391 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1392 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001393 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001394 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001395 pn = pns->nodes[0];
1396 is_as = true;
1397 }
Damien George635543c2014-04-10 12:56:52 +01001398 if (MP_PARSE_NODE_IS_NULL(pn)) {
1399 // empty name (eg, from . import x)
1400 *q_base = MP_QSTR_;
1401 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1402 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001403 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001404 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001405 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001406 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001407 }
Damien George635543c2014-04-10 12:56:52 +01001408 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001409 } else {
1410 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001411 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001412 {
Damien429d7192013-10-04 19:53:11 +01001413 // a name of the form a.b.c
1414 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001415 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001416 }
Damiend99b0522013-12-21 18:17:45 +00001417 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001418 int len = n - 1;
1419 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001420 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001421 }
Damien George55baff42014-01-21 21:40:13 +00001422 byte *q_ptr;
1423 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001424 for (int i = 0; i < n; i++) {
1425 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001426 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001427 }
Damien George39dc1452014-10-03 19:52:22 +01001428 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001429 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001430 memcpy(str_dest, str_src, str_src_len);
1431 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001432 }
Damien George635543c2014-04-10 12:56:52 +01001433 qstr q_full = qstr_build_end(q_ptr);
1434 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001435 if (is_as) {
1436 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001437 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001438 }
1439 }
Damien429d7192013-10-04 19:53:11 +01001440 }
Damien429d7192013-10-04 19:53:11 +01001441 }
1442}
1443
Damien George6be0b0a2014-08-15 14:30:52 +01001444STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001445 EMIT_ARG(load_const_small_int, 0); // level 0 import
1446 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1447 qstr q_base;
1448 do_import_name(comp, pn, &q_base);
1449 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001450}
1451
Damien George969a6b32014-12-10 22:07:04 +00001452STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001453 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1454}
1455
Damien George969a6b32014-12-10 22:07:04 +00001456STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001457 mp_parse_node_t pn_import_source = pns->nodes[0];
1458
1459 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1460 uint import_level = 0;
1461 do {
1462 mp_parse_node_t pn_rel;
1463 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)) {
1464 // This covers relative imports with dots only like "from .. import"
1465 pn_rel = pn_import_source;
1466 pn_import_source = MP_PARSE_NODE_NULL;
1467 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1468 // This covers relative imports starting with dot(s) like "from .foo import"
1469 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1470 pn_rel = pns_2b->nodes[0];
1471 pn_import_source = pns_2b->nodes[1];
1472 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1473 } else {
1474 // Not a relative import
1475 break;
1476 }
1477
1478 // get the list of . and/or ...'s
1479 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001480 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001481
1482 // count the total number of .'s
1483 for (int i = 0; i < n; i++) {
1484 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1485 import_level++;
1486 } else {
1487 // should be an MP_TOKEN_ELLIPSIS
1488 import_level += 3;
1489 }
1490 }
1491 } while (0);
1492
Damiend99b0522013-12-21 18:17:45 +00001493 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001494 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001495
1496 // build the "fromlist" tuple
1497#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001498 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001499#else
Damien George708c0732014-04-27 19:23:46 +01001500 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001501 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001502#endif
1503
1504 // do the import
Damien George635543c2014-04-10 12:56:52 +01001505 qstr dummy_q;
1506 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001507 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001508
Damien429d7192013-10-04 19:53:11 +01001509 } else {
Damien George635543c2014-04-10 12:56:52 +01001510 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001511
1512 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001513 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001514 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001515#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001516 {
1517 vstr_t *vstr = vstr_new();
1518 vstr_printf(vstr, "(");
1519 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001520 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1521 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1522 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001523 if (i > 0) {
1524 vstr_printf(vstr, ", ");
1525 }
1526 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001527 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001528 const byte *str = qstr_data(id2, &len);
1529 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001530 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001531 }
Damien02f89412013-12-12 15:13:36 +00001532 if (n == 1) {
1533 vstr_printf(vstr, ",");
1534 }
1535 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001536 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001537 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001538 }
Damiendb4c3612013-12-10 17:27:24 +00001539#else
1540 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001541 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1542 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1543 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001544 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001545 }
Damien Georgeb9791222014-01-23 00:34:21 +00001546 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001547#endif
1548
1549 // do the import
Damien George635543c2014-04-10 12:56:52 +01001550 qstr dummy_q;
1551 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001552 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
Damien Georgeb9791222014-01-23 00:34:21 +00001556 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001557 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001558 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001559 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001560 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001561 }
1562 }
1563 EMIT(pop_top);
1564 }
1565}
1566
Damien George584ba672014-12-21 17:26:45 +00001567STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1568 bool added;
1569 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1570 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001571 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001572 compile_syntax_error(comp, pn, "identifier already used");
1573 return;
1574 }
1575 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1576
1577 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1578 id_info = scope_find_global(comp->scope_cur, qst);
1579 if (id_info != NULL) {
1580 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1581 }
1582}
1583
Damien George969a6b32014-12-10 22:07:04 +00001584STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001585 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001586 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001587 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001588 for (int i = 0; i < n; i++) {
1589 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001590 }
1591 }
1592}
1593
Damien George584ba672014-12-21 17:26:45 +00001594STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1595 bool added;
1596 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1597 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001598 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001599 compile_syntax_error(comp, pn, "identifier already used");
1600 return;
1601 }
1602 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1603 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)) {
1604 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1605 return;
1606 }
1607 id_info->kind = ID_INFO_KIND_FREE;
1608 scope_close_over_in_parents(comp->scope_cur, qst);
1609}
1610
Damien George969a6b32014-12-10 22:07:04 +00001611STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001612 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001613 if (comp->scope_cur->kind == SCOPE_MODULE) {
1614 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1615 return;
1616 }
1617 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001618 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001619 for (int i = 0; i < n; i++) {
1620 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001621 }
1622 }
1623}
1624
Damien George969a6b32014-12-10 22:07:04 +00001625STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001626 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001627 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001628 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 +00001629 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001630 // assertion message
1631 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001632 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001633 }
Damien Georgeb9791222014-01-23 00:34:21 +00001634 EMIT_ARG(raise_varargs, 1);
1635 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001636}
1637
Damien George969a6b32014-12-10 22:07:04 +00001638STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001639 // TODO proper and/or short circuiting
1640
Damien George6f355fd2014-04-10 14:11:31 +01001641 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001642
Damien George391db862014-10-17 17:57:33 +00001643 // optimisation: don't emit anything when "if False" (not in CPython)
1644 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1645 uint l_fail = comp_next_label(comp);
1646 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001647
Damien George391db862014-10-17 17:57:33 +00001648 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001649
Damien George391db862014-10-17 17:57:33 +00001650 // optimisation: skip everything else when "if True" (not in CPython)
1651 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1652 goto done;
1653 }
1654
1655 if (
1656 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1657 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1658 // optimisation: don't jump if last instruction was return
1659 && !EMIT(last_emit_was_return_value)
1660 ) {
1661 // jump over elif/else blocks
1662 EMIT_ARG(jump, l_end);
1663 }
1664
1665 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001666 }
1667
Damien George235f9b32014-10-17 17:30:16 +00001668 // compile elif blocks (if any)
1669 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001670 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 +00001671 for (int i = 0; i < n_elif; i++) {
1672 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1673 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001674
Damien George391db862014-10-17 17:57:33 +00001675 // optimisation: don't emit anything when "if False" (not in CPython)
1676 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1677 uint l_fail = comp_next_label(comp);
1678 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001679
Damien George391db862014-10-17 17:57:33 +00001680 compile_node(comp, pns_elif->nodes[1]); // elif block
1681
1682 // optimisation: skip everything else when "elif True" (not in CPython)
1683 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1684 goto done;
1685 }
1686
1687 // optimisation: don't jump if last instruction was return
1688 if (!EMIT(last_emit_was_return_value)) {
1689 EMIT_ARG(jump, l_end);
1690 }
1691 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001692 }
1693 }
1694
1695 // compile else block
1696 compile_node(comp, pns->nodes[3]); // can be null
1697
Damien George391db862014-10-17 17:57:33 +00001698done:
Damien Georgeb9791222014-01-23 00:34:21 +00001699 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001700}
1701
Damien Georgecbddb272014-02-01 20:08:18 +00001702#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001703 uint16_t old_break_label = comp->break_label; \
1704 uint16_t old_continue_label = comp->continue_label; \
1705 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001706 uint break_label = comp_next_label(comp); \
1707 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001708 comp->break_label = break_label; \
1709 comp->continue_label = continue_label; \
1710 comp->break_continue_except_level = comp->cur_except_level;
1711
1712#define END_BREAK_CONTINUE_BLOCK \
1713 comp->break_label = old_break_label; \
1714 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001715 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001716
Damien George969a6b32014-12-10 22:07:04 +00001717STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001718 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001719
Damience89a212013-10-15 22:25:17 +01001720 // compared to CPython, we have an optimised version of while loops
1721#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001722 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(setup_loop, break_label);
1724 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001725 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1726 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001727 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001728 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001729 }
Damien Georgeb9791222014-01-23 00:34:21 +00001730 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001731 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1732 // this is a small hack to agree with CPython
1733 if (!node_is_const_true(pns->nodes[0])) {
1734 EMIT(pop_block);
1735 }
Damience89a212013-10-15 22:25:17 +01001736#else
Damien George391db862014-10-17 17:57:33 +00001737 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1738 uint top_label = comp_next_label(comp);
1739 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1740 EMIT_ARG(jump, continue_label);
1741 }
1742 EMIT_ARG(label_assign, top_label);
1743 compile_node(comp, pns->nodes[1]); // body
1744 EMIT_ARG(label_assign, continue_label);
1745 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1746 }
Damience89a212013-10-15 22:25:17 +01001747#endif
1748
1749 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001750 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001751
1752 compile_node(comp, pns->nodes[2]); // else
1753
Damien Georgeb9791222014-01-23 00:34:21 +00001754 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001755}
1756
Damien George2ac4af62014-08-15 16:45:41 +01001757#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001758// This function compiles an optimised for-loop of the form:
1759// for <var> in range(<start>, <end>, <step>):
1760// <body>
1761// else:
1762// <else>
1763// <var> must be an identifier and <step> must be a small-int.
1764//
1765// Semantics of for-loop require:
1766// - final failing value should not be stored in the loop variable
1767// - if the loop never runs, the loop variable should never be assigned
1768// - assignments to <var>, <end> or <step> in the body do not alter the loop
1769// (<step> is a constant for us, so no need to worry about it changing)
1770//
1771// If <end> is a small-int, then the stack during the for-loop contains just
1772// the current value of <var>. Otherwise, the stack contains <end> then the
1773// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001774STATIC 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 +00001775 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001776
Damien George6f355fd2014-04-10 14:11:31 +01001777 uint top_label = comp_next_label(comp);
1778 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001779
Damien Georgee181c0d2014-12-12 17:19:56 +00001780 // put the end value on the stack if it's not a small-int constant
1781 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1782 if (end_on_stack) {
1783 compile_node(comp, pn_end);
1784 }
1785
1786 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001787 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001788
Damien Georgeb9791222014-01-23 00:34:21 +00001789 EMIT_ARG(jump, entry_label);
1790 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001791
Damien Georgec33ce602014-12-11 17:35:23 +00001792 // duplicate next value and store it to var
1793 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001794 c_assign(comp, pn_var, ASSIGN_STORE);
1795
Damienf3822fc2013-11-09 20:12:03 +00001796 // compile body
1797 compile_node(comp, pn_body);
1798
Damien Georgeb9791222014-01-23 00:34:21 +00001799 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001800
Damien Georgee181c0d2014-12-12 17:19:56 +00001801 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001802 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001803 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001804
Damien Georgeb9791222014-01-23 00:34:21 +00001805 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001806
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001807 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001808 if (end_on_stack) {
1809 EMIT(dup_top_two);
1810 EMIT(rot_two);
1811 } else {
1812 EMIT(dup_top);
1813 compile_node(comp, pn_end);
1814 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001815 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1816 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001817 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001818 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001819 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001820 }
Damien George63f38322015-02-28 15:04:06 +00001821 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001822
1823 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001824 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001825
1826 compile_node(comp, pn_else);
1827
Damien Georgeb9791222014-01-23 00:34:21 +00001828 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001829
1830 // discard final value of var that failed the loop condition
1831 EMIT(pop_top);
1832
1833 // discard <end> value if it's on the stack
1834 if (end_on_stack) {
1835 EMIT(pop_top);
1836 }
Damienf72fd0e2013-11-06 20:20:49 +00001837}
Damien George2ac4af62014-08-15 16:45:41 +01001838#endif
Damienf72fd0e2013-11-06 20:20:49 +00001839
Damien George969a6b32014-12-10 22:07:04 +00001840STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001841#if !MICROPY_EMIT_CPYTHON
1842 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1843 // this is actually slower, but uses no heap memory
1844 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001845 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 +00001846 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001847 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1848 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1849 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1850 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001851 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1852 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001853 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001854 mp_parse_node_t pn_range_start;
1855 mp_parse_node_t pn_range_end;
1856 mp_parse_node_t pn_range_step;
1857 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001858 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001859 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001860 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001861 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001862 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001863 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001864 } else if (n_args == 2) {
1865 pn_range_start = args[0];
1866 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001867 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001868 } else {
1869 pn_range_start = args[0];
1870 pn_range_end = args[1];
1871 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001872 // We need to know sign of step. This is possible only if it's constant
1873 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1874 optimize = false;
1875 }
Damienf72fd0e2013-11-06 20:20:49 +00001876 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001877 }
1878 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001879 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1880 return;
1881 }
1882 }
1883 }
1884#endif
1885
Damien Georgecbddb272014-02-01 20:08:18 +00001886 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001887 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001888
Damien George6f355fd2014-04-10 14:11:31 +01001889 uint pop_label = comp_next_label(comp);
1890 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001891
Damience89a212013-10-15 22:25:17 +01001892 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1893#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001894 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001895#endif
1896
Damien429d7192013-10-04 19:53:11 +01001897 compile_node(comp, pns->nodes[1]); // iterator
1898 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001899 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001900 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001901 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1902 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001903 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001904 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001905 }
Damien Georgeb9791222014-01-23 00:34:21 +00001906 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001907 EMIT(for_iter_end);
1908
1909 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001910 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001911
Damience89a212013-10-15 22:25:17 +01001912#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001913 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001914#endif
Damien429d7192013-10-04 19:53:11 +01001915
1916 compile_node(comp, pns->nodes[3]); // else (not tested)
1917
Damien Georgeb9791222014-01-23 00:34:21 +00001918 EMIT_ARG(label_assign, break_label);
1919 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001920}
1921
Damien George6be0b0a2014-08-15 14:30:52 +01001922STATIC 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 +01001923 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001924 uint l1 = comp_next_label(comp);
1925 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001926
Damien Georgeb9791222014-01-23 00:34:21 +00001927 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001928 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001929
Damien429d7192013-10-04 19:53:11 +01001930 compile_node(comp, pn_body); // body
1931 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001932 EMIT_ARG(jump, success_label); // jump over exception handler
1933
1934 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001935 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001936
Damien George6f355fd2014-04-10 14:11:31 +01001937 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001938
1939 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001940 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1941 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001942
1943 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001944 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001945
Damiend99b0522013-12-21 18:17:45 +00001946 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001947 // this is a catch all exception handler
1948 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001949 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001950 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001951 return;
1952 }
1953 } else {
1954 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001955 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1956 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1957 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1958 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001959 // handler binds the exception to a local
1960 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001961 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001962 }
1963 }
1964 EMIT(dup_top);
1965 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001966 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001967 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001968 }
1969
1970 EMIT(pop_top);
1971
1972 if (qstr_exception_local == 0) {
1973 EMIT(pop_top);
1974 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001975 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001976 }
1977
1978 EMIT(pop_top);
1979
Damien George6f355fd2014-04-10 14:11:31 +01001980 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001981 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001982 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001983 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001984 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001985 }
1986 compile_node(comp, pns_except->nodes[1]);
1987 if (qstr_exception_local != 0) {
1988 EMIT(pop_block);
1989 }
1990 EMIT(pop_except);
1991 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001992 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1993 EMIT_ARG(label_assign, l3);
1994 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1995 EMIT_ARG(store_id, qstr_exception_local);
1996 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001997
Damien George8dcc0c72014-03-27 10:55:21 +00001998 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001999 EMIT(end_finally);
2000 }
Damien Georgeb9791222014-01-23 00:34:21 +00002001 EMIT_ARG(jump, l2);
2002 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002003 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002004 }
2005
Damien George8dcc0c72014-03-27 10:55:21 +00002006 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002007 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002008 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002009
Damien Georgeb9791222014-01-23 00:34:21 +00002010 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002011 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002012 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002013}
2014
Damien George6be0b0a2014-08-15 14:30:52 +01002015STATIC 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 +01002016 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002017
Damien Georgeb9791222014-01-23 00:34:21 +00002018 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002019 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002020
Damien429d7192013-10-04 19:53:11 +01002021 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002022 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002023 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002024 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002025 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002026 } else {
2027 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2028 }
2029 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002030 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2031 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002032 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002033
Damien George8dcc0c72014-03-27 10:55:21 +00002034 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002035 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002036}
2037
Damien George969a6b32014-12-10 22:07:04 +00002038STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00002039 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2040 {
Damiend99b0522013-12-21 18:17:45 +00002041 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2042 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002043 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002044 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2045 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002046 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002047 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002048 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002049 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002050 // no finally
2051 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2052 } else {
2053 // have finally
Damiend99b0522013-12-21 18:17:45 +00002054 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 +01002055 }
2056 } else {
2057 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002058 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002059 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002060 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002061 }
Damien429d7192013-10-04 19:53:11 +01002062 }
2063}
2064
Damien George6be0b0a2014-08-15 14:30:52 +01002065STATIC 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 +01002066 if (n == 0) {
2067 // no more pre-bits, compile the body of the with
2068 compile_node(comp, body);
2069 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002070 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002071 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002072 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002073 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002074 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002075 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002076 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2077 } else {
2078 // this pre-bit is just an expression
2079 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002080 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002081 EMIT(pop_top);
2082 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002083 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002084 // compile additional pre-bits and the body
2085 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2086 // finish this with block
2087 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002088 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2089 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002090 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002091 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002092 EMIT(end_finally);
2093 }
2094}
2095
Damien George969a6b32014-12-10 22:07:04 +00002096STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002097 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002098 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002099 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002100 assert(n > 0);
2101
2102 // compile in a nested fashion
2103 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2104}
2105
Damien George969a6b32014-12-10 22:07:04 +00002106STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002107 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002108 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2109 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002110 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002111 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002112 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002113 EMIT(pop_top);
2114
Damien429d7192013-10-04 19:53:11 +01002115 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002116 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002117 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002118 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002119 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2120 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002121 // do nothing with a lonely constant
2122 } else {
2123 compile_node(comp, pns->nodes[0]); // just an expression
2124 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2125 }
Damien429d7192013-10-04 19:53:11 +01002126 }
2127 } else {
Damiend99b0522013-12-21 18:17:45 +00002128 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2129 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002130 if (kind == PN_expr_stmt_augassign) {
2131 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2132 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002133 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002134 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002135 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002136 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2137 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2138 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2139 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2140 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2141 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2142 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2143 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2144 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2145 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2146 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002147 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002148 }
Damien George7e5fb242014-02-01 22:18:47 +00002149 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002150 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2151 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002152 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2153 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002154 // following CPython, we store left-most first
2155 if (rhs > 0) {
2156 EMIT(dup_top);
2157 }
2158 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2159 for (int i = 0; i < rhs; i++) {
2160 if (i + 1 < rhs) {
2161 EMIT(dup_top);
2162 }
Damiend99b0522013-12-21 18:17:45 +00002163 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002164 }
Damien George0bb97132015-02-27 14:25:47 +00002165 } else {
2166 assert(kind == PN_expr_stmt_assign); // should be
Damien George42e0c592015-03-14 13:11:35 +00002167 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
2168 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002169 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2170 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2171 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002172 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002173 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2174 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002175 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2176 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2177 // can't optimise when it's a star expression on the lhs
2178 goto no_optimisation;
2179 }
Damien429d7192013-10-04 19:53:11 +01002180 compile_node(comp, pns10->nodes[0]); // rhs
2181 compile_node(comp, pns10->nodes[1]); // rhs
2182 EMIT(rot_two);
2183 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2184 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00002185 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2186 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002187 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2188 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2189 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002190 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002191 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2192 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002193 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2194 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2195 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2196 // can't optimise when it's a star expression on the lhs
2197 goto no_optimisation;
2198 }
Damien429d7192013-10-04 19:53:11 +01002199 compile_node(comp, pns10->nodes[0]); // rhs
2200 compile_node(comp, pns10->nodes[1]); // rhs
2201 compile_node(comp, pns10->nodes[2]); // rhs
2202 EMIT(rot_three);
2203 EMIT(rot_two);
2204 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2205 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2206 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2207 } else {
Damien George495d7812014-04-08 17:51:47 +01002208 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002209 compile_node(comp, pns1->nodes[0]); // rhs
2210 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2211 }
Damien429d7192013-10-04 19:53:11 +01002212 }
2213 }
2214}
2215
Damien George6be0b0a2014-08-15 14:30:52 +01002216STATIC 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 +00002217 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002218 compile_node(comp, pns->nodes[0]);
2219 for (int i = 1; i < num_nodes; i += 1) {
2220 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002221 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002222 }
2223}
2224
Damien George969a6b32014-12-10 22:07:04 +00002225STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002226 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2227 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002228
Damien George6f355fd2014-04-10 14:11:31 +01002229 uint l_fail = comp_next_label(comp);
2230 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002231 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2232 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002233 EMIT_ARG(jump, l_end);
2234 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002235 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002236 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002237 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002238}
2239
Damien George969a6b32014-12-10 22:07:04 +00002240STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002241 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002242 //mp_parse_node_t pn_params = pns->nodes[0];
2243 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002244
Damien George36db6bc2014-05-07 17:24:22 +01002245 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002246 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002247 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 +01002248 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002249 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002250 }
2251
2252 // get the scope for this lambda
2253 scope_t *this_scope = (scope_t*)pns->nodes[2];
2254
2255 // make the lambda
2256 close_over_variables_etc(comp, this_scope, 0, 0);
2257}
2258
Damien George7711afb2015-02-28 15:10:18 +00002259STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002260 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002261 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002262 for (int i = 0; i < n; i += 1) {
2263 compile_node(comp, pns->nodes[i]);
2264 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002265 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002266 }
2267 }
Damien Georgeb9791222014-01-23 00:34:21 +00002268 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002269}
2270
Damien George7711afb2015-02-28 15:10:18 +00002271STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2272 compile_or_and_test(comp, pns, true);
2273}
2274
Damien George969a6b32014-12-10 22:07:04 +00002275STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002276 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002277}
2278
Damien George969a6b32014-12-10 22:07:04 +00002279STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002280 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002281 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002282}
2283
Damien George969a6b32014-12-10 22:07:04 +00002284STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002285 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002286 compile_node(comp, pns->nodes[0]);
2287 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002288 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002289 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002290 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002291 }
2292 for (int i = 1; i + 1 < num_nodes; i += 2) {
2293 compile_node(comp, pns->nodes[i + 1]);
2294 if (i + 2 < num_nodes) {
2295 EMIT(dup_top);
2296 EMIT(rot_three);
2297 }
Damien George7e5fb242014-02-01 22:18:47 +00002298 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002299 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002300 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002301 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2302 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2303 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2304 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2305 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2306 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002307 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002308 }
2309 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002310 } else {
2311 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002312 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2313 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002314 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002315 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002316 } else {
2317 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002318 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002319 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002320 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002321 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002322 }
Damien429d7192013-10-04 19:53:11 +01002323 }
Damien429d7192013-10-04 19:53:11 +01002324 }
2325 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002326 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002327 }
2328 }
2329 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002330 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002331 EMIT_ARG(jump, l_end);
2332 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002333 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002334 EMIT(rot_two);
2335 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002336 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002337 }
2338}
2339
Damien George969a6b32014-12-10 22:07:04 +00002340STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002341 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002342}
2343
Damien George969a6b32014-12-10 22:07:04 +00002344STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002345 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002346}
2347
Damien George969a6b32014-12-10 22:07:04 +00002348STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002349 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002350}
2351
Damien George969a6b32014-12-10 22:07:04 +00002352STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002353 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002354}
2355
Damien George969a6b32014-12-10 22:07:04 +00002356STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002357 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002358 compile_node(comp, pns->nodes[0]);
2359 for (int i = 1; i + 1 < num_nodes; i += 2) {
2360 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002361 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002362 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002363 } else {
Damien George0bb97132015-02-27 14:25:47 +00002364 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2365 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002366 }
2367 }
2368}
2369
Damien George969a6b32014-12-10 22:07:04 +00002370STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002371 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002372 compile_node(comp, pns->nodes[0]);
2373 for (int i = 1; i + 1 < num_nodes; i += 2) {
2374 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002375 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002376 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002377 } else {
Damien George0bb97132015-02-27 14:25:47 +00002378 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2379 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002380 }
2381 }
2382}
2383
Damien George969a6b32014-12-10 22:07:04 +00002384STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002385 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002386 compile_node(comp, pns->nodes[0]);
2387 for (int i = 1; i + 1 < num_nodes; i += 2) {
2388 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002389 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002390 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002391 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002392 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002393 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002394 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002395 } else {
Damien George0bb97132015-02-27 14:25:47 +00002396 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2397 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002398 }
2399 }
2400}
2401
Damien George969a6b32014-12-10 22:07:04 +00002402STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002403 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002404 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002405 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002406 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002407 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002408 } else {
Damien George0bb97132015-02-27 14:25:47 +00002409 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2410 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002411 }
2412}
2413
Damien George969a6b32014-12-10 22:07:04 +00002414STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002415 // this is to handle special super() call
2416 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2417
2418 compile_generic_all_nodes(comp, pns);
2419}
2420
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002421STATIC 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 +01002422 // function to call is on top of stack
2423
Damien George35e2a4e2014-02-05 00:51:47 +00002424#if !MICROPY_EMIT_CPYTHON
2425 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002426 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 +00002427 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002428 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002429 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002430 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002431 // first argument found; load it and call super
Damien George0abb5602015-01-16 12:24:49 +00002432 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002433 EMIT_ARG(call_function, 2, 0, 0);
2434 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002435 }
2436 }
Damien George01039b52014-12-21 17:44:27 +00002437 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002438 return;
2439 }
2440#endif
2441
Damien George2c0842b2014-04-27 16:46:51 +01002442 // get the list of arguments
2443 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002444 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002445
Damien George2c0842b2014-04-27 16:46:51 +01002446 // compile the arguments
2447 // Rather than calling compile_node on the list, we go through the list of args
2448 // explicitly here so that we can count the number of arguments and give sensible
2449 // error messages.
2450 int n_positional = n_positional_extra;
2451 uint n_keyword = 0;
2452 uint star_flags = 0;
2453 for (int i = 0; i < n_args; i++) {
2454 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2455 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2456 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2457 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2458 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2459 return;
2460 }
2461 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2462 compile_node(comp, pns_arg->nodes[0]);
2463 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2464 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2465 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2466 return;
2467 }
2468 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2469 compile_node(comp, pns_arg->nodes[0]);
2470 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2471 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2472 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2473 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2474 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2475 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2476 return;
2477 }
Damien George968bf342014-04-27 19:12:05 +01002478 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002479 compile_node(comp, pns2->nodes[0]);
2480 n_keyword += 1;
2481 } else {
2482 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2483 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2484 n_positional++;
2485 }
2486 } else {
2487 goto normal_argument;
2488 }
2489 } else {
2490 normal_argument:
2491 if (n_keyword > 0) {
2492 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2493 return;
2494 }
2495 compile_node(comp, args[i]);
2496 n_positional++;
2497 }
Damien429d7192013-10-04 19:53:11 +01002498 }
2499
Damien George2c0842b2014-04-27 16:46:51 +01002500 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002501 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002502 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002503 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002504 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002505 }
Damien429d7192013-10-04 19:53:11 +01002506}
2507
Damien George969a6b32014-12-10 22:07:04 +00002508STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002509 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002510 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002511 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 +01002512 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002513 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2514 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002515 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002516 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002517 i += 1;
2518 } else {
2519 compile_node(comp, pns->nodes[i]);
2520 }
Damien George35e2a4e2014-02-05 00:51:47 +00002521 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002522 }
2523}
2524
Damien George969a6b32014-12-10 22:07:04 +00002525STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002526 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002527 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002528}
2529
Damien George969a6b32014-12-10 22:07:04 +00002530STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002531 // a list of strings
Damien63321742013-12-10 17:41:49 +00002532
2533 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002534 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002535 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002536 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002537 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002538 int pn_kind;
2539 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2540 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2541 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2542 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2543 } else {
2544 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2545 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002546 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2547 pn_kind = MP_PARSE_NODE_STRING;
2548 } else {
2549 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2550 pn_kind = MP_PARSE_NODE_BYTES;
2551 }
Damien George40f3c022014-07-03 13:25:24 +01002552 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002553 }
Damien63321742013-12-10 17:41:49 +00002554 if (i == 0) {
2555 string_kind = pn_kind;
2556 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002557 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002558 return;
2559 }
Damien429d7192013-10-04 19:53:11 +01002560 }
Damien63321742013-12-10 17:41:49 +00002561
Damien George65ef6b72015-01-14 21:17:27 +00002562 // if we are not in the last pass, just load a dummy object
2563 if (comp->pass != MP_PASS_EMIT) {
2564 EMIT_ARG(load_const_obj, mp_const_none);
2565 return;
2566 }
2567
Damien63321742013-12-10 17:41:49 +00002568 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002569 vstr_t vstr;
2570 vstr_init_len(&vstr, n_bytes);
2571 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002572 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002573 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002574 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002575 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2576 memcpy(s_dest, s, s_len);
2577 s_dest += s_len;
2578 } else {
2579 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002580 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2581 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002582 }
Damien63321742013-12-10 17:41:49 +00002583 }
Damien George65ef6b72015-01-14 21:17:27 +00002584
2585 // load the object
Damien George05005f62015-01-21 22:48:37 +00002586 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 +01002587}
2588
2589// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002590STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002591 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2592 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2593 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002594
Damien George36db6bc2014-05-07 17:24:22 +01002595 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002596 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002597 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 +01002598 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002599 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002600 }
2601
2602 // get the scope for this comprehension
2603 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2604
2605 // compile the comprehension
2606 close_over_variables_etc(comp, this_scope, 0, 0);
2607
2608 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2609 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002610 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002611}
2612
Damien George969a6b32014-12-10 22:07:04 +00002613STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002614 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002615 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002616 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2617 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2618 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2619 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2620 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2621 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2622 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002623 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002624 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002625 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002626 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002627 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002628 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002629 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002630 // generator expression
2631 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2632 } else {
2633 // tuple with 2 items
2634 goto tuple_with_2_items;
2635 }
2636 } else {
2637 // tuple with 2 items
2638 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002639 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002640 }
2641 } else {
2642 // parenthesis around a single item, is just that item
2643 compile_node(comp, pns->nodes[0]);
2644 }
2645}
2646
Damien George969a6b32014-12-10 22:07:04 +00002647STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002648 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002649 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002650 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002651 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2652 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2653 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2654 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2655 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002656 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002657 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002658 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002659 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002660 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002661 // list of many items
2662 compile_node(comp, pns2->nodes[0]);
2663 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002664 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002665 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002666 // list comprehension
2667 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2668 } else {
2669 // list with 2 items
2670 goto list_with_2_items;
2671 }
2672 } else {
2673 // list with 2 items
2674 list_with_2_items:
2675 compile_node(comp, pns2->nodes[0]);
2676 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002677 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002678 }
2679 } else {
2680 // list with 1 item
2681 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002682 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002683 }
2684}
2685
Damien George969a6b32014-12-10 22:07:04 +00002686STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002687 mp_parse_node_t pn = pns->nodes[0];
2688 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002689 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002690 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002691 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2692 pns = (mp_parse_node_struct_t*)pn;
2693 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002694 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002695 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002696 compile_node(comp, pn);
2697 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002698 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2699 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2700 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2701 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002702 // dict/set with multiple elements
2703
2704 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002705 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002706 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002707
2708 // first element sets whether it's a dict or set
2709 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002710 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002711 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002712 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002713 compile_node(comp, pns->nodes[0]);
2714 EMIT(store_map);
2715 is_dict = true;
2716 } else {
2717 // a set
2718 compile_node(comp, pns->nodes[0]); // 1st value of set
2719 is_dict = false;
2720 }
2721
2722 // process rest of elements
2723 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002724 mp_parse_node_t pn_i = nodes[i];
2725 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2726 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002727 if (is_dict) {
2728 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002729 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002730 return;
2731 }
2732 EMIT(store_map);
2733 } else {
2734 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002735 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002736 return;
2737 }
2738 }
2739 }
2740
Damien Georgee37dcaa2014-12-27 17:07:16 +00002741 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002742 // if it's a set, build it
2743 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002744 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002745 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002746 #endif
Damien George0bb97132015-02-27 14:25:47 +00002747 } else {
2748 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002749 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002750 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002751 // a dictionary comprehension
2752 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2753 } else {
2754 // a set comprehension
2755 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2756 }
Damien429d7192013-10-04 19:53:11 +01002757 }
2758 } else {
2759 // set with one element
2760 goto set_with_one_element;
2761 }
2762 } else {
2763 // set with one element
2764 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002765 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002766 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002767 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002768 #else
2769 assert(0);
2770 #endif
Damien429d7192013-10-04 19:53:11 +01002771 }
2772}
2773
Damien George969a6b32014-12-10 22:07:04 +00002774STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002775 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002776}
2777
Damien George969a6b32014-12-10 22:07:04 +00002778STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002779 // object who's index we want is on top of stack
2780 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002781 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002782}
2783
Damien George969a6b32014-12-10 22:07:04 +00002784STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002785 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002786 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002787}
2788
Damien George83204f32014-12-27 17:20:41 +00002789#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002790STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002791 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2792 mp_parse_node_t pn = pns->nodes[0];
2793 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002794 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002795 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2796 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002797 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2798 pns = (mp_parse_node_struct_t*)pn;
2799 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002800 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002801 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002802 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002803 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002804 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002805 } else {
2806 // [?::x]
2807 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002808 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002809 }
Damiend99b0522013-12-21 18:17:45 +00002810 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002811 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002812 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2813 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2814 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2815 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002816 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002817 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002818 } else {
2819 // [?:x:x]
2820 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002821 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002822 }
2823 } else {
2824 // [?:x]
2825 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002826 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002827 }
2828 } else {
2829 // [?:x]
2830 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002831 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002832 }
2833}
2834
Damien George969a6b32014-12-10 22:07:04 +00002835STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002836 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002837 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2838 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002839}
2840
Damien George969a6b32014-12-10 22:07:04 +00002841STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002842 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002843 compile_subscript_3_helper(comp, pns);
2844}
Damien George83204f32014-12-27 17:20:41 +00002845#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002846
Damien George969a6b32014-12-10 22:07:04 +00002847STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002848 // if this is called then we are compiling a dict key:value pair
2849 compile_node(comp, pns->nodes[1]); // value
2850 compile_node(comp, pns->nodes[0]); // key
2851}
2852
Damien George969a6b32014-12-10 22:07:04 +00002853STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002854 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002855 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002857}
2858
Damien George969a6b32014-12-10 22:07:04 +00002859STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002860 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002861 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002862 return;
2863 }
Damiend99b0522013-12-21 18:17:45 +00002864 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002865 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002866 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002867 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2868 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002869 compile_node(comp, pns->nodes[0]);
2870 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002871 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002872 EMIT(yield_from);
2873 } else {
2874 compile_node(comp, pns->nodes[0]);
2875 EMIT(yield_value);
2876 }
2877}
2878
Damien George65ef6b72015-01-14 21:17:27 +00002879STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2880 // only create and load the actual str object on the last pass
2881 if (comp->pass != MP_PASS_EMIT) {
2882 EMIT_ARG(load_const_obj, mp_const_none);
2883 } else {
2884 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2885 }
2886}
2887
2888STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2889 // only create and load the actual bytes object on the last pass
2890 if (comp->pass != MP_PASS_EMIT) {
2891 EMIT_ARG(load_const_obj, mp_const_none);
2892 } else {
2893 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2894 }
2895}
2896
Damien George7d414a12015-02-08 01:57:40 +00002897STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2898 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2899}
2900
Damiend99b0522013-12-21 18:17:45 +00002901typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002902STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002903#define nc NULL
2904#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002905#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002906#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002907#undef nc
2908#undef c
2909#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002910 NULL,
2911 compile_string,
2912 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002913 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002914};
2915
Damien George6be0b0a2014-08-15 14:30:52 +01002916STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002917 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002918 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002919 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002920 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002921 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002922 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002923 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002924 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002925 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002926 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2927 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damien Georged2d64f02015-01-14 21:32:42 +00002928 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002929 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002930 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002931 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002932 // do nothing
2933 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002934 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002935 }
2936 break;
Damien429d7192013-10-04 19:53:11 +01002937 }
2938 } else {
Damiend99b0522013-12-21 18:17:45 +00002939 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002940 EMIT_ARG(set_line_number, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002941 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2942 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002943#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002944 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2945 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002946#endif
Damien George65ef6b72015-01-14 21:17:27 +00002947 compile_syntax_error(comp, pn, "internal compiler error");
2948 } else {
2949 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002950 }
2951 }
2952}
2953
Damien George2ac4af62014-08-15 16:45:41 +01002954STATIC 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 +01002955 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002956 qstr param_name = MP_QSTR_NULL;
2957 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002958 if (MP_PARSE_NODE_IS_ID(pn)) {
2959 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002960 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002961 // comes after a star, so counts as a keyword-only parameter
2962 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002963 } else {
Damien George2827d622014-04-27 15:50:52 +01002964 // comes before a star, so counts as a positional parameter
2965 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002966 }
Damienb14de212013-10-06 00:28:28 +01002967 } else {
Damiend99b0522013-12-21 18:17:45 +00002968 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2969 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2970 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2971 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002972 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002973 // comes after a star, so counts as a keyword-only parameter
2974 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002975 } else {
Damien George2827d622014-04-27 15:50:52 +01002976 // comes before a star, so counts as a positional parameter
2977 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002978 }
Damiend99b0522013-12-21 18:17:45 +00002979 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002980 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002981 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002982 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002983 // bare star
2984 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002985 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002986 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002987 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002988 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002989 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002990 } else {
2991 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002992 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002993 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002994 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2995 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002996 }
Damien George0bb97132015-02-27 14:25:47 +00002997 } else {
2998 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002999 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003000 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003001 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003002 }
Damien429d7192013-10-04 19:53:11 +01003003 }
3004
Damien George2827d622014-04-27 15:50:52 +01003005 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003006 bool added;
3007 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3008 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003009 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003010 return;
3011 }
Damien429d7192013-10-04 19:53:11 +01003012 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003013 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003014 }
3015}
3016
Damien George2827d622014-04-27 15:50:52 +01003017STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003018 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003019}
3020
Damien George2827d622014-04-27 15:50:52 +01003021STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003022 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3023}
3024
3025STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3026 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3027 // no annotation
3028 return;
3029 }
3030
3031 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3032 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3033 // named parameter with possible annotation
3034 // fallthrough
3035 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3036 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3037 // named star with possible annotation
3038 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3039 // fallthrough
3040 } else {
3041 // no annotation
3042 return;
3043 }
3044 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3045 // double star with possible annotation
3046 // fallthrough
3047 } else {
3048 // no annotation
3049 return;
3050 }
3051
3052 mp_parse_node_t pn_annotation = pns->nodes[1];
3053
3054 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3055 #if MICROPY_EMIT_NATIVE
3056 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3057 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3058 assert(id_info != NULL);
3059
3060 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3061 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3062 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3063 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3064 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003065 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003066 }
3067 }
3068 #endif // MICROPY_EMIT_NATIVE
3069 }
Damien429d7192013-10-04 19:53:11 +01003070}
3071
Damien George6be0b0a2014-08-15 14:30:52 +01003072STATIC 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 +01003073 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003074 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003075 // no more nested if/for; compile inner expression
3076 compile_node(comp, pn_inner_expr);
3077 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003078 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003079 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003080 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003081 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003082 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003083 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003084 #endif
Damien429d7192013-10-04 19:53:11 +01003085 } else {
3086 EMIT(yield_value);
3087 EMIT(pop_top);
3088 }
Damiend99b0522013-12-21 18:17:45 +00003089 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003090 // if condition
Damiend99b0522013-12-21 18:17:45 +00003091 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003092 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3093 pn_iter = pns_comp_if->nodes[1];
3094 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00003095 } else {
3096 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01003097 // for loop
Damiend99b0522013-12-21 18:17:45 +00003098 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003099 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003100 uint l_end2 = comp_next_label(comp);
3101 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003102 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003103 EMIT_ARG(label_assign, l_top2);
3104 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003105 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3106 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 +00003107 EMIT_ARG(jump, l_top2);
3108 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003109 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01003110 }
3111}
3112
Damien George1463c1f2014-04-25 23:52:57 +01003113STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3114#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003115 // see http://www.python.org/dev/peps/pep-0257/
3116
3117 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003118 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003119 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003120 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003121 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003122 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3123 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003124 for (int i = 0; i < num_nodes; i++) {
3125 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003126 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 +00003127 // not a newline, so this is the first statement; finish search
3128 break;
3129 }
3130 }
3131 // 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 +00003132 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003133 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003134 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003135 } else {
3136 return;
3137 }
3138
3139 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003140 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3141 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003142 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3143 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3144 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3145 // compile the doc string
3146 compile_node(comp, pns->nodes[0]);
3147 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003148 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003149 }
3150 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003151#else
3152 (void)comp;
3153 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003154#endif
Damien429d7192013-10-04 19:53:11 +01003155}
3156
Damien George1463c1f2014-04-25 23:52:57 +01003157STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003158 comp->pass = pass;
3159 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003160 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003161 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003162
Damien George36db6bc2014-05-07 17:24:22 +01003163 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003164 // reset maximum stack sizes in scope
3165 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003166 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003167 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003168 }
3169
Damien5ac1b2e2013-10-18 19:58:12 +01003170#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003171 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003172 scope_print_info(scope);
3173 }
Damien5ac1b2e2013-10-18 19:58:12 +01003174#endif
Damien429d7192013-10-04 19:53:11 +01003175
3176 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003177 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3178 assert(scope->kind == SCOPE_MODULE);
3179 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3180 compile_node(comp, pns->nodes[0]); // compile the expression
3181 EMIT(return_value);
3182 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003183 if (!comp->is_repl) {
3184 check_for_doc_string(comp, scope->pn);
3185 }
Damien429d7192013-10-04 19:53:11 +01003186 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003187 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003188 EMIT(return_value);
3189 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003190 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3191 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3192 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003193
3194 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003195 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003196 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003197 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003198 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003199 } else {
3200 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003201
Damien George2ac4af62014-08-15 16:45:41 +01003202 // argument annotations
3203 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3204
3205 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003206 mp_parse_node_t pn_annotation = pns->nodes[2];
3207 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3208 #if MICROPY_EMIT_NATIVE
3209 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3210 // nodes[2] can be null or a test-expr
3211 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3212 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3213 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3214 } else {
3215 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3216 }
Damien George2ac4af62014-08-15 16:45:41 +01003217 }
Damien Georgea5190a72014-08-15 22:39:08 +01003218 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003219 }
Damien George2ac4af62014-08-15 16:45:41 +01003220 }
Damien429d7192013-10-04 19:53:11 +01003221
3222 compile_node(comp, pns->nodes[3]); // 3 is function body
3223 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003224 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003225 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003226 EMIT(return_value);
3227 }
3228 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003229 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3230 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3231 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003232
3233 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003234 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003235 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003236 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003237 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3238 }
3239
3240 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003241
3242 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3243 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3244 EMIT(pop_top);
3245 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3246 }
Damien429d7192013-10-04 19:53:11 +01003247 EMIT(return_value);
3248 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3249 // a bit of a hack at the moment
3250
Damiend99b0522013-12-21 18:17:45 +00003251 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3252 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3253 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3254 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3255 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003256
Damien George708c0732014-04-27 19:23:46 +01003257 // We need a unique name for the comprehension argument (the iterator).
3258 // CPython uses .0, but we should be able to use anything that won't
3259 // clash with a user defined variable. Best to use an existing qstr,
3260 // so we use the blank qstr.
3261#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003262 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003263#else
3264 qstr qstr_arg = MP_QSTR_;
3265#endif
Damien George36db6bc2014-05-07 17:24:22 +01003266 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003267 bool added;
3268 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3269 assert(added);
3270 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003271 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003272 }
3273
3274 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003275 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003276 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003277 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003278 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003279 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003280 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003281 #endif
Damien429d7192013-10-04 19:53:11 +01003282 }
3283
Damien George6f355fd2014-04-10 14:11:31 +01003284 uint l_end = comp_next_label(comp);
3285 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003286 EMIT_ARG(load_id, qstr_arg);
3287 EMIT_ARG(label_assign, l_top);
3288 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003289 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3290 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003291 EMIT_ARG(jump, l_top);
3292 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003293 EMIT(for_iter_end);
3294
3295 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003296 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003297 }
3298 EMIT(return_value);
3299 } else {
3300 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003301 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3302 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3303 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003304
Damien George36db6bc2014-05-07 17:24:22 +01003305 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003306 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003307 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003308 assert(added);
3309 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003310 }
3311
Damien Georgeb9791222014-01-23 00:34:21 +00003312 EMIT_ARG(load_id, MP_QSTR___name__);
3313 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003314 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 +00003315 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003316
3317 check_for_doc_string(comp, pns->nodes[2]);
3318 compile_node(comp, pns->nodes[2]); // 2 is class body
3319
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003320 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003321 assert(id != NULL);
3322 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003323 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003324 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003325#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003326 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003327#else
Damien George0abb5602015-01-16 12:24:49 +00003328 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003329#endif
Damien429d7192013-10-04 19:53:11 +01003330 }
3331 EMIT(return_value);
3332 }
3333
Damien415eb6f2013-10-05 12:19:06 +01003334 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003335
3336 // make sure we match all the exception levels
3337 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003338}
3339
Damien George094d4502014-04-02 17:31:27 +01003340#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003341// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003342STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003343 comp->pass = pass;
3344 comp->scope_cur = scope;
3345 comp->next_label = 1;
3346
3347 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003348 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003349 return;
3350 }
3351
Damien George36db6bc2014-05-07 17:24:22 +01003352 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003353 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003354 }
3355
Damien826005c2013-10-05 23:17:28 +01003356 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003357 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3358 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3359 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003360
Damiend99b0522013-12-21 18:17:45 +00003361 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003362
Damiena2f2f7d2013-10-06 00:14:13 +01003363 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003364 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003365 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003366 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003367 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003368 if (comp->compile_error != MP_OBJ_NULL) {
3369 goto inline_asm_error;
3370 }
Damiena2f2f7d2013-10-06 00:14:13 +01003371 }
3372
Damiend99b0522013-12-21 18:17:45 +00003373 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003374
Damiend99b0522013-12-21 18:17:45 +00003375 mp_parse_node_t pn_body = pns->nodes[3]; // body
3376 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003377 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003378
Damien Georgecbd2f742014-01-19 11:48:48 +00003379 /*
Damien George36db6bc2014-05-07 17:24:22 +01003380 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003381 //printf("----\n");
3382 scope_print_info(scope);
3383 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003384 */
Damien826005c2013-10-05 23:17:28 +01003385
3386 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003387 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3388 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003389 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3390 // no instructions
3391 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003392 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003393 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003394 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003395 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003396 return;
3397 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003398
3399 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003400 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003401 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3402 goto not_an_instruction;
3403 }
Damiend99b0522013-12-21 18:17:45 +00003404 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003405 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3406 goto not_an_instruction;
3407 }
3408 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3409 goto not_an_instruction;
3410 }
3411 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3412 goto not_an_instruction;
3413 }
Damiend99b0522013-12-21 18:17:45 +00003414 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003415
3416 // parse node looks like an instruction
3417 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003418 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3419 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3420 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003421 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003422
3423 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003424 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003425 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003426 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003427 return;
3428 }
Damien George6f355fd2014-04-10 14:11:31 +01003429 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003430 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003431 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3432 compile_syntax_error(comp, nodes[i], "label redefined");
3433 return;
3434 }
Damien826005c2013-10-05 23:17:28 +01003435 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003436 } else if (op == MP_QSTR_align) {
3437 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003438 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003439 return;
3440 }
Damien George36db6bc2014-05-07 17:24:22 +01003441 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003442 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3443 }
3444 } else if (op == MP_QSTR_data) {
3445 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003446 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003447 return;
3448 }
Damien George36db6bc2014-05-07 17:24:22 +01003449 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003450 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003451 for (uint j = 1; j < n_args; j++) {
3452 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003453 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003454 return;
3455 }
Damien George50912e72015-01-20 11:55:10 +00003456 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003457 }
3458 }
Damien826005c2013-10-05 23:17:28 +01003459 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003460 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003461 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003462 }
3463 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003464
3465 if (comp->compile_error != MP_OBJ_NULL) {
3466 pns = pns2; // this is the parse node that had the error
3467 goto inline_asm_error;
3468 }
Damien826005c2013-10-05 23:17:28 +01003469 }
3470
Damien George36db6bc2014-05-07 17:24:22 +01003471 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003472 EMIT_INLINE_ASM(end_pass);
3473 }
3474
3475 if (comp->compile_error != MP_OBJ_NULL) {
3476 // inline assembler had an error; add traceback to its exception
3477 inline_asm_error:
3478 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 +01003479 }
Damien429d7192013-10-04 19:53:11 +01003480}
Damien George094d4502014-04-02 17:31:27 +01003481#endif
Damien429d7192013-10-04 19:53:11 +01003482
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003483STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003484#if !MICROPY_EMIT_CPYTHON
3485 // in Micro Python we put the *x parameter after all other parameters (except **y)
3486 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3487 id_info_t *id_param = NULL;
3488 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3489 id_info_t *id = &scope->id_info[i];
3490 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3491 if (id_param != NULL) {
3492 // swap star param with last param
3493 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3494 }
3495 break;
3496 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3497 id_param = id;
3498 }
3499 }
3500 }
3501#endif
3502
Damien429d7192013-10-04 19:53:11 +01003503 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003504 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003505 scope->num_locals = 0;
3506 for (int i = 0; i < scope->id_info_len; i++) {
3507 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003508 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003509 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3510 continue;
3511 }
3512 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3513 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3514 }
Damien George2827d622014-04-27 15:50:52 +01003515 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003516 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003517 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003518 }
3519 }
3520
3521 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003522#if MICROPY_EMIT_CPYTHON
3523 int num_cell = 0;
3524#endif
Damien9ecbcff2013-12-11 00:41:43 +00003525 for (int i = 0; i < scope->id_info_len; i++) {
3526 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003527#if MICROPY_EMIT_CPYTHON
3528 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003529 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003530 id->local_num = num_cell;
3531 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003532 }
Damien George6baf76e2013-12-30 22:32:17 +00003533#else
3534 // in Micro Python the cells come right after the fast locals
3535 // parameters are not counted here, since they remain at the start
3536 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003537 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003538 id->local_num = scope->num_locals;
3539 scope->num_locals += 1;
3540 }
3541#endif
Damien9ecbcff2013-12-11 00:41:43 +00003542 }
Damien9ecbcff2013-12-11 00:41:43 +00003543
3544 // compute the index of free vars (freevars[idx] in CPython)
3545 // make sure they are in the order of the parent scope
3546 if (scope->parent != NULL) {
3547 int num_free = 0;
3548 for (int i = 0; i < scope->parent->id_info_len; i++) {
3549 id_info_t *id = &scope->parent->id_info[i];
3550 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3551 for (int j = 0; j < scope->id_info_len; j++) {
3552 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003553 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003554 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003555#if MICROPY_EMIT_CPYTHON
3556 // in CPython the frees are numbered after the cells
3557 id2->local_num = num_cell + num_free;
3558#else
3559 // in Micro Python the frees come first, before the params
3560 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003561#endif
3562 num_free += 1;
3563 }
3564 }
3565 }
Damien429d7192013-10-04 19:53:11 +01003566 }
Damien George6baf76e2013-12-30 22:32:17 +00003567#if !MICROPY_EMIT_CPYTHON
3568 // in Micro Python shift all other locals after the free locals
3569 if (num_free > 0) {
3570 for (int i = 0; i < scope->id_info_len; i++) {
3571 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003572 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003573 id->local_num += num_free;
3574 }
3575 }
Damien George2827d622014-04-27 15:50:52 +01003576 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 +00003577 scope->num_locals += num_free;
3578 }
3579#endif
Damien429d7192013-10-04 19:53:11 +01003580 }
3581
Damien George8725f8f2014-02-15 19:33:11 +00003582 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003583
3584#if MICROPY_EMIT_CPYTHON
3585 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003586 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) {
3587 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003588 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003589 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003590 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 +00003591 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003592 }
3593 }
Damien George882b3632014-04-02 15:56:31 +01003594#endif
3595
Damien429d7192013-10-04 19:53:11 +01003596 int num_free = 0;
3597 for (int i = 0; i < scope->id_info_len; i++) {
3598 id_info_t *id = &scope->id_info[i];
3599 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3600 num_free += 1;
3601 }
3602 }
3603 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003604 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003605 }
3606}
3607
Damien George65cad122014-04-06 11:48:15 +01003608mp_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 +01003609 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003610 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003611 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003612 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003613
Damien George62a3a282015-03-01 12:04:05 +00003614 // create the module scope
3615 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3616
3617 // optimise constants (scope must be set for error messages to work)
3618 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003619 mp_map_t consts;
3620 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003621 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003622 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003623
Damien826005c2013-10-05 23:17:28 +01003624 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003625 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003626 comp->emit_method_table = &emit_pass1_method_table;
Damien Georgea77ffe62015-03-14 12:59:31 +00003627 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003628 comp->emit_inline_asm = NULL;
3629 comp->emit_inline_asm_method_table = NULL;
Damien Georgea77ffe62015-03-14 12:59:31 +00003630 #endif
Damien826005c2013-10-05 23:17:28 +01003631 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003632 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003633 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003634#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003635 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003636 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003637#endif
Damien826005c2013-10-05 23:17:28 +01003638 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003639 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003640 }
3641
3642 // update maximim number of labels needed
3643 if (comp->next_label > max_num_labels) {
3644 max_num_labels = comp->next_label;
3645 }
Damien429d7192013-10-04 19:53:11 +01003646 }
3647
Damien826005c2013-10-05 23:17:28 +01003648 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003649 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 +00003650 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003651 }
3652
Damien826005c2013-10-05 23:17:28 +01003653 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003654 emit_pass1_free(comp->emit);
3655
Damien826005c2013-10-05 23:17:28 +01003656 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003657#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003658 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003659#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003660 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003661#endif
Damien3ef4abb2013-10-12 16:53:13 +01003662#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003663 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003664#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003665#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003666 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003667 if (false) {
3668 // dummy
3669
Damien3ef4abb2013-10-12 16:53:13 +01003670#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003671 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003672 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003673 if (emit_inline_thumb == NULL) {
3674 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3675 }
3676 comp->emit = NULL;
3677 comp->emit_method_table = NULL;
3678 comp->emit_inline_asm = emit_inline_thumb;
3679 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003680 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003681 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003682 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003683 }
Damienc025ebb2013-10-12 14:30:21 +01003684#endif
3685
Damien826005c2013-10-05 23:17:28 +01003686 } else {
Damienc025ebb2013-10-12 14:30:21 +01003687
3688 // choose the emit type
3689
Damien3ef4abb2013-10-12 16:53:13 +01003690#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003691 comp->emit = emit_cpython_new(max_num_labels);
3692 comp->emit_method_table = &emit_cpython_method_table;
3693#else
Damien826005c2013-10-05 23:17:28 +01003694 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003695
3696#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003697 case MP_EMIT_OPT_NATIVE_PYTHON:
3698 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003699#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003700 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003701 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003702 }
Damien13ed3a62013-10-08 09:05:10 +01003703 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003704#elif MICROPY_EMIT_X86
3705 if (emit_native == NULL) {
3706 emit_native = emit_native_x86_new(max_num_labels);
3707 }
3708 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003709#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003710 if (emit_native == NULL) {
3711 emit_native = emit_native_thumb_new(max_num_labels);
3712 }
3713 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003714#elif MICROPY_EMIT_ARM
3715 if (emit_native == NULL) {
3716 emit_native = emit_native_arm_new(max_num_labels);
3717 }
3718 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003719#endif
3720 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003721 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003722 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003723#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003724
Damien826005c2013-10-05 23:17:28 +01003725 default:
3726 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003727 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003728 }
3729 comp->emit = emit_bc;
3730 comp->emit_method_table = &emit_bc_method_table;
3731 break;
3732 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003733#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003734
Damien George1e1779e2015-01-14 00:20:28 +00003735 // need a pass to compute stack size
3736 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3737
Damien George36db6bc2014-05-07 17:24:22 +01003738 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003739 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003740 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3741 }
3742
3743 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003744 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003745 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003746 }
Damien6cdd3af2013-10-05 18:08:26 +01003747 }
Damien429d7192013-10-04 19:53:11 +01003748 }
3749
Damien George41d02b62014-01-24 22:42:28 +00003750 // free the emitters
3751#if !MICROPY_EMIT_CPYTHON
3752 if (emit_bc != NULL) {
3753 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003754 }
Damien George41d02b62014-01-24 22:42:28 +00003755#if MICROPY_EMIT_NATIVE
3756 if (emit_native != NULL) {
3757#if MICROPY_EMIT_X64
3758 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003759#elif MICROPY_EMIT_X86
3760 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003761#elif MICROPY_EMIT_THUMB
3762 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003763#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003764 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003765#endif
3766 }
3767#endif
3768#if MICROPY_EMIT_INLINE_THUMB
3769 if (emit_inline_thumb != NULL) {
3770 emit_inline_thumb_free(emit_inline_thumb);
3771 }
3772#endif
3773#endif // !MICROPY_EMIT_CPYTHON
3774
Damien George52b5d762014-09-23 15:31:56 +00003775 // free the parse tree
Damien George62a3a282015-03-01 12:04:05 +00003776 mp_parse_node_free(module_scope->pn);
Damien George52b5d762014-09-23 15:31:56 +00003777
Damien George41d02b62014-01-24 22:42:28 +00003778 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003779 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003780 for (scope_t *s = module_scope; s;) {
3781 scope_t *next = s->next;
3782 scope_free(s);
3783 s = next;
3784 }
Damien5ac1b2e2013-10-18 19:58:12 +01003785
Damien George41d02b62014-01-24 22:42:28 +00003786 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003787 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003788 m_del_obj(compiler_t, comp);
3789
Damien Georgea91ac202014-10-05 19:01:34 +01003790 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003791 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003792 } else {
3793#if MICROPY_EMIT_CPYTHON
3794 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003795 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003796 return mp_const_true;
3797#else
3798 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003799 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003800#endif
3801 }
Damien429d7192013-10-04 19:53:11 +01003802}