blob: b82fea732189a59f65e9d211358f57bcea422c0e [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/scope.h"
35#include "py/emit.h"
36#include "py/compile.h"
37#include "py/smallint.h"
38#include "py/runtime.h"
39#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010040
41// TODO need to mangle __attr names
42
43typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000044#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000045#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010046#undef DEF_RULE
47 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010048 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000049 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000050 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010051} pn_kind_t;
52
Damien Georgeb9791222014-01-23 00:34:21 +000053#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
54#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
55#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
56#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010057
Damien Georgea91ac202014-10-05 19:01:34 +010058// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010059typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000060 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010061
Damien George78035b92014-04-09 12:27:39 +010062 uint8_t is_repl;
63 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010064 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010065 uint8_t have_star;
66
67 // try to keep compiler clean from nlr
68 // this is set to an exception object if we have a compile error
69 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010070
Damien George6f355fd2014-04-10 14:11:31 +010071 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010072
Damien George69b89d22014-04-11 13:38:30 +000073 uint16_t num_dict_params;
74 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010075
Damien Georgea91ac202014-10-05 19:01:34 +010076 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
77 uint16_t continue_label;
78 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000079 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010080
Damien429d7192013-10-04 19:53:11 +010081 scope_t *scope_head;
82 scope_t *scope_cur;
83
Damien6cdd3af2013-10-05 18:08:26 +010084 emit_t *emit; // current emitter
85 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010086
87 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
88 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010089} compiler_t;
90
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010091STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010092 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
93 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010094 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien Georgea91ac202014-10-05 19:01:34 +010095 mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010096 } else {
Damien Georgea91ac202014-10-05 19:01:34 +010097 // we don't have a line number, so just pass 0
98 mp_obj_exception_add_traceback(exc, comp->source_file, 0, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010099 }
Damien Georgea91ac202014-10-05 19:01:34 +0100100 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000101}
102
Damien Georgeddd1e182015-01-10 14:07:24 +0000103#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100104STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300105 #if MICROPY_PY_UCTYPES
106 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
107 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100108 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100109 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100110};
Damien Georgeddd1e182015-01-10 14:07:24 +0000111STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
112#endif
Damien George57e99eb2014-04-10 22:42:11 +0100113
Damien Georgeffae48d2014-05-08 15:58:39 +0000114// this function is essentially a simple preprocessor
115STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
116 if (0) {
117 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100118#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000119 } else if (MP_PARSE_NODE_IS_ID(pn)) {
120 // lookup identifier in table of dynamic constants
121 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
122 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
123 if (elem != NULL) {
124 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
125 }
126#endif
127 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000128 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100129
Damien Georgeffae48d2014-05-08 15:58:39 +0000130 // fold some parse nodes before folding their arguments
131 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100132#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000133 case PN_expr_stmt:
134 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
135 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
136 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
137 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
138 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
139 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
140 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
141 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
142 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
143 ) {
144 // code to assign dynamic constants: id = const(value)
145
146 // get the id
147 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
148
149 // get the value
150 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
151 pn_value = fold_constants(comp, pn_value, consts);
152 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
153 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
154 break;
155 }
Damien George40f3c022014-07-03 13:25:24 +0100156 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000157
158 // store the value in the table of dynamic constants
159 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
160 if (elem->value != MP_OBJ_NULL) {
161 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
162 break;
163 }
164 elem->value = MP_OBJ_NEW_SMALL_INT(value);
165
166 // replace const(value) with value
167 pns1->nodes[0] = pn_value;
168
169 // finished folding this assignment
170 return pn;
171 }
172 }
173 }
174 break;
175#endif
Damien George5042bce2014-05-25 22:06:06 +0100176 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000177 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000178 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100179 return pn;
Damien429d7192013-10-04 19:53:11 +0100180 }
181
Damien Georgeffae48d2014-05-08 15:58:39 +0000182 // fold arguments
183 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
184 for (int i = 0; i < n; i++) {
185 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
186 }
187
188 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000189 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100190 case PN_atom_paren:
191 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
192 // (int)
193 pn = pns->nodes[0];
194 }
195 break;
196
197 case PN_expr:
198 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
199 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100200 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
201 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100202 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
203 }
204 break;
205
206 case PN_and_expr:
207 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
208 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100209 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
210 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100211 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
212 }
213 break;
214
Damien429d7192013-10-04 19:53:11 +0100215 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000216 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100217 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
218 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000219 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100220 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000221 if (!(arg1 >= (mp_int_t)BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
Damien Georgea26dc502014-04-12 17:54:52 +0100222 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
223 }
Damiend99b0522013-12-21 18:17:45 +0000224 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100225 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000226 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200227 // Shifting to big amounts is underfined behavior
228 // in C and is CPU-dependent; propagate sign bit.
229 arg1 = BITS_PER_WORD - 1;
230 }
Damiend99b0522013-12-21 18:17:45 +0000231 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100232 } else {
233 // shouldn't happen
234 assert(0);
235 }
236 }
237 break;
238
239 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100240 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000241 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100242 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
243 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000244 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100245 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000246 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000247 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100248 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000249 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100250 } else {
251 // shouldn't happen
252 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100253 }
Damien Georged1e355e2014-05-28 14:51:12 +0100254 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100255 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000256 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100257 }
258 }
259 break;
260
261 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000262 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100263 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
264 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000265 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100266 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000267 if (!mp_small_int_mul_overflow(arg0, arg1)) {
268 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100269 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000270 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
271 }
272 }
Damiend99b0522013-12-21 18:17:45 +0000273 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100274 // int / int
275 // pass
Damiend99b0522013-12-21 18:17:45 +0000276 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100277 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000278 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000279 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300280 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100281 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000282 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300283 }
Damien429d7192013-10-04 19:53:11 +0100284 } else {
285 // shouldn't happen
286 assert(0);
287 }
288 }
289 break;
290
291 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000292 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100293 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000294 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100295 // +int
Damiend99b0522013-12-21 18:17:45 +0000296 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
297 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100298 // -int
Damiend99b0522013-12-21 18:17:45 +0000299 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
300 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100301 // ~int
Damiend99b0522013-12-21 18:17:45 +0000302 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100303 } else {
304 // shouldn't happen
305 assert(0);
306 }
307 }
308 break;
309
310 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100311 if (0) {
312#if MICROPY_EMIT_CPYTHON
313 } else if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100314 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100315 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000316 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
317 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200318 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100319 if (power >= 0) {
320 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200321 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100322 for (; power > 0; power--) {
323 ans *= base;
324 }
Damiend99b0522013-12-21 18:17:45 +0000325 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100326 }
327 }
Damien George57e99eb2014-04-10 22:42:11 +0100328#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000329#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100330 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
331 // id.id
332 // look it up in constant table, see if it can be replaced with an integer
333 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
334 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
335 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
336 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
337 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
338 if (elem != NULL) {
339 mp_obj_t dest[2];
340 mp_load_method_maybe(elem->value, q_attr, dest);
341 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100342 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000343 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100344 }
345 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000346#endif
Damien429d7192013-10-04 19:53:11 +0100347 }
348 break;
349 }
350 }
351
352 return pn;
353}
354
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200355STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George6be0b0a2014-08-15 14:30:52 +0100356STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000357STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100358
Damien George6f355fd2014-04-10 14:11:31 +0100359STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100360 return comp->next_label++;
361}
362
Damien George8dcc0c72014-03-27 10:55:21 +0000363STATIC void compile_increase_except_level(compiler_t *comp) {
364 comp->cur_except_level += 1;
365 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
366 comp->scope_cur->exc_stack_size = comp->cur_except_level;
367 }
368}
369
370STATIC void compile_decrease_except_level(compiler_t *comp) {
371 assert(comp->cur_except_level > 0);
372 comp->cur_except_level -= 1;
373}
374
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200375STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgedf8127a2014-04-13 11:04:33 +0100376 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100377 scope->parent = comp->scope_cur;
378 scope->next = NULL;
379 if (comp->scope_head == NULL) {
380 comp->scope_head = scope;
381 } else {
382 scope_t *s = comp->scope_head;
383 while (s->next != NULL) {
384 s = s->next;
385 }
386 s->next = scope;
387 }
388 return scope;
389}
390
Damien George963a5a32015-01-16 17:47:07 +0000391STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
392 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000393 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
394 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100395 for (int i = 0; i < num_nodes; i++) {
396 f(comp, pns->nodes[i]);
397 }
Damiend99b0522013-12-21 18:17:45 +0000398 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100399 f(comp, pn);
400 }
401}
402
Damien George963a5a32015-01-16 17:47:07 +0000403STATIC int list_get(mp_parse_node_t *pn, pn_kind_t pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000404 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100405 *nodes = NULL;
406 return 0;
Damiend99b0522013-12-21 18:17:45 +0000407 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100408 *nodes = pn;
409 return 1;
410 } else {
Damiend99b0522013-12-21 18:17:45 +0000411 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
412 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100413 *nodes = pn;
414 return 1;
415 } else {
416 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000417 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100418 }
419 }
420}
421
Damien George969a6b32014-12-10 22:07:04 +0000422STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000423 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100424 for (int i = 0; i < num_nodes; i++) {
425 compile_node(comp, pns->nodes[i]);
426 }
427}
428
Damien3ef4abb2013-10-12 16:53:13 +0100429#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200430STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100431 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
432 return true;
433 }
Damien George4c81ba82015-01-13 16:21:23 +0000434 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
435 return true;
436 }
Damien George7d414a12015-02-08 01:57:40 +0000437 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
438 return true;
439 }
Damiend99b0522013-12-21 18:17:45 +0000440 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100441 return false;
442 }
Damiend99b0522013-12-21 18:17:45 +0000443 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100444 return false;
445 }
446 return true;
447}
448
Damien George5042bce2014-05-25 22:06:06 +0100449STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000450 bool has_single_quote = false;
451 bool has_double_quote = false;
452 for (int i = 0; i < len; i++) {
453 if (str[i] == '\'') {
454 has_single_quote = true;
455 } else if (str[i] == '"') {
456 has_double_quote = true;
457 }
458 }
459 if (bytes) {
460 vstr_printf(vstr, "b");
461 }
462 bool quote_single = false;
463 if (has_single_quote && !has_double_quote) {
464 vstr_printf(vstr, "\"");
465 } else {
466 quote_single = true;
467 vstr_printf(vstr, "'");
468 }
469 for (int i = 0; i < len; i++) {
470 if (str[i] == '\n') {
471 vstr_printf(vstr, "\\n");
472 } else if (str[i] == '\\') {
473 vstr_printf(vstr, "\\\\");
474 } else if (str[i] == '\'' && quote_single) {
475 vstr_printf(vstr, "\\'");
476 } else {
477 vstr_printf(vstr, "%c", str[i]);
478 }
479 }
480 if (has_single_quote && !has_double_quote) {
481 vstr_printf(vstr, "\"");
482 } else {
483 vstr_printf(vstr, "'");
484 }
485}
486
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200487STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000488 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 +0100489 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000490 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 +0100491 return;
492 }
493
Damien George7d414a12015-02-08 01:57:40 +0000494 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
495 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
496 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
497 return;
498 }
499
Damiend99b0522013-12-21 18:17:45 +0000500 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200501 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
502 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
503 return;
504 }
505
Damien George42f3de92014-10-03 17:44:14 +0000506 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000507 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
508 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100509 case MP_PARSE_NODE_STRING:
510 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100511 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100512 const byte *str = qstr_data(arg, &len);
513 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
514 break;
515 }
Damiend99b0522013-12-21 18:17:45 +0000516 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100517 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000518 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
519 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
520 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100521 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100522 }
523 break;
524 default: assert(0);
525 }
526}
527
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200528STATIC 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 +0100529 int n = 0;
530 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000531 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100532 }
533 int total = n;
534 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000535 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100536 total += 1;
Damien3a205172013-10-12 15:01:56 +0100537 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100538 is_const = false;
539 }
540 }
541 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100542 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100543 is_const = false;
544 break;
545 }
546 }
547 if (total > 0 && is_const) {
548 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000549 vstr_t *vstr = vstr_new();
550 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000551 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000552 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100553 need_comma = true;
554 }
555 for (int i = 0; i < n; i++) {
556 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000557 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100558 }
Damien02f89412013-12-12 15:13:36 +0000559 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100560 need_comma = true;
561 }
562 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000563 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100564 } else {
Damien02f89412013-12-12 15:13:36 +0000565 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100566 }
Damien George0d3cb672015-01-28 23:43:01 +0000567 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000568 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100569 } else {
Damiend99b0522013-12-21 18:17:45 +0000570 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100571 compile_node(comp, pn);
572 }
573 for (int i = 0; i < n; i++) {
574 compile_node(comp, pns_list->nodes[i]);
575 }
Damien Georgeb9791222014-01-23 00:34:21 +0000576 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100577 }
578}
Damien3a205172013-10-12 15:01:56 +0100579#endif
580
581// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100582STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100583#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100584 cpython_c_tuple(comp, pn, pns_list);
585#else
586 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000587 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100588 compile_node(comp, pn);
589 total += 1;
590 }
591 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000592 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100593 for (int i = 0; i < n; i++) {
594 compile_node(comp, pns_list->nodes[i]);
595 }
596 total += n;
597 }
Damien Georgeb9791222014-01-23 00:34:21 +0000598 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100599#endif
600}
Damien429d7192013-10-04 19:53:11 +0100601
Damien George969a6b32014-12-10 22:07:04 +0000602STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100603 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000604 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100605}
606
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200607STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000608 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
609 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100610}
611
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200612STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000613 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
614 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100615}
616
Damien3ef4abb2013-10-12 16:53:13 +0100617#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100618// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200619STATIC 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 +0100620 if (node_is_const_false(pn)) {
621 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000622 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100623 }
624 return;
625 } else if (node_is_const_true(pn)) {
626 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000627 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100628 }
629 return;
Damiend99b0522013-12-21 18:17:45 +0000630 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
631 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
632 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
633 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100634 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100635 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100636 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100637 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100638 }
Damien3a205172013-10-12 15:01:56 +0100639 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000640 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100641 } else {
642 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100643 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100644 }
645 }
646 return;
Damiend99b0522013-12-21 18:17:45 +0000647 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100648 if (jump_if == false) {
649 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100650 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100651 }
652 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100653 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100654 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100655 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100656 }
Damien3a205172013-10-12 15:01:56 +0100657 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000658 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100659 }
660 return;
Damiend99b0522013-12-21 18:17:45 +0000661 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100662 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100663 return;
664 }
665 }
666
667 // nothing special, fall back to default compiling for node and jump
668 compile_node(comp, pn);
669 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000670 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100671 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000672 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100673 }
674}
Damien3a205172013-10-12 15:01:56 +0100675#endif
Damien429d7192013-10-04 19:53:11 +0100676
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200677STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100678#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100679 cpython_c_if_cond(comp, pn, jump_if, label, false);
680#else
681 if (node_is_const_false(pn)) {
682 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000683 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100684 }
685 return;
686 } else if (node_is_const_true(pn)) {
687 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000688 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100689 }
690 return;
Damiend99b0522013-12-21 18:17:45 +0000691 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
692 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
693 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
694 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100695 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100696 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100697 for (int i = 0; i < n - 1; i++) {
698 c_if_cond(comp, pns->nodes[i], true, label2);
699 }
700 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000701 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100702 } else {
703 for (int i = 0; i < n; i++) {
704 c_if_cond(comp, pns->nodes[i], true, label);
705 }
706 }
707 return;
Damiend99b0522013-12-21 18:17:45 +0000708 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100709 if (jump_if == false) {
710 for (int i = 0; i < n; i++) {
711 c_if_cond(comp, pns->nodes[i], false, label);
712 }
713 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100714 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100715 for (int i = 0; i < n - 1; i++) {
716 c_if_cond(comp, pns->nodes[i], false, label2);
717 }
718 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000719 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100720 }
721 return;
Damiend99b0522013-12-21 18:17:45 +0000722 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100723 c_if_cond(comp, pns->nodes[0], !jump_if, label);
724 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100725 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
726 // cond is something in parenthesis
727 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
728 // empty tuple, acts as false for the condition
729 if (jump_if == false) {
730 EMIT_ARG(jump, label);
731 }
732 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
733 // non-empty tuple, acts as true for the condition
734 if (jump_if == true) {
735 EMIT_ARG(jump, label);
736 }
737 } else {
738 // parenthesis around 1 item, is just that item
739 c_if_cond(comp, pns->nodes[0], jump_if, label);
740 }
741 return;
Damien3a205172013-10-12 15:01:56 +0100742 }
743 }
744
745 // nothing special, fall back to default compiling for node and jump
746 compile_node(comp, pn);
747 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000748 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100749 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000750 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100751 }
752#endif
Damien429d7192013-10-04 19:53:11 +0100753}
754
755typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100756STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100757
Damien George6be0b0a2014-08-15 14:30:52 +0100758STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100759 if (assign_kind != ASSIGN_AUG_STORE) {
760 compile_node(comp, pns->nodes[0]);
761 }
762
Damiend99b0522013-12-21 18:17:45 +0000763 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
764 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
765 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
766 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100767 if (assign_kind != ASSIGN_AUG_STORE) {
768 for (int i = 0; i < n - 1; i++) {
769 compile_node(comp, pns1->nodes[i]);
770 }
771 }
Damiend99b0522013-12-21 18:17:45 +0000772 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
773 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100774 }
Damiend99b0522013-12-21 18:17:45 +0000775 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100776 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000777 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100778 if (assign_kind == ASSIGN_AUG_STORE) {
779 EMIT(rot_three);
780 EMIT(store_subscr);
781 } else {
782 compile_node(comp, pns1->nodes[0]);
783 if (assign_kind == ASSIGN_AUG_LOAD) {
784 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100785 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100786 } else {
787 EMIT(store_subscr);
788 }
789 }
Damiend99b0522013-12-21 18:17:45 +0000790 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
791 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100792 if (assign_kind == ASSIGN_AUG_LOAD) {
793 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000794 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100795 } else {
796 if (assign_kind == ASSIGN_AUG_STORE) {
797 EMIT(rot_two);
798 }
Damien Georgeb9791222014-01-23 00:34:21 +0000799 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100800 }
801 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100802 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100803 }
804 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100805 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100806 }
807
Damiend99b0522013-12-21 18:17:45 +0000808 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100809 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100810 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100811
812 return;
813
814cannot_assign:
815 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100816}
817
Damien George0288cf02014-04-11 11:53:00 +0000818// 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 +0100819STATIC 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 +0000820 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
821
822 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000823 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000824 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
825 EMIT_ARG(unpack_ex, 0, num_tail);
826 have_star_index = 0;
827 }
Damien George963a5a32015-01-16 17:47:07 +0000828 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000829 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000830 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000831 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
832 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100833 } else {
Damien George9d181f62014-04-27 16:55:27 +0100834 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100835 return;
836 }
837 }
838 }
Damien George963a5a32015-01-16 17:47:07 +0000839 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000840 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100841 }
Damien George0288cf02014-04-11 11:53:00 +0000842 if (num_head != 0) {
843 if (0 == have_star_index) {
844 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100845 } else {
Damien George0288cf02014-04-11 11:53:00 +0000846 c_assign(comp, node_head, ASSIGN_STORE);
847 }
848 }
Damien George963a5a32015-01-16 17:47:07 +0000849 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000850 if (num_head + i == have_star_index) {
851 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
852 } else {
853 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100854 }
855 }
856}
857
858// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100859STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100860 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000861 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100862 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000863 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
864 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000865 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100866 switch (assign_kind) {
867 case ASSIGN_STORE:
868 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000869 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100870 break;
871 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000872 default:
Damien Georgeb9791222014-01-23 00:34:21 +0000873 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100874 break;
875 }
876 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100877 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100878 return;
879 }
880 } else {
Damiend99b0522013-12-21 18:17:45 +0000881 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
882 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100883 case PN_power:
884 // lhs is an index or attribute
885 c_assign_power(comp, pns, assign_kind);
886 break;
887
888 case PN_testlist_star_expr:
889 case PN_exprlist:
890 // lhs is a tuple
891 if (assign_kind != ASSIGN_STORE) {
892 goto bad_aug;
893 }
Damien George0288cf02014-04-11 11:53:00 +0000894 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100895 break;
896
897 case PN_atom_paren:
898 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000899 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100900 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100901 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000902 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
903 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100904 goto testlist_comp;
905 } else {
906 // parenthesis around 1 item, is just that item
907 pn = pns->nodes[0];
908 goto tail_recursion;
909 }
910 break;
911
912 case PN_atom_bracket:
913 // lhs is something in brackets
914 if (assign_kind != ASSIGN_STORE) {
915 goto bad_aug;
916 }
Damiend99b0522013-12-21 18:17:45 +0000917 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100918 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000919 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000920 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
921 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100922 goto testlist_comp;
923 } else {
924 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000925 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100926 }
927 break;
928
929 default:
Damien George9d181f62014-04-27 16:55:27 +0100930 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100931 }
932 return;
933
934 testlist_comp:
935 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000936 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
937 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
938 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100939 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000940 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000941 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000942 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100943 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000944 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
945 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000946 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100947 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100948 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100949 } else {
950 // sequence with 2 items
951 goto sequence_with_2_items;
952 }
953 } else {
954 // sequence with 2 items
955 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000956 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100957 }
958 return;
959 }
960 return;
961
Damien George9d181f62014-04-27 16:55:27 +0100962 cannot_assign:
963 compile_syntax_error(comp, pn, "can't assign to expression");
964 return;
965
Damien429d7192013-10-04 19:53:11 +0100966 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100967 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100968}
969
970// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100971// if we are not in CPython compatibility mode then:
972// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
973// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
974// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100975STATIC 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 +0100976 assert(n_pos_defaults >= 0);
977 assert(n_kw_defaults >= 0);
978
Damien429d7192013-10-04 19:53:11 +0100979 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000980 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100981 int nfree = 0;
982 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000983 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
984 id_info_t *id = &comp->scope_cur->id_info[i];
985 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
986 for (int j = 0; j < this_scope->id_info_len; j++) {
987 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100988 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000989#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100990 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000991#else
992 // in Micro Python we load closures using LOAD_FAST
Damien George0abb5602015-01-16 12:24:49 +0000993 EMIT_ARG(load_fast, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000994#endif
Damien318aec62013-12-10 18:28:17 +0000995 nfree += 1;
996 }
997 }
Damien429d7192013-10-04 19:53:11 +0100998 }
999 }
1000 }
Damien429d7192013-10-04 19:53:11 +01001001
1002 // make the function/closure
1003 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001004 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001005 } else {
Damien George3558f622014-04-20 17:50:40 +01001006 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001007 }
1008}
1009
Damien George6be0b0a2014-08-15 14:30:52 +01001010STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001011 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001012 comp->have_star = true;
1013 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001014 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1015 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001016 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001017 } else {
1018 // named star
Damien429d7192013-10-04 19:53:11 +01001019 }
Damien George8b19db02014-04-11 23:25:34 +01001020 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001021
1022 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001023 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001024 // TODO do we need to do anything with this?
1025
1026 } else {
1027 mp_parse_node_t pn_id;
1028 mp_parse_node_t pn_colon;
1029 mp_parse_node_t pn_equal;
1030 if (MP_PARSE_NODE_IS_ID(pn)) {
1031 // this parameter is just an id
1032
1033 pn_id = pn;
1034 pn_colon = MP_PARSE_NODE_NULL;
1035 pn_equal = MP_PARSE_NODE_NULL;
1036
1037 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1038 // this parameter has a colon and/or equal specifier
1039
1040 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1041 pn_id = pns->nodes[0];
1042 pn_colon = pns->nodes[1];
1043 pn_equal = pns->nodes[2];
1044
1045 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001046 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001047 assert(0);
1048 return;
1049 }
1050
1051 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1052 // this parameter does not have a default value
1053
1054 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001055 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001056 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001057 return;
1058 }
1059
1060 } else {
1061 // this parameter has a default value
1062 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1063
Damien George8b19db02014-04-11 23:25:34 +01001064 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001065 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001066#if MICROPY_EMIT_CPYTHON
1067 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1068 compile_node(comp, pn_equal);
1069#else
Damien George69b89d22014-04-11 13:38:30 +00001070 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1071 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001072 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1073 // we need to do this here before we start building the map for the default keywords
1074 if (comp->num_default_params > 0) {
1075 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001076 } else {
1077 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001078 }
Damien George69b89d22014-04-11 13:38:30 +00001079 // first default dict param, so make the map
1080 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001081 }
Damien Georgef0778a72014-06-07 22:01:00 +01001082
1083 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001084 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001085 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001086 EMIT(store_map);
1087#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001088 } else {
Damien George69b89d22014-04-11 13:38:30 +00001089 comp->num_default_params += 1;
1090 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001091 }
1092 }
1093
1094 // TODO pn_colon not implemented
1095 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001096 }
1097}
1098
1099// leaves function object on stack
1100// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001101STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001102 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001103 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001104 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001105 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001106 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001107 }
1108
1109 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001110 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001111 uint old_num_dict_params = comp->num_dict_params;
1112 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001113
1114 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001115 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001116 comp->num_dict_params = 0;
1117 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001118 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001119
Damien Georgea91ac202014-10-05 19:01:34 +01001120 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001121 return MP_QSTR_NULL;
1122 }
1123
Damien Georgee337f1e2014-03-31 15:18:37 +01001124#if !MICROPY_EMIT_CPYTHON
1125 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001126 // the default keywords args may have already made the tuple; if not, do it now
1127 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001128 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001129 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001130 }
1131#endif
1132
Damien429d7192013-10-04 19:53:11 +01001133 // get the scope for this function
1134 scope_t *fscope = (scope_t*)pns->nodes[4];
1135
1136 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001137 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001138
1139 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001140 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001141 comp->num_dict_params = old_num_dict_params;
1142 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001143
1144 // return its name (the 'f' in "def f(...):")
1145 return fscope->simple_name;
1146}
1147
1148// leaves class object on stack
1149// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001150STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001151 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001152 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001153 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001154 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001155 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001156 }
1157
1158 EMIT(load_build_class);
1159
1160 // scope for this class
1161 scope_t *cscope = (scope_t*)pns->nodes[3];
1162
1163 // compile the class
1164 close_over_variables_etc(comp, cscope, 0, 0);
1165
1166 // get its name
Damien George968bf342014-04-27 19:12:05 +01001167 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001168
1169 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001170 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1171 mp_parse_node_t parents = pns->nodes[1];
1172 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1173 parents = MP_PARSE_NODE_NULL;
1174 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001175 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001176 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001177
1178 // return its name (the 'C' in class C(...):")
1179 return cscope->simple_name;
1180}
1181
Damien6cdd3af2013-10-05 18:08:26 +01001182// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001183STATIC 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 +00001184 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001185 return false;
1186 }
1187
1188 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001189 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001190 return true;
1191 }
1192
Damiend99b0522013-12-21 18:17:45 +00001193 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001194 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001195 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001196#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001197 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001198 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001199 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001200 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001201#endif
Damien3ef4abb2013-10-12 16:53:13 +01001202#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001203 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001204 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001205#endif
Damien6cdd3af2013-10-05 18:08:26 +01001206 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001207 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001208 }
1209
1210 return true;
1211}
1212
Damien George969a6b32014-12-10 22:07:04 +00001213STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001214 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001215 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001216 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1217
Damien6cdd3af2013-10-05 18:08:26 +01001218 // inherit emit options for this function/class definition
1219 uint emit_options = comp->scope_cur->emit_options;
1220
1221 // compile each decorator
1222 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001223 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001224 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1225 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001226
1227 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001228 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001229 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1230
1231 // check for built-in decorators
1232 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1233 // this was a built-in
1234 num_built_in_decorators += 1;
1235
1236 } else {
1237 // not a built-in, compile normally
1238
1239 // compile the decorator function
1240 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001241 for (int j = 1; j < name_len; j++) {
1242 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1243 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001244 }
1245
1246 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001247 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001248 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001249 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001250 compile_node(comp, pns_decorator->nodes[1]);
1251 }
Damien429d7192013-10-04 19:53:11 +01001252 }
1253 }
1254
1255 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001256 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001257 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001258 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001259 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001260 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001261 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001262 } else {
1263 // shouldn't happen
1264 assert(0);
1265 }
1266
1267 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001268 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001269 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001270 }
1271
1272 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001273 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001274}
1275
Damien George969a6b32014-12-10 22:07:04 +00001276STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001277 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001278 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001279 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001280}
1281
Damien George6be0b0a2014-08-15 14:30:52 +01001282STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001283 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001284 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001285 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1286 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001287
1288 compile_node(comp, pns->nodes[0]); // base of the power node
1289
Damiend99b0522013-12-21 18:17:45 +00001290 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1291 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1292 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1293 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001294 for (int i = 0; i < n - 1; i++) {
1295 compile_node(comp, pns1->nodes[i]);
1296 }
Damiend99b0522013-12-21 18:17:45 +00001297 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1298 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001299 }
Damiend99b0522013-12-21 18:17:45 +00001300 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001301 // can't delete function calls
1302 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001303 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001304 compile_node(comp, pns1->nodes[0]);
1305 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001306 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1307 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001308 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001309 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001310 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001311 }
1312 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001313 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001314 }
1315
Damiend99b0522013-12-21 18:17:45 +00001316 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001317 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001318 }
Damiend99b0522013-12-21 18:17:45 +00001319 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1320 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1321 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1322 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001323 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1324
Damiend99b0522013-12-21 18:17:45 +00001325 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1326 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1327 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001328 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001329 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001330 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001331 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001332 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001333 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001334 c_del_stmt(comp, pns->nodes[0]);
1335 for (int i = 0; i < n; i++) {
1336 c_del_stmt(comp, pns1->nodes[i]);
1337 }
Damiend99b0522013-12-21 18:17:45 +00001338 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001339 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001340 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001341 } else {
1342 // sequence with 2 items
1343 goto sequence_with_2_items;
1344 }
1345 } else {
1346 // sequence with 2 items
1347 sequence_with_2_items:
1348 c_del_stmt(comp, pns->nodes[0]);
1349 c_del_stmt(comp, pns->nodes[1]);
1350 }
1351 } else {
1352 // tuple with 1 element
1353 c_del_stmt(comp, pn);
1354 }
1355 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001356 // TODO is there anything else to implement?
1357 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001358 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001359
1360 return;
1361
1362cannot_delete:
1363 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001364}
1365
Damien George969a6b32014-12-10 22:07:04 +00001366STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001367 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1368}
1369
Damien George969a6b32014-12-10 22:07:04 +00001370STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001371 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001372 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001373 }
Damien George090c9232014-10-17 14:08:49 +00001374 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001375 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001376}
1377
Damien George969a6b32014-12-10 22:07:04 +00001378STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001379 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001380 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001381 }
Damien George090c9232014-10-17 14:08:49 +00001382 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001383 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001384}
1385
Damien George969a6b32014-12-10 22:07:04 +00001386STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001387 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001388 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001389 return;
1390 }
Damiend99b0522013-12-21 18:17:45 +00001391 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001392 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001393 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001394 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001395 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001396 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1397 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 +01001398
Damien George6f355fd2014-04-10 14:11:31 +01001399 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001400 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1401 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1402 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001403 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001404 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1405 } else {
1406 compile_node(comp, pns->nodes[0]);
1407 }
1408 EMIT(return_value);
1409}
1410
Damien George969a6b32014-12-10 22:07:04 +00001411STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001412 compile_node(comp, pns->nodes[0]);
1413 EMIT(pop_top);
1414}
1415
Damien George969a6b32014-12-10 22:07:04 +00001416STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001417 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001418 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001419 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001420 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001421 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001422 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001423 compile_node(comp, pns->nodes[0]);
1424 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001425 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001426 } else {
1427 // raise x
1428 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001429 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001430 }
1431}
1432
Damien George635543c2014-04-10 12:56:52 +01001433// q_base holds the base of the name
1434// eg a -> q_base=a
1435// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001436STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001437 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001438 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1439 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001440 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001441 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001442 pn = pns->nodes[0];
1443 is_as = true;
1444 }
Damien George635543c2014-04-10 12:56:52 +01001445 if (MP_PARSE_NODE_IS_NULL(pn)) {
1446 // empty name (eg, from . import x)
1447 *q_base = MP_QSTR_;
1448 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1449 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001450 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001451 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001452 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001453 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001454 }
Damien George635543c2014-04-10 12:56:52 +01001455 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001456 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1457 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1458 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001459 // a name of the form a.b.c
1460 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001461 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001462 }
Damiend99b0522013-12-21 18:17:45 +00001463 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001464 int len = n - 1;
1465 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001466 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001467 }
Damien George55baff42014-01-21 21:40:13 +00001468 byte *q_ptr;
1469 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001470 for (int i = 0; i < n; i++) {
1471 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001472 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001473 }
Damien George39dc1452014-10-03 19:52:22 +01001474 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001475 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001476 memcpy(str_dest, str_src, str_src_len);
1477 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001478 }
Damien George635543c2014-04-10 12:56:52 +01001479 qstr q_full = qstr_build_end(q_ptr);
1480 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001481 if (is_as) {
1482 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001483 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001484 }
1485 }
1486 } else {
Damien George635543c2014-04-10 12:56:52 +01001487 // shouldn't happen
1488 assert(0);
Damien429d7192013-10-04 19:53:11 +01001489 }
1490 } else {
Damien George635543c2014-04-10 12:56:52 +01001491 // shouldn't happen
1492 assert(0);
Damien429d7192013-10-04 19:53:11 +01001493 }
1494}
1495
Damien George6be0b0a2014-08-15 14:30:52 +01001496STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001497 EMIT_ARG(load_const_small_int, 0); // level 0 import
1498 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1499 qstr q_base;
1500 do_import_name(comp, pn, &q_base);
1501 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001502}
1503
Damien George969a6b32014-12-10 22:07:04 +00001504STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001505 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1506}
1507
Damien George969a6b32014-12-10 22:07:04 +00001508STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001509 mp_parse_node_t pn_import_source = pns->nodes[0];
1510
1511 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1512 uint import_level = 0;
1513 do {
1514 mp_parse_node_t pn_rel;
1515 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)) {
1516 // This covers relative imports with dots only like "from .. import"
1517 pn_rel = pn_import_source;
1518 pn_import_source = MP_PARSE_NODE_NULL;
1519 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1520 // This covers relative imports starting with dot(s) like "from .foo import"
1521 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1522 pn_rel = pns_2b->nodes[0];
1523 pn_import_source = pns_2b->nodes[1];
1524 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1525 } else {
1526 // Not a relative import
1527 break;
1528 }
1529
1530 // get the list of . and/or ...'s
1531 mp_parse_node_t *nodes;
1532 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1533
1534 // count the total number of .'s
1535 for (int i = 0; i < n; i++) {
1536 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1537 import_level++;
1538 } else {
1539 // should be an MP_TOKEN_ELLIPSIS
1540 import_level += 3;
1541 }
1542 }
1543 } while (0);
1544
Damiend99b0522013-12-21 18:17:45 +00001545 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001546 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001547
1548 // build the "fromlist" tuple
1549#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001550 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001551#else
Damien George708c0732014-04-27 19:23:46 +01001552 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001553 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001554#endif
1555
1556 // do the import
Damien George635543c2014-04-10 12:56:52 +01001557 qstr dummy_q;
1558 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001559 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001560
Damien429d7192013-10-04 19:53:11 +01001561 } else {
Damien George635543c2014-04-10 12:56:52 +01001562 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001563
1564 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001565 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001566 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001567#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001568 {
1569 vstr_t *vstr = vstr_new();
1570 vstr_printf(vstr, "(");
1571 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001572 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1573 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1574 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001575 if (i > 0) {
1576 vstr_printf(vstr, ", ");
1577 }
1578 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001579 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001580 const byte *str = qstr_data(id2, &len);
1581 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001582 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001583 }
Damien02f89412013-12-12 15:13:36 +00001584 if (n == 1) {
1585 vstr_printf(vstr, ",");
1586 }
1587 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001588 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001589 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001590 }
Damiendb4c3612013-12-10 17:27:24 +00001591#else
1592 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001593 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1594 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1595 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001596 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001597 }
Damien Georgeb9791222014-01-23 00:34:21 +00001598 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001599#endif
1600
1601 // do the import
Damien George635543c2014-04-10 12:56:52 +01001602 qstr dummy_q;
1603 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001604 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001605 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1606 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1607 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001608 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001609 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001610 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001611 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001612 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001613 }
1614 }
1615 EMIT(pop_top);
1616 }
1617}
1618
Damien George584ba672014-12-21 17:26:45 +00001619STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1620 bool added;
1621 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1622 if (!added) {
1623 compile_syntax_error(comp, pn, "identifier already used");
1624 return;
1625 }
1626 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1627
1628 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1629 id_info = scope_find_global(comp->scope_cur, qst);
1630 if (id_info != NULL) {
1631 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1632 }
1633}
1634
Damien George969a6b32014-12-10 22:07:04 +00001635STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001636 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001637 mp_parse_node_t *nodes;
1638 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1639 for (int i = 0; i < n; i++) {
1640 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001641 }
1642 }
1643}
1644
Damien George584ba672014-12-21 17:26:45 +00001645STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1646 bool added;
1647 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1648 if (!added) {
1649 compile_syntax_error(comp, pn, "identifier already used");
1650 return;
1651 }
1652 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1653 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)) {
1654 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1655 return;
1656 }
1657 id_info->kind = ID_INFO_KIND_FREE;
1658 scope_close_over_in_parents(comp->scope_cur, qst);
1659}
1660
Damien George969a6b32014-12-10 22:07:04 +00001661STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001662 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001663 if (comp->scope_cur->kind == SCOPE_MODULE) {
1664 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1665 return;
1666 }
1667 mp_parse_node_t *nodes;
1668 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1669 for (int i = 0; i < n; i++) {
1670 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001671 }
1672 }
1673}
1674
Damien George969a6b32014-12-10 22:07:04 +00001675STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001676 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001677 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001678 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 +00001679 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001680 // assertion message
1681 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001682 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001683 }
Damien Georgeb9791222014-01-23 00:34:21 +00001684 EMIT_ARG(raise_varargs, 1);
1685 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001686}
1687
Damien George969a6b32014-12-10 22:07:04 +00001688STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001689 // TODO proper and/or short circuiting
1690
Damien George6f355fd2014-04-10 14:11:31 +01001691 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001692
Damien George391db862014-10-17 17:57:33 +00001693 // optimisation: don't emit anything when "if False" (not in CPython)
1694 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1695 uint l_fail = comp_next_label(comp);
1696 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001697
Damien George391db862014-10-17 17:57:33 +00001698 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001699
Damien George391db862014-10-17 17:57:33 +00001700 // optimisation: skip everything else when "if True" (not in CPython)
1701 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1702 goto done;
1703 }
1704
1705 if (
1706 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1707 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1708 // optimisation: don't jump if last instruction was return
1709 && !EMIT(last_emit_was_return_value)
1710 ) {
1711 // jump over elif/else blocks
1712 EMIT_ARG(jump, l_end);
1713 }
1714
1715 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001716 }
1717
Damien George235f9b32014-10-17 17:30:16 +00001718 // compile elif blocks (if any)
1719 mp_parse_node_t *pn_elif;
1720 int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1721 for (int i = 0; i < n_elif; i++) {
1722 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1723 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001724
Damien George391db862014-10-17 17:57:33 +00001725 // optimisation: don't emit anything when "if False" (not in CPython)
1726 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1727 uint l_fail = comp_next_label(comp);
1728 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001729
Damien George391db862014-10-17 17:57:33 +00001730 compile_node(comp, pns_elif->nodes[1]); // elif block
1731
1732 // optimisation: skip everything else when "elif True" (not in CPython)
1733 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1734 goto done;
1735 }
1736
1737 // optimisation: don't jump if last instruction was return
1738 if (!EMIT(last_emit_was_return_value)) {
1739 EMIT_ARG(jump, l_end);
1740 }
1741 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001742 }
1743 }
1744
1745 // compile else block
1746 compile_node(comp, pns->nodes[3]); // can be null
1747
Damien George391db862014-10-17 17:57:33 +00001748done:
Damien Georgeb9791222014-01-23 00:34:21 +00001749 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001750}
1751
Damien Georgecbddb272014-02-01 20:08:18 +00001752#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001753 uint16_t old_break_label = comp->break_label; \
1754 uint16_t old_continue_label = comp->continue_label; \
1755 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001756 uint break_label = comp_next_label(comp); \
1757 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001758 comp->break_label = break_label; \
1759 comp->continue_label = continue_label; \
1760 comp->break_continue_except_level = comp->cur_except_level;
1761
1762#define END_BREAK_CONTINUE_BLOCK \
1763 comp->break_label = old_break_label; \
1764 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001765 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001766
Damien George969a6b32014-12-10 22:07:04 +00001767STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001768 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001769
Damience89a212013-10-15 22:25:17 +01001770 // compared to CPython, we have an optimised version of while loops
1771#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001772 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001773 EMIT_ARG(setup_loop, break_label);
1774 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001775 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1776 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001777 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001778 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001779 }
Damien Georgeb9791222014-01-23 00:34:21 +00001780 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001781 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1782 // this is a small hack to agree with CPython
1783 if (!node_is_const_true(pns->nodes[0])) {
1784 EMIT(pop_block);
1785 }
Damience89a212013-10-15 22:25:17 +01001786#else
Damien George391db862014-10-17 17:57:33 +00001787 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1788 uint top_label = comp_next_label(comp);
1789 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1790 EMIT_ARG(jump, continue_label);
1791 }
1792 EMIT_ARG(label_assign, top_label);
1793 compile_node(comp, pns->nodes[1]); // body
1794 EMIT_ARG(label_assign, continue_label);
1795 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1796 }
Damience89a212013-10-15 22:25:17 +01001797#endif
1798
1799 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001800 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001801
1802 compile_node(comp, pns->nodes[2]); // else
1803
Damien Georgeb9791222014-01-23 00:34:21 +00001804 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001805}
1806
Damien George2ac4af62014-08-15 16:45:41 +01001807#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001808// This function compiles an optimised for-loop of the form:
1809// for <var> in range(<start>, <end>, <step>):
1810// <body>
1811// else:
1812// <else>
1813// <var> must be an identifier and <step> must be a small-int.
1814//
1815// Semantics of for-loop require:
1816// - final failing value should not be stored in the loop variable
1817// - if the loop never runs, the loop variable should never be assigned
1818// - assignments to <var>, <end> or <step> in the body do not alter the loop
1819// (<step> is a constant for us, so no need to worry about it changing)
1820//
1821// If <end> is a small-int, then the stack during the for-loop contains just
1822// the current value of <var>. Otherwise, the stack contains <end> then the
1823// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001824STATIC 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 +00001825 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001826
Damien George6f355fd2014-04-10 14:11:31 +01001827 uint top_label = comp_next_label(comp);
1828 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001829
Damien Georgee181c0d2014-12-12 17:19:56 +00001830 // put the end value on the stack if it's not a small-int constant
1831 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1832 if (end_on_stack) {
1833 compile_node(comp, pn_end);
1834 }
1835
1836 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001837 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001838
Damien Georgeb9791222014-01-23 00:34:21 +00001839 EMIT_ARG(jump, entry_label);
1840 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001841
Damien Georgec33ce602014-12-11 17:35:23 +00001842 // duplicate next value and store it to var
1843 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001844 c_assign(comp, pn_var, ASSIGN_STORE);
1845
Damienf3822fc2013-11-09 20:12:03 +00001846 // compile body
1847 compile_node(comp, pn_body);
1848
Damien Georgeb9791222014-01-23 00:34:21 +00001849 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001850
Damien Georgee181c0d2014-12-12 17:19:56 +00001851 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001852 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001853 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001854
Damien Georgeb9791222014-01-23 00:34:21 +00001855 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001856
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001857 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001858 if (end_on_stack) {
1859 EMIT(dup_top_two);
1860 EMIT(rot_two);
1861 } else {
1862 EMIT(dup_top);
1863 compile_node(comp, pn_end);
1864 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001865 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1866 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001867 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001868 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001869 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001870 }
Damien Georgeb9791222014-01-23 00:34:21 +00001871 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001872
1873 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001874 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001875
1876 compile_node(comp, pn_else);
1877
Damien Georgeb9791222014-01-23 00:34:21 +00001878 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001879
1880 // discard final value of var that failed the loop condition
1881 EMIT(pop_top);
1882
1883 // discard <end> value if it's on the stack
1884 if (end_on_stack) {
1885 EMIT(pop_top);
1886 }
Damienf72fd0e2013-11-06 20:20:49 +00001887}
Damien George2ac4af62014-08-15 16:45:41 +01001888#endif
Damienf72fd0e2013-11-06 20:20:49 +00001889
Damien George969a6b32014-12-10 22:07:04 +00001890STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001891#if !MICROPY_EMIT_CPYTHON
1892 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1893 // this is actually slower, but uses no heap memory
1894 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001895 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 +00001896 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001897 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1898 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1899 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1900 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001901 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1902 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001903 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001904 mp_parse_node_t pn_range_start;
1905 mp_parse_node_t pn_range_end;
1906 mp_parse_node_t pn_range_step;
1907 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001908 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001909 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001910 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001911 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001912 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001913 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001914 } else if (n_args == 2) {
1915 pn_range_start = args[0];
1916 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001917 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001918 } else {
1919 pn_range_start = args[0];
1920 pn_range_end = args[1];
1921 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001922 // We need to know sign of step. This is possible only if it's constant
1923 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1924 optimize = false;
1925 }
Damienf72fd0e2013-11-06 20:20:49 +00001926 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001927 }
1928 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001929 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1930 return;
1931 }
1932 }
1933 }
1934#endif
1935
Damien Georgecbddb272014-02-01 20:08:18 +00001936 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001937 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001938
Damien George6f355fd2014-04-10 14:11:31 +01001939 uint pop_label = comp_next_label(comp);
1940 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001941
Damience89a212013-10-15 22:25:17 +01001942 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1943#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001944 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001945#endif
1946
Damien429d7192013-10-04 19:53:11 +01001947 compile_node(comp, pns->nodes[1]); // iterator
1948 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001949 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001950 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001951 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1952 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001953 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001954 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001955 }
Damien Georgeb9791222014-01-23 00:34:21 +00001956 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001957 EMIT(for_iter_end);
1958
1959 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001960 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001961
Damience89a212013-10-15 22:25:17 +01001962#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001963 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001964#endif
Damien429d7192013-10-04 19:53:11 +01001965
1966 compile_node(comp, pns->nodes[3]); // else (not tested)
1967
Damien Georgeb9791222014-01-23 00:34:21 +00001968 EMIT_ARG(label_assign, break_label);
1969 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001970}
1971
Damien George6be0b0a2014-08-15 14:30:52 +01001972STATIC 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 +01001973 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001974 uint l1 = comp_next_label(comp);
1975 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001976
Damien Georgeb9791222014-01-23 00:34:21 +00001977 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001978 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001979
Damien429d7192013-10-04 19:53:11 +01001980 compile_node(comp, pn_body); // body
1981 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001982 EMIT_ARG(jump, success_label); // jump over exception handler
1983
1984 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001985 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001986
Damien George6f355fd2014-04-10 14:11:31 +01001987 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001988
1989 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001990 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1991 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001992
1993 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001994 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001995
Damiend99b0522013-12-21 18:17:45 +00001996 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001997 // this is a catch all exception handler
1998 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001999 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00002000 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002001 return;
2002 }
2003 } else {
2004 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00002005 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
2006 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
2007 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
2008 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01002009 // handler binds the exception to a local
2010 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002011 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002012 }
2013 }
2014 EMIT(dup_top);
2015 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002016 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00002017 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002018 }
2019
2020 EMIT(pop_top);
2021
2022 if (qstr_exception_local == 0) {
2023 EMIT(pop_top);
2024 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002025 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002026 }
2027
2028 EMIT(pop_top);
2029
Damien George6f355fd2014-04-10 14:11:31 +01002030 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002031 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002032 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002033 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002034 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002035 }
2036 compile_node(comp, pns_except->nodes[1]);
2037 if (qstr_exception_local != 0) {
2038 EMIT(pop_block);
2039 }
2040 EMIT(pop_except);
2041 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002042 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2043 EMIT_ARG(label_assign, l3);
2044 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2045 EMIT_ARG(store_id, qstr_exception_local);
2046 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002047
Damien George8dcc0c72014-03-27 10:55:21 +00002048 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002049 EMIT(end_finally);
2050 }
Damien Georgeb9791222014-01-23 00:34:21 +00002051 EMIT_ARG(jump, l2);
2052 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002053 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002054 }
2055
Damien George8dcc0c72014-03-27 10:55:21 +00002056 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002057 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002058 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002059
Damien Georgeb9791222014-01-23 00:34:21 +00002060 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002061 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002062 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002063}
2064
Damien George6be0b0a2014-08-15 14:30:52 +01002065STATIC 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 +01002066 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002067
Damien Georgeb9791222014-01-23 00:34:21 +00002068 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002069 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002070
Damien429d7192013-10-04 19:53:11 +01002071 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002072 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002073 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002074 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002075 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002076 } else {
2077 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2078 }
2079 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002080 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2081 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002082 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002083
Damien George8dcc0c72014-03-27 10:55:21 +00002084 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002085 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002086}
2087
Damien George969a6b32014-12-10 22:07:04 +00002088STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002089 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2090 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2091 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002092 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002093 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2094 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002095 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002096 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002097 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002098 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002099 // no finally
2100 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2101 } else {
2102 // have finally
Damiend99b0522013-12-21 18:17:45 +00002103 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 +01002104 }
2105 } else {
2106 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002107 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002108 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002109 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002110 }
2111 } else {
2112 // shouldn't happen
2113 assert(0);
2114 }
2115}
2116
Damien George6be0b0a2014-08-15 14:30:52 +01002117STATIC 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 +01002118 if (n == 0) {
2119 // no more pre-bits, compile the body of the with
2120 compile_node(comp, body);
2121 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002122 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002123 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002124 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002125 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002126 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002127 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002128 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2129 } else {
2130 // this pre-bit is just an expression
2131 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002132 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002133 EMIT(pop_top);
2134 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002135 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002136 // compile additional pre-bits and the body
2137 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2138 // finish this with block
2139 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002140 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2141 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002142 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002143 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002144 EMIT(end_finally);
2145 }
2146}
2147
Damien George969a6b32014-12-10 22:07:04 +00002148STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002149 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002150 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002151 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2152 assert(n > 0);
2153
2154 // compile in a nested fashion
2155 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2156}
2157
Damien George969a6b32014-12-10 22:07:04 +00002158STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002159 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002160 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2161 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002162 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002163 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002164 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002165 EMIT(pop_top);
2166
Damien429d7192013-10-04 19:53:11 +01002167 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002168 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002169 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002170 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002171 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2172 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002173 // do nothing with a lonely constant
2174 } else {
2175 compile_node(comp, pns->nodes[0]); // just an expression
2176 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2177 }
Damien429d7192013-10-04 19:53:11 +01002178 }
2179 } else {
Damiend99b0522013-12-21 18:17:45 +00002180 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2181 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002182 if (kind == PN_expr_stmt_augassign) {
2183 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2184 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002185 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002186 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002187 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002188 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2189 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2190 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2191 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2192 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2193 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2194 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2195 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2196 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2197 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2198 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002199 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002200 }
Damien George7e5fb242014-02-01 22:18:47 +00002201 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002202 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2203 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002204 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2205 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002206 // following CPython, we store left-most first
2207 if (rhs > 0) {
2208 EMIT(dup_top);
2209 }
2210 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2211 for (int i = 0; i < rhs; i++) {
2212 if (i + 1 < rhs) {
2213 EMIT(dup_top);
2214 }
Damiend99b0522013-12-21 18:17:45 +00002215 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002216 }
2217 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002218 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002219 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2220 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2221 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002222 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002223 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2224 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002225 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2226 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2227 // can't optimise when it's a star expression on the lhs
2228 goto no_optimisation;
2229 }
Damien429d7192013-10-04 19:53:11 +01002230 compile_node(comp, pns10->nodes[0]); // rhs
2231 compile_node(comp, pns10->nodes[1]); // rhs
2232 EMIT(rot_two);
2233 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2234 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002235 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2236 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2237 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2238 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002239 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002240 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2241 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002242 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2243 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2244 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2245 // can't optimise when it's a star expression on the lhs
2246 goto no_optimisation;
2247 }
Damien429d7192013-10-04 19:53:11 +01002248 compile_node(comp, pns10->nodes[0]); // rhs
2249 compile_node(comp, pns10->nodes[1]); // rhs
2250 compile_node(comp, pns10->nodes[2]); // rhs
2251 EMIT(rot_three);
2252 EMIT(rot_two);
2253 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2254 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2255 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2256 } else {
Damien George495d7812014-04-08 17:51:47 +01002257 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002258 compile_node(comp, pns1->nodes[0]); // rhs
2259 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2260 }
2261 } else {
2262 // shouldn't happen
2263 assert(0);
2264 }
2265 }
2266}
2267
Damien George6be0b0a2014-08-15 14:30:52 +01002268STATIC 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 +00002269 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002270 compile_node(comp, pns->nodes[0]);
2271 for (int i = 1; i < num_nodes; i += 1) {
2272 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002273 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002274 }
2275}
2276
Damien George969a6b32014-12-10 22:07:04 +00002277STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002278 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2279 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002280
Damien George6f355fd2014-04-10 14:11:31 +01002281 uint l_fail = comp_next_label(comp);
2282 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002283 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2284 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002285 EMIT_ARG(jump, l_end);
2286 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002287 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002288 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002289 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002290}
2291
Damien George969a6b32014-12-10 22:07:04 +00002292STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002293 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002294 //mp_parse_node_t pn_params = pns->nodes[0];
2295 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002296
Damien George36db6bc2014-05-07 17:24:22 +01002297 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002298 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002299 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 +01002300 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002301 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002302 }
2303
2304 // get the scope for this lambda
2305 scope_t *this_scope = (scope_t*)pns->nodes[2];
2306
2307 // make the lambda
2308 close_over_variables_etc(comp, this_scope, 0, 0);
2309}
2310
Damien George969a6b32014-12-10 22:07:04 +00002311STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002312 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002313 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002314 for (int i = 0; i < n; i += 1) {
2315 compile_node(comp, pns->nodes[i]);
2316 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002317 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002318 }
2319 }
Damien Georgeb9791222014-01-23 00:34:21 +00002320 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002321}
2322
Damien George969a6b32014-12-10 22:07:04 +00002323STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002324 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002325 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002326 for (int i = 0; i < n; i += 1) {
2327 compile_node(comp, pns->nodes[i]);
2328 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002329 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002330 }
2331 }
Damien Georgeb9791222014-01-23 00:34:21 +00002332 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002333}
2334
Damien George969a6b32014-12-10 22:07:04 +00002335STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002336 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002337 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002338}
2339
Damien George969a6b32014-12-10 22:07:04 +00002340STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002341 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002342 compile_node(comp, pns->nodes[0]);
2343 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002344 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002345 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002346 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002347 }
2348 for (int i = 1; i + 1 < num_nodes; i += 2) {
2349 compile_node(comp, pns->nodes[i + 1]);
2350 if (i + 2 < num_nodes) {
2351 EMIT(dup_top);
2352 EMIT(rot_three);
2353 }
Damien George7e5fb242014-02-01 22:18:47 +00002354 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002355 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002356 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002357 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2358 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2359 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2360 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2361 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2362 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002363 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002364 }
2365 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002366 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2367 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2368 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002369 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002370 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002371 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002372 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002373 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002374 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002375 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002376 }
2377 } else {
2378 // shouldn't happen
2379 assert(0);
2380 }
2381 } else {
2382 // shouldn't happen
2383 assert(0);
2384 }
2385 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002386 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002387 }
2388 }
2389 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002390 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002391 EMIT_ARG(jump, l_end);
2392 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002393 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002394 EMIT(rot_two);
2395 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002396 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002397 }
2398}
2399
Damien George969a6b32014-12-10 22:07:04 +00002400STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002401 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002402}
2403
Damien George969a6b32014-12-10 22:07:04 +00002404STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002405 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002406}
2407
Damien George969a6b32014-12-10 22:07:04 +00002408STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002409 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002410}
2411
Damien George969a6b32014-12-10 22:07:04 +00002412STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002413 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002414}
2415
Damien George969a6b32014-12-10 22:07:04 +00002416STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002417 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002418 compile_node(comp, pns->nodes[0]);
2419 for (int i = 1; i + 1 < num_nodes; i += 2) {
2420 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002421 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002422 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002423 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002424 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002425 } else {
2426 // shouldn't happen
2427 assert(0);
2428 }
2429 }
2430}
2431
Damien George969a6b32014-12-10 22:07:04 +00002432STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002433 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002434 compile_node(comp, pns->nodes[0]);
2435 for (int i = 1; i + 1 < num_nodes; i += 2) {
2436 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002437 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002438 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002439 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002440 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002441 } else {
2442 // shouldn't happen
2443 assert(0);
2444 }
2445 }
2446}
2447
Damien George969a6b32014-12-10 22:07:04 +00002448STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002449 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002450 compile_node(comp, pns->nodes[0]);
2451 for (int i = 1; i + 1 < num_nodes; i += 2) {
2452 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002453 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002454 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002455 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002456 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002457 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002458 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002459 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002460 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002461 } else {
2462 // shouldn't happen
2463 assert(0);
2464 }
2465 }
2466}
2467
Damien George969a6b32014-12-10 22:07:04 +00002468STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002469 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002470 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002471 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002472 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002473 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002474 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002475 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002476 } else {
2477 // shouldn't happen
2478 assert(0);
2479 }
2480}
2481
Damien George969a6b32014-12-10 22:07:04 +00002482STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002483 // this is to handle special super() call
2484 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2485
2486 compile_generic_all_nodes(comp, pns);
2487}
2488
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002489STATIC 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 +01002490 // function to call is on top of stack
2491
Damien George35e2a4e2014-02-05 00:51:47 +00002492#if !MICROPY_EMIT_CPYTHON
2493 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002494 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 +00002495 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002496 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002497 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002498 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002499 // first argument found; load it and call super
Damien George0abb5602015-01-16 12:24:49 +00002500 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002501 EMIT_ARG(call_function, 2, 0, 0);
2502 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002503 }
2504 }
Damien George01039b52014-12-21 17:44:27 +00002505 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002506 return;
2507 }
2508#endif
2509
Damien George2c0842b2014-04-27 16:46:51 +01002510 // get the list of arguments
2511 mp_parse_node_t *args;
2512 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002513
Damien George2c0842b2014-04-27 16:46:51 +01002514 // compile the arguments
2515 // Rather than calling compile_node on the list, we go through the list of args
2516 // explicitly here so that we can count the number of arguments and give sensible
2517 // error messages.
2518 int n_positional = n_positional_extra;
2519 uint n_keyword = 0;
2520 uint star_flags = 0;
2521 for (int i = 0; i < n_args; i++) {
2522 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2523 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2524 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2525 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2526 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2527 return;
2528 }
2529 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2530 compile_node(comp, pns_arg->nodes[0]);
2531 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2532 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2533 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2534 return;
2535 }
2536 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2537 compile_node(comp, pns_arg->nodes[0]);
2538 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2539 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2540 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2541 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2542 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2543 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2544 return;
2545 }
Damien George968bf342014-04-27 19:12:05 +01002546 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002547 compile_node(comp, pns2->nodes[0]);
2548 n_keyword += 1;
2549 } else {
2550 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2551 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2552 n_positional++;
2553 }
2554 } else {
2555 goto normal_argument;
2556 }
2557 } else {
2558 normal_argument:
2559 if (n_keyword > 0) {
2560 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2561 return;
2562 }
2563 compile_node(comp, args[i]);
2564 n_positional++;
2565 }
Damien429d7192013-10-04 19:53:11 +01002566 }
2567
Damien George2c0842b2014-04-27 16:46:51 +01002568 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002569 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002570 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002571 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002572 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002573 }
Damien429d7192013-10-04 19:53:11 +01002574}
2575
Damien George969a6b32014-12-10 22:07:04 +00002576STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002577 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002578 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002579 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 +01002580 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002581 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2582 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002583 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002584 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002585 i += 1;
2586 } else {
2587 compile_node(comp, pns->nodes[i]);
2588 }
Damien George35e2a4e2014-02-05 00:51:47 +00002589 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002590 }
2591}
2592
Damien George969a6b32014-12-10 22:07:04 +00002593STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002594 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002595 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002596}
2597
Damien George969a6b32014-12-10 22:07:04 +00002598STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002599 // a list of strings
Damien63321742013-12-10 17:41:49 +00002600
2601 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002602 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002603 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002604 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002605 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002606 int pn_kind;
2607 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2608 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2609 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2610 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2611 } else {
2612 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2613 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002614 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2615 pn_kind = MP_PARSE_NODE_STRING;
2616 } else {
2617 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2618 pn_kind = MP_PARSE_NODE_BYTES;
2619 }
Damien George40f3c022014-07-03 13:25:24 +01002620 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002621 }
Damien63321742013-12-10 17:41:49 +00002622 if (i == 0) {
2623 string_kind = pn_kind;
2624 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002625 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002626 return;
2627 }
Damien429d7192013-10-04 19:53:11 +01002628 }
Damien63321742013-12-10 17:41:49 +00002629
Damien George65ef6b72015-01-14 21:17:27 +00002630 // if we are not in the last pass, just load a dummy object
2631 if (comp->pass != MP_PASS_EMIT) {
2632 EMIT_ARG(load_const_obj, mp_const_none);
2633 return;
2634 }
2635
Damien63321742013-12-10 17:41:49 +00002636 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002637 vstr_t vstr;
2638 vstr_init_len(&vstr, n_bytes);
2639 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002640 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002641 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002642 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002643 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2644 memcpy(s_dest, s, s_len);
2645 s_dest += s_len;
2646 } else {
2647 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002648 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2649 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002650 }
Damien63321742013-12-10 17:41:49 +00002651 }
Damien George65ef6b72015-01-14 21:17:27 +00002652
2653 // load the object
Damien George05005f62015-01-21 22:48:37 +00002654 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 +01002655}
2656
2657// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002658STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002659 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2660 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2661 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002662
Damien George36db6bc2014-05-07 17:24:22 +01002663 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002664 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002665 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 +01002666 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002667 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002668 }
2669
2670 // get the scope for this comprehension
2671 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2672
2673 // compile the comprehension
2674 close_over_variables_etc(comp, this_scope, 0, 0);
2675
2676 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2677 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002678 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002679}
2680
Damien George969a6b32014-12-10 22:07:04 +00002681STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002682 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002683 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002684 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2685 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2686 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2687 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2688 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2689 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2690 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002691 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002692 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002693 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002694 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002695 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002696 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002697 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002698 // generator expression
2699 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2700 } else {
2701 // tuple with 2 items
2702 goto tuple_with_2_items;
2703 }
2704 } else {
2705 // tuple with 2 items
2706 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002707 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002708 }
2709 } else {
2710 // parenthesis around a single item, is just that item
2711 compile_node(comp, pns->nodes[0]);
2712 }
2713}
2714
Damien George969a6b32014-12-10 22:07:04 +00002715STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002716 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002717 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002718 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002719 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2720 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2721 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2722 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2723 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002724 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002725 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002726 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002727 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002728 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002729 // list of many items
2730 compile_node(comp, pns2->nodes[0]);
2731 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002732 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002733 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002734 // list comprehension
2735 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2736 } else {
2737 // list with 2 items
2738 goto list_with_2_items;
2739 }
2740 } else {
2741 // list with 2 items
2742 list_with_2_items:
2743 compile_node(comp, pns2->nodes[0]);
2744 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002745 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002746 }
2747 } else {
2748 // list with 1 item
2749 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002750 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002751 }
2752}
2753
Damien George969a6b32014-12-10 22:07:04 +00002754STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002755 mp_parse_node_t pn = pns->nodes[0];
2756 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002757 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002758 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002759 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2760 pns = (mp_parse_node_struct_t*)pn;
2761 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002762 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002763 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002764 compile_node(comp, pn);
2765 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002766 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2767 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2768 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2769 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002770 // dict/set with multiple elements
2771
2772 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002773 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002774 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2775
2776 // first element sets whether it's a dict or set
2777 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002778 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002779 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002780 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002781 compile_node(comp, pns->nodes[0]);
2782 EMIT(store_map);
2783 is_dict = true;
2784 } else {
2785 // a set
2786 compile_node(comp, pns->nodes[0]); // 1st value of set
2787 is_dict = false;
2788 }
2789
2790 // process rest of elements
2791 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002792 mp_parse_node_t pn_i = nodes[i];
2793 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2794 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002795 if (is_dict) {
2796 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002797 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002798 return;
2799 }
2800 EMIT(store_map);
2801 } else {
2802 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002803 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002804 return;
2805 }
2806 }
2807 }
2808
Damien Georgee37dcaa2014-12-27 17:07:16 +00002809 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002810 // if it's a set, build it
2811 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002812 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002813 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002814 #endif
Damiend99b0522013-12-21 18:17:45 +00002815 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002816 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002817 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002818 // a dictionary comprehension
2819 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2820 } else {
2821 // a set comprehension
2822 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2823 }
2824 } else {
2825 // shouldn't happen
2826 assert(0);
2827 }
2828 } else {
2829 // set with one element
2830 goto set_with_one_element;
2831 }
2832 } else {
2833 // set with one element
2834 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002835 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002836 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002837 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002838 #else
2839 assert(0);
2840 #endif
Damien429d7192013-10-04 19:53:11 +01002841 }
2842}
2843
Damien George969a6b32014-12-10 22:07:04 +00002844STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002845 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002846}
2847
Damien George969a6b32014-12-10 22:07:04 +00002848STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002849 // object who's index we want is on top of stack
2850 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002851 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002852}
2853
Damien George969a6b32014-12-10 22:07:04 +00002854STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002855 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002857}
2858
Damien George83204f32014-12-27 17:20:41 +00002859#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002860STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002861 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2862 mp_parse_node_t pn = pns->nodes[0];
2863 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002864 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002865 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2866 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002867 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2868 pns = (mp_parse_node_struct_t*)pn;
2869 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002870 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002871 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002872 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002873 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002874 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002875 } else {
2876 // [?::x]
2877 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002878 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002879 }
Damiend99b0522013-12-21 18:17:45 +00002880 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002881 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002882 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2883 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2884 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2885 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002886 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002887 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002888 } else {
2889 // [?:x:x]
2890 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002892 }
2893 } else {
2894 // [?:x]
2895 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002896 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002897 }
2898 } else {
2899 // [?:x]
2900 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002901 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002902 }
2903}
2904
Damien George969a6b32014-12-10 22:07:04 +00002905STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002906 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002907 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2908 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002909}
2910
Damien George969a6b32014-12-10 22:07:04 +00002911STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002912 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002913 compile_subscript_3_helper(comp, pns);
2914}
Damien George83204f32014-12-27 17:20:41 +00002915#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002916
Damien George969a6b32014-12-10 22:07:04 +00002917STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002918 // if this is called then we are compiling a dict key:value pair
2919 compile_node(comp, pns->nodes[1]); // value
2920 compile_node(comp, pns->nodes[0]); // key
2921}
2922
Damien George969a6b32014-12-10 22:07:04 +00002923STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002924 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002925 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002926 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002927}
2928
Damien George969a6b32014-12-10 22:07:04 +00002929STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002930 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002931 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002932 return;
2933 }
Damiend99b0522013-12-21 18:17:45 +00002934 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002935 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002936 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002937 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2938 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002939 compile_node(comp, pns->nodes[0]);
2940 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002941 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002942 EMIT(yield_from);
2943 } else {
2944 compile_node(comp, pns->nodes[0]);
2945 EMIT(yield_value);
2946 }
2947}
2948
Damien George65ef6b72015-01-14 21:17:27 +00002949STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2950 // only create and load the actual str object on the last pass
2951 if (comp->pass != MP_PASS_EMIT) {
2952 EMIT_ARG(load_const_obj, mp_const_none);
2953 } else {
2954 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2955 }
2956}
2957
2958STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2959 // only create and load the actual bytes object on the last pass
2960 if (comp->pass != MP_PASS_EMIT) {
2961 EMIT_ARG(load_const_obj, mp_const_none);
2962 } else {
2963 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2964 }
2965}
2966
Damien George7d414a12015-02-08 01:57:40 +00002967STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2968 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2969}
2970
Damiend99b0522013-12-21 18:17:45 +00002971typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002972STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002973#define nc NULL
2974#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002975#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002976#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002977#undef nc
2978#undef c
2979#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002980 NULL,
2981 compile_string,
2982 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002983 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002984};
2985
Damien George6be0b0a2014-08-15 14:30:52 +01002986STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002987 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002988 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002989 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002990 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002991 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002992 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002993 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002994 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002995 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002996 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2997 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damien Georged2d64f02015-01-14 21:32:42 +00002998 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002999 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01003000 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01003001 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01003002 // do nothing
3003 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00003004 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01003005 }
3006 break;
Damien429d7192013-10-04 19:53:11 +01003007 }
3008 } else {
Damiend99b0522013-12-21 18:17:45 +00003009 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00003010 EMIT_ARG(set_line_number, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00003011 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
3012 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01003013#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00003014 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
3015 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01003016#endif
Damien George65ef6b72015-01-14 21:17:27 +00003017 compile_syntax_error(comp, pn, "internal compiler error");
3018 } else {
3019 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01003020 }
3021 }
3022}
3023
Damien George2ac4af62014-08-15 16:45:41 +01003024STATIC 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 +01003025 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01003026 qstr param_name = MP_QSTR_NULL;
3027 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003028 if (MP_PARSE_NODE_IS_ID(pn)) {
3029 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01003030 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003031 // comes after a star, so counts as a keyword-only parameter
3032 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01003033 } else {
Damien George2827d622014-04-27 15:50:52 +01003034 // comes before a star, so counts as a positional parameter
3035 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003036 }
Damienb14de212013-10-06 00:28:28 +01003037 } else {
Damiend99b0522013-12-21 18:17:45 +00003038 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3039 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3040 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3041 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003042 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003043 // comes after a star, so counts as a keyword-only parameter
3044 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003045 } else {
Damien George2827d622014-04-27 15:50:52 +01003046 // comes before a star, so counts as a positional parameter
3047 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003048 }
Damiend99b0522013-12-21 18:17:45 +00003049 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003050 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003051 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003052 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003053 // bare star
3054 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003055 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003056 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003057 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003058 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003059 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01003060 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01003061 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003062 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003063 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3064 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003065 } else {
3066 // shouldn't happen
3067 assert(0);
3068 }
Damiend99b0522013-12-21 18:17:45 +00003069 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
3070 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003071 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003072 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003073 } else {
Damienb14de212013-10-06 00:28:28 +01003074 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01003075 assert(0);
3076 }
Damien429d7192013-10-04 19:53:11 +01003077 }
3078
Damien George2827d622014-04-27 15:50:52 +01003079 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003080 bool added;
3081 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3082 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003083 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003084 return;
3085 }
Damien429d7192013-10-04 19:53:11 +01003086 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003087 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003088 }
3089}
3090
Damien George2827d622014-04-27 15:50:52 +01003091STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003092 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003093}
3094
Damien George2827d622014-04-27 15:50:52 +01003095STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003096 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3097}
3098
3099STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3100 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3101 // no annotation
3102 return;
3103 }
3104
3105 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3106 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3107 // named parameter with possible annotation
3108 // fallthrough
3109 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3110 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3111 // named star with possible annotation
3112 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3113 // fallthrough
3114 } else {
3115 // no annotation
3116 return;
3117 }
3118 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3119 // double star with possible annotation
3120 // fallthrough
3121 } else {
3122 // no annotation
3123 return;
3124 }
3125
3126 mp_parse_node_t pn_annotation = pns->nodes[1];
3127
3128 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3129 #if MICROPY_EMIT_NATIVE
3130 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3131 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3132 assert(id_info != NULL);
3133
3134 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3135 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3136 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3137 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3138 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003139 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003140 }
3141 }
3142 #endif // MICROPY_EMIT_NATIVE
3143 }
Damien429d7192013-10-04 19:53:11 +01003144}
3145
Damien George6be0b0a2014-08-15 14:30:52 +01003146STATIC 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 +01003147 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003148 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003149 // no more nested if/for; compile inner expression
3150 compile_node(comp, pn_inner_expr);
3151 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003152 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003153 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003154 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003155 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003156 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003157 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003158 #endif
Damien429d7192013-10-04 19:53:11 +01003159 } else {
3160 EMIT(yield_value);
3161 EMIT(pop_top);
3162 }
Damiend99b0522013-12-21 18:17:45 +00003163 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003164 // if condition
Damiend99b0522013-12-21 18:17:45 +00003165 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003166 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3167 pn_iter = pns_comp_if->nodes[1];
3168 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003169 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003170 // for loop
Damiend99b0522013-12-21 18:17:45 +00003171 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003172 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003173 uint l_end2 = comp_next_label(comp);
3174 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003175 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003176 EMIT_ARG(label_assign, l_top2);
3177 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003178 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3179 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 +00003180 EMIT_ARG(jump, l_top2);
3181 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003182 EMIT(for_iter_end);
3183 } else {
3184 // shouldn't happen
3185 assert(0);
3186 }
3187}
3188
Damien George1463c1f2014-04-25 23:52:57 +01003189STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3190#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003191 // see http://www.python.org/dev/peps/pep-0257/
3192
3193 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003194 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003195 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003196 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003197 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003198 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3199 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003200 for (int i = 0; i < num_nodes; i++) {
3201 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003202 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 +00003203 // not a newline, so this is the first statement; finish search
3204 break;
3205 }
3206 }
3207 // 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 +00003208 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003209 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003210 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003211 } else {
3212 return;
3213 }
3214
3215 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003216 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3217 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003218 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3219 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3220 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3221 // compile the doc string
3222 compile_node(comp, pns->nodes[0]);
3223 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003224 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003225 }
3226 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003227#else
3228 (void)comp;
3229 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003230#endif
Damien429d7192013-10-04 19:53:11 +01003231}
3232
Damien George1463c1f2014-04-25 23:52:57 +01003233STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003234 comp->pass = pass;
3235 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003236 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003237 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003238
Damien George36db6bc2014-05-07 17:24:22 +01003239 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003240 // reset maximum stack sizes in scope
3241 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003242 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003243 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003244 }
3245
Damien5ac1b2e2013-10-18 19:58:12 +01003246#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003247 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003248 scope_print_info(scope);
3249 }
Damien5ac1b2e2013-10-18 19:58:12 +01003250#endif
Damien429d7192013-10-04 19:53:11 +01003251
3252 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003253 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3254 assert(scope->kind == SCOPE_MODULE);
3255 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3256 compile_node(comp, pns->nodes[0]); // compile the expression
3257 EMIT(return_value);
3258 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003259 if (!comp->is_repl) {
3260 check_for_doc_string(comp, scope->pn);
3261 }
Damien429d7192013-10-04 19:53:11 +01003262 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003263 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003264 EMIT(return_value);
3265 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003266 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3267 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3268 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003269
3270 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003271 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003272 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003273 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003274 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003275 } else {
3276 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003277
Damien George2ac4af62014-08-15 16:45:41 +01003278 // argument annotations
3279 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3280
3281 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003282 mp_parse_node_t pn_annotation = pns->nodes[2];
3283 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3284 #if MICROPY_EMIT_NATIVE
3285 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3286 // nodes[2] can be null or a test-expr
3287 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3288 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3289 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3290 } else {
3291 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3292 }
Damien George2ac4af62014-08-15 16:45:41 +01003293 }
Damien Georgea5190a72014-08-15 22:39:08 +01003294 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003295 }
Damien George2ac4af62014-08-15 16:45:41 +01003296 }
Damien429d7192013-10-04 19:53:11 +01003297
3298 compile_node(comp, pns->nodes[3]); // 3 is function body
3299 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003300 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003301 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003302 EMIT(return_value);
3303 }
3304 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003305 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3306 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3307 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003308
3309 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003310 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003311 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003312 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003313 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3314 }
3315
3316 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003317
3318 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3319 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3320 EMIT(pop_top);
3321 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3322 }
Damien429d7192013-10-04 19:53:11 +01003323 EMIT(return_value);
3324 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3325 // a bit of a hack at the moment
3326
Damiend99b0522013-12-21 18:17:45 +00003327 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3328 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3329 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3330 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3331 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003332
Damien George708c0732014-04-27 19:23:46 +01003333 // We need a unique name for the comprehension argument (the iterator).
3334 // CPython uses .0, but we should be able to use anything that won't
3335 // clash with a user defined variable. Best to use an existing qstr,
3336 // so we use the blank qstr.
3337#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003338 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003339#else
3340 qstr qstr_arg = MP_QSTR_;
3341#endif
Damien George36db6bc2014-05-07 17:24:22 +01003342 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003343 bool added;
3344 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3345 assert(added);
3346 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003347 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003348 }
3349
3350 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003351 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003352 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003353 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003354 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003355 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003356 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003357 #endif
Damien429d7192013-10-04 19:53:11 +01003358 }
3359
Damien George6f355fd2014-04-10 14:11:31 +01003360 uint l_end = comp_next_label(comp);
3361 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003362 EMIT_ARG(load_id, qstr_arg);
3363 EMIT_ARG(label_assign, l_top);
3364 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003365 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3366 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003367 EMIT_ARG(jump, l_top);
3368 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003369 EMIT(for_iter_end);
3370
3371 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003372 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003373 }
3374 EMIT(return_value);
3375 } else {
3376 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003377 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3378 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3379 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003380
Damien George36db6bc2014-05-07 17:24:22 +01003381 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003382 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003383 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003384 assert(added);
3385 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003386 }
3387
Damien Georgeb9791222014-01-23 00:34:21 +00003388 EMIT_ARG(load_id, MP_QSTR___name__);
3389 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003390 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 +00003391 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003392
3393 check_for_doc_string(comp, pns->nodes[2]);
3394 compile_node(comp, pns->nodes[2]); // 2 is class body
3395
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003396 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003397 assert(id != NULL);
3398 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003399 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003400 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003401#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003402 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003403#else
Damien George0abb5602015-01-16 12:24:49 +00003404 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003405#endif
Damien429d7192013-10-04 19:53:11 +01003406 }
3407 EMIT(return_value);
3408 }
3409
Damien415eb6f2013-10-05 12:19:06 +01003410 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003411
3412 // make sure we match all the exception levels
3413 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003414}
3415
Damien George094d4502014-04-02 17:31:27 +01003416#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003417// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003418STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003419 comp->pass = pass;
3420 comp->scope_cur = scope;
3421 comp->next_label = 1;
3422
3423 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003424 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003425 return;
3426 }
3427
Damien George36db6bc2014-05-07 17:24:22 +01003428 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003429 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003430 }
3431
Damien826005c2013-10-05 23:17:28 +01003432 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003433 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3434 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3435 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003436
Damiend99b0522013-12-21 18:17:45 +00003437 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003438
Damiena2f2f7d2013-10-06 00:14:13 +01003439 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003440 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003441 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003442 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003443 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003444 }
3445
Damiend99b0522013-12-21 18:17:45 +00003446 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003447
Damiend99b0522013-12-21 18:17:45 +00003448 mp_parse_node_t pn_body = pns->nodes[3]; // body
3449 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003450 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3451
Damien Georgecbd2f742014-01-19 11:48:48 +00003452 /*
Damien George36db6bc2014-05-07 17:24:22 +01003453 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003454 //printf("----\n");
3455 scope_print_info(scope);
3456 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003457 */
Damien826005c2013-10-05 23:17:28 +01003458
3459 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003460 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3461 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003462 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3463 // no instructions
3464 continue;
3465 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3466 // an instruction; fall through
3467 } else {
3468 // not an instruction; error
3469 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3470 return;
3471 }
Damiend99b0522013-12-21 18:17:45 +00003472 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3473 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3474 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3475 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3476 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3477 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3478 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3479 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3480 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3481 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003482 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3483
3484 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003485 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003486 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003487 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003488 return;
3489 }
Damien George6f355fd2014-04-10 14:11:31 +01003490 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003491 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003492 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003493 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003494 } else if (op == MP_QSTR_align) {
3495 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3496 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3497 return;
3498 }
Damien George36db6bc2014-05-07 17:24:22 +01003499 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003500 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3501 }
3502 } else if (op == MP_QSTR_data) {
3503 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3504 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3505 return;
3506 }
Damien George36db6bc2014-05-07 17:24:22 +01003507 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003508 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003509 for (uint j = 1; j < n_args; j++) {
3510 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003511 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3512 return;
3513 }
Damien George50912e72015-01-20 11:55:10 +00003514 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003515 }
3516 }
Damien826005c2013-10-05 23:17:28 +01003517 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003518 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003519 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003520 }
3521 }
3522 }
3523
Damien George36db6bc2014-05-07 17:24:22 +01003524 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003525 bool success = EMIT_INLINE_ASM(end_pass);
3526 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003527 // TODO get proper exception from inline assembler
3528 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003529 }
Damienb05d7072013-10-05 13:37:10 +01003530 }
Damien429d7192013-10-04 19:53:11 +01003531}
Damien George094d4502014-04-02 17:31:27 +01003532#endif
Damien429d7192013-10-04 19:53:11 +01003533
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003534STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003535#if !MICROPY_EMIT_CPYTHON
3536 // in Micro Python we put the *x parameter after all other parameters (except **y)
3537 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3538 id_info_t *id_param = NULL;
3539 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3540 id_info_t *id = &scope->id_info[i];
3541 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3542 if (id_param != NULL) {
3543 // swap star param with last param
3544 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3545 }
3546 break;
3547 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3548 id_param = id;
3549 }
3550 }
3551 }
3552#endif
3553
Damien429d7192013-10-04 19:53:11 +01003554 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003555 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003556 scope->num_locals = 0;
3557 for (int i = 0; i < scope->id_info_len; i++) {
3558 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003559 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003560 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3561 continue;
3562 }
3563 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3564 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3565 }
Damien George2827d622014-04-27 15:50:52 +01003566 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003567 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003568 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003569 }
3570 }
3571
3572 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003573#if MICROPY_EMIT_CPYTHON
3574 int num_cell = 0;
3575#endif
Damien9ecbcff2013-12-11 00:41:43 +00003576 for (int i = 0; i < scope->id_info_len; i++) {
3577 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003578#if MICROPY_EMIT_CPYTHON
3579 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003580 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003581 id->local_num = num_cell;
3582 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003583 }
Damien George6baf76e2013-12-30 22:32:17 +00003584#else
3585 // in Micro Python the cells come right after the fast locals
3586 // parameters are not counted here, since they remain at the start
3587 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003588 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003589 id->local_num = scope->num_locals;
3590 scope->num_locals += 1;
3591 }
3592#endif
Damien9ecbcff2013-12-11 00:41:43 +00003593 }
Damien9ecbcff2013-12-11 00:41:43 +00003594
3595 // compute the index of free vars (freevars[idx] in CPython)
3596 // make sure they are in the order of the parent scope
3597 if (scope->parent != NULL) {
3598 int num_free = 0;
3599 for (int i = 0; i < scope->parent->id_info_len; i++) {
3600 id_info_t *id = &scope->parent->id_info[i];
3601 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3602 for (int j = 0; j < scope->id_info_len; j++) {
3603 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003604 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003605 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003606#if MICROPY_EMIT_CPYTHON
3607 // in CPython the frees are numbered after the cells
3608 id2->local_num = num_cell + num_free;
3609#else
3610 // in Micro Python the frees come first, before the params
3611 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003612#endif
3613 num_free += 1;
3614 }
3615 }
3616 }
Damien429d7192013-10-04 19:53:11 +01003617 }
Damien George6baf76e2013-12-30 22:32:17 +00003618#if !MICROPY_EMIT_CPYTHON
3619 // in Micro Python shift all other locals after the free locals
3620 if (num_free > 0) {
3621 for (int i = 0; i < scope->id_info_len; i++) {
3622 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003623 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003624 id->local_num += num_free;
3625 }
3626 }
Damien George2827d622014-04-27 15:50:52 +01003627 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 +00003628 scope->num_locals += num_free;
3629 }
3630#endif
Damien429d7192013-10-04 19:53:11 +01003631 }
3632
Damien George8725f8f2014-02-15 19:33:11 +00003633 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003634
3635#if MICROPY_EMIT_CPYTHON
3636 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003637 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) {
3638 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003639 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003640 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003641 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 +00003642 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003643 }
3644 }
Damien George882b3632014-04-02 15:56:31 +01003645#endif
3646
Damien429d7192013-10-04 19:53:11 +01003647 int num_free = 0;
3648 for (int i = 0; i < scope->id_info_len; i++) {
3649 id_info_t *id = &scope->id_info[i];
3650 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3651 num_free += 1;
3652 }
3653 }
3654 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003655 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003656 }
3657}
3658
Damien George65cad122014-04-06 11:48:15 +01003659mp_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 +01003660 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003661 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003662 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003663 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003664
Damien826005c2013-10-05 23:17:28 +01003665 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003666 mp_map_t consts;
3667 mp_map_init(&consts, 0);
3668 pn = fold_constants(comp, pn, &consts);
3669 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003670
3671 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003672 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003673
Damien826005c2013-10-05 23:17:28 +01003674 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003675 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003676 comp->emit_method_table = &emit_pass1_method_table;
3677 comp->emit_inline_asm = NULL;
3678 comp->emit_inline_asm_method_table = NULL;
3679 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003680 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003681 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003682#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003683 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003684 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003685#endif
Damien826005c2013-10-05 23:17:28 +01003686 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003687 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003688 }
3689
3690 // update maximim number of labels needed
3691 if (comp->next_label > max_num_labels) {
3692 max_num_labels = comp->next_label;
3693 }
Damien429d7192013-10-04 19:53:11 +01003694 }
3695
Damien826005c2013-10-05 23:17:28 +01003696 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003697 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 +00003698 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003699 }
3700
Damien826005c2013-10-05 23:17:28 +01003701 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003702 emit_pass1_free(comp->emit);
3703
Damien826005c2013-10-05 23:17:28 +01003704 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003705#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003706 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003707#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003708 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003709#endif
Damien3ef4abb2013-10-12 16:53:13 +01003710#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003711 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003712#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003713#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003714 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003715 if (false) {
3716 // dummy
3717
Damien3ef4abb2013-10-12 16:53:13 +01003718#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003719 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003720 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003721 if (emit_inline_thumb == NULL) {
3722 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3723 }
3724 comp->emit = NULL;
3725 comp->emit_method_table = NULL;
3726 comp->emit_inline_asm = emit_inline_thumb;
3727 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003728 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003729 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003730 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003731 }
Damienc025ebb2013-10-12 14:30:21 +01003732#endif
3733
Damien826005c2013-10-05 23:17:28 +01003734 } else {
Damienc025ebb2013-10-12 14:30:21 +01003735
3736 // choose the emit type
3737
Damien3ef4abb2013-10-12 16:53:13 +01003738#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003739 comp->emit = emit_cpython_new(max_num_labels);
3740 comp->emit_method_table = &emit_cpython_method_table;
3741#else
Damien826005c2013-10-05 23:17:28 +01003742 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003743
3744#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003745 case MP_EMIT_OPT_NATIVE_PYTHON:
3746 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003747#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003748 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003749 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003750 }
Damien13ed3a62013-10-08 09:05:10 +01003751 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003752#elif MICROPY_EMIT_X86
3753 if (emit_native == NULL) {
3754 emit_native = emit_native_x86_new(max_num_labels);
3755 }
3756 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003757#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003758 if (emit_native == NULL) {
3759 emit_native = emit_native_thumb_new(max_num_labels);
3760 }
3761 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003762#elif MICROPY_EMIT_ARM
3763 if (emit_native == NULL) {
3764 emit_native = emit_native_arm_new(max_num_labels);
3765 }
3766 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003767#endif
3768 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003769 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003770 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003771#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003772
Damien826005c2013-10-05 23:17:28 +01003773 default:
3774 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003775 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003776 }
3777 comp->emit = emit_bc;
3778 comp->emit_method_table = &emit_bc_method_table;
3779 break;
3780 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003781#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003782
Damien George1e1779e2015-01-14 00:20:28 +00003783 // need a pass to compute stack size
3784 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3785
Damien George36db6bc2014-05-07 17:24:22 +01003786 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003787 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003788 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3789 }
3790
3791 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003792 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003793 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003794 }
Damien6cdd3af2013-10-05 18:08:26 +01003795 }
Damien429d7192013-10-04 19:53:11 +01003796 }
3797
Damien George41d02b62014-01-24 22:42:28 +00003798 // free the emitters
3799#if !MICROPY_EMIT_CPYTHON
3800 if (emit_bc != NULL) {
3801 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003802 }
Damien George41d02b62014-01-24 22:42:28 +00003803#if MICROPY_EMIT_NATIVE
3804 if (emit_native != NULL) {
3805#if MICROPY_EMIT_X64
3806 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003807#elif MICROPY_EMIT_X86
3808 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003809#elif MICROPY_EMIT_THUMB
3810 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003811#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003812 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003813#endif
3814 }
3815#endif
3816#if MICROPY_EMIT_INLINE_THUMB
3817 if (emit_inline_thumb != NULL) {
3818 emit_inline_thumb_free(emit_inline_thumb);
3819 }
3820#endif
3821#endif // !MICROPY_EMIT_CPYTHON
3822
Damien George52b5d762014-09-23 15:31:56 +00003823 // free the parse tree
3824 mp_parse_node_free(pn);
3825
Damien George41d02b62014-01-24 22:42:28 +00003826 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003827 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003828 for (scope_t *s = module_scope; s;) {
3829 scope_t *next = s->next;
3830 scope_free(s);
3831 s = next;
3832 }
Damien5ac1b2e2013-10-18 19:58:12 +01003833
Damien George41d02b62014-01-24 22:42:28 +00003834 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003835 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003836 m_del_obj(compiler_t, comp);
3837
Damien Georgea91ac202014-10-05 19:01:34 +01003838 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003839 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003840 } else {
3841#if MICROPY_EMIT_CPYTHON
3842 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003843 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003844 return mp_const_true;
3845#else
3846 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003847 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003848#endif
3849 }
Damien429d7192013-10-04 19:53:11 +01003850}