blob: c3502521ba0faac886b5fe3db73ee29a1fab9aec [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/scope.h"
35#include "py/emit.h"
36#include "py/compile.h"
37#include "py/smallint.h"
38#include "py/runtime.h"
39#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010040
41// TODO need to mangle __attr names
42
43typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000044#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000045#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010046#undef DEF_RULE
47 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010048 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000049 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000050 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010051} pn_kind_t;
52
Damien Georgeb9791222014-01-23 00:34:21 +000053#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
54#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
55#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
56#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010057
Damien Georgea91ac202014-10-05 19:01:34 +010058// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010059typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000060 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010061
Damien George78035b92014-04-09 12:27:39 +010062 uint8_t is_repl;
63 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010064 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010065 uint8_t have_star;
66
67 // try to keep compiler clean from nlr
68 // this is set to an exception object if we have a compile error
69 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010070
Damien George6f355fd2014-04-10 14:11:31 +010071 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010072
Damien George69b89d22014-04-11 13:38:30 +000073 uint16_t num_dict_params;
74 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010075
Damien Georgea91ac202014-10-05 19:01:34 +010076 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
77 uint16_t continue_label;
78 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000079 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010080
Damien429d7192013-10-04 19:53:11 +010081 scope_t *scope_head;
82 scope_t *scope_cur;
83
Damien6cdd3af2013-10-05 18:08:26 +010084 emit_t *emit; // current emitter
85 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010086
87 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
88 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010089} compiler_t;
90
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010091STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010092 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
93 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010094 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George8dfbd2d2015-02-13 01:00:51 +000095 mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010096 } else {
Damien Georgea91ac202014-10-05 19:01:34 +010097 // we don't have a line number, so just pass 0
Damien George8dfbd2d2015-02-13 01:00:51 +000098 mp_obj_exception_add_traceback(exc, comp->source_file, 0, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010099 }
Damien Georgea91ac202014-10-05 19:01:34 +0100100 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000101}
102
Damien Georgeddd1e182015-01-10 14:07:24 +0000103#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100104STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300105 #if MICROPY_PY_UCTYPES
106 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
107 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100108 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100109 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100110};
Damien Georgeddd1e182015-01-10 14:07:24 +0000111STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
112#endif
Damien George57e99eb2014-04-10 22:42:11 +0100113
Damien Georgeffae48d2014-05-08 15:58:39 +0000114// this function is essentially a simple preprocessor
115STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
116 if (0) {
117 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100118#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000119 } else if (MP_PARSE_NODE_IS_ID(pn)) {
120 // lookup identifier in table of dynamic constants
121 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
122 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
123 if (elem != NULL) {
124 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
125 }
126#endif
127 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000128 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100129
Damien Georgeffae48d2014-05-08 15:58:39 +0000130 // fold some parse nodes before folding their arguments
131 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100132#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000133 case PN_expr_stmt:
134 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
135 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
136 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
137 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
138 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
139 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
140 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
141 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
142 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
143 ) {
144 // code to assign dynamic constants: id = const(value)
145
146 // get the id
147 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
148
149 // get the value
150 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
151 pn_value = fold_constants(comp, pn_value, consts);
152 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
153 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
154 break;
155 }
Damien George40f3c022014-07-03 13:25:24 +0100156 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000157
158 // store the value in the table of dynamic constants
159 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
160 if (elem->value != MP_OBJ_NULL) {
161 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
162 break;
163 }
164 elem->value = MP_OBJ_NEW_SMALL_INT(value);
165
166 // replace const(value) with value
167 pns1->nodes[0] = pn_value;
168
169 // finished folding this assignment
170 return pn;
171 }
172 }
173 }
174 break;
175#endif
Damien George5042bce2014-05-25 22:06:06 +0100176 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000177 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000178 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100179 return pn;
Damien429d7192013-10-04 19:53:11 +0100180 }
181
Damien Georgeffae48d2014-05-08 15:58:39 +0000182 // fold arguments
183 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
184 for (int i = 0; i < n; i++) {
185 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
186 }
187
188 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000189 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100190 case PN_atom_paren:
191 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
192 // (int)
193 pn = pns->nodes[0];
194 }
195 break;
196
197 case PN_expr:
198 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
199 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100200 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
201 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100202 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
203 }
204 break;
205
206 case PN_and_expr:
207 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
208 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100209 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
210 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100211 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
212 }
213 break;
214
Damien429d7192013-10-04 19:53:11 +0100215 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000216 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100217 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
218 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000219 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100220 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000221 if (!(arg1 >= (mp_int_t)BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
Damien Georgea26dc502014-04-12 17:54:52 +0100222 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
223 }
Damien George0bb97132015-02-27 14:25:47 +0000224 } else {
225 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100226 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000227 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200228 // Shifting to big amounts is underfined behavior
229 // in C and is CPU-dependent; propagate sign bit.
230 arg1 = BITS_PER_WORD - 1;
231 }
Damiend99b0522013-12-21 18:17:45 +0000232 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100233 }
234 }
235 break;
236
237 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100238 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000239 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100240 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
241 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000242 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100243 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000244 arg0 += arg1;
Damien George0bb97132015-02-27 14:25:47 +0000245 } else {
246 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100247 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000248 arg0 -= arg1;
Damien0efb3a12013-10-12 16:16:56 +0100249 }
Damien Georged1e355e2014-05-28 14:51:12 +0100250 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100251 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000252 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100253 }
254 }
255 break;
256
257 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000258 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 +0100259 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
260 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000261 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100262 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000263 if (!mp_small_int_mul_overflow(arg0, arg1)) {
264 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100265 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000266 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
267 }
268 }
Damiend99b0522013-12-21 18:17:45 +0000269 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100270 // int / int
271 // pass
Damiend99b0522013-12-21 18:17:45 +0000272 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100273 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000274 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damien George0bb97132015-02-27 14:25:47 +0000275 } else {
276 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300277 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100278 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000279 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300280 }
Damien429d7192013-10-04 19:53:11 +0100281 }
282 }
283 break;
284
285 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000286 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100287 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000288 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100289 // +int
Damiend99b0522013-12-21 18:17:45 +0000290 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
291 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100292 // -int
Damiend99b0522013-12-21 18:17:45 +0000293 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
Damien George0bb97132015-02-27 14:25:47 +0000294 } else {
295 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100296 // ~int
Damiend99b0522013-12-21 18:17:45 +0000297 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100298 }
299 }
300 break;
301
302 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100303 if (0) {
304#if MICROPY_EMIT_CPYTHON
305 } 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 +0100306 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100307 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000308 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
309 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200310 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100311 if (power >= 0) {
312 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200313 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100314 for (; power > 0; power--) {
315 ans *= base;
316 }
Damiend99b0522013-12-21 18:17:45 +0000317 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100318 }
319 }
Damien George57e99eb2014-04-10 22:42:11 +0100320#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000321#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100322 } 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])) {
323 // id.id
324 // look it up in constant table, see if it can be replaced with an integer
325 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
326 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
327 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
328 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
329 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
330 if (elem != NULL) {
331 mp_obj_t dest[2];
332 mp_load_method_maybe(elem->value, q_attr, dest);
333 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100334 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000335 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100336 }
337 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000338#endif
Damien429d7192013-10-04 19:53:11 +0100339 }
340 break;
341 }
342 }
343
344 return pn;
345}
346
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200347STATIC 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 +0100348STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000349STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100350
Damien George6f355fd2014-04-10 14:11:31 +0100351STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100352 return comp->next_label++;
353}
354
Damien George8dcc0c72014-03-27 10:55:21 +0000355STATIC void compile_increase_except_level(compiler_t *comp) {
356 comp->cur_except_level += 1;
357 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
358 comp->scope_cur->exc_stack_size = comp->cur_except_level;
359 }
360}
361
362STATIC void compile_decrease_except_level(compiler_t *comp) {
363 assert(comp->cur_except_level > 0);
364 comp->cur_except_level -= 1;
365}
366
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200367STATIC 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 +0100368 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100369 scope->parent = comp->scope_cur;
370 scope->next = NULL;
371 if (comp->scope_head == NULL) {
372 comp->scope_head = scope;
373 } else {
374 scope_t *s = comp->scope_head;
375 while (s->next != NULL) {
376 s = s->next;
377 }
378 s->next = scope;
379 }
380 return scope;
381}
382
Damien George963a5a32015-01-16 17:47:07 +0000383STATIC 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)) {
384 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000385 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
386 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100387 for (int i = 0; i < num_nodes; i++) {
388 f(comp, pns->nodes[i]);
389 }
Damiend99b0522013-12-21 18:17:45 +0000390 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100391 f(comp, pn);
392 }
393}
394
Damien George969a6b32014-12-10 22:07:04 +0000395STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000396 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100397 for (int i = 0; i < num_nodes; i++) {
398 compile_node(comp, pns->nodes[i]);
399 }
400}
401
Damien3ef4abb2013-10-12 16:53:13 +0100402#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200403STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100404 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
405 return true;
406 }
Damien George4c81ba82015-01-13 16:21:23 +0000407 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
408 return true;
409 }
Damien George7d414a12015-02-08 01:57:40 +0000410 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
411 return true;
412 }
Damiend99b0522013-12-21 18:17:45 +0000413 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100414 return false;
415 }
Damiend99b0522013-12-21 18:17:45 +0000416 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100417 return false;
418 }
419 return true;
420}
421
Damien George5042bce2014-05-25 22:06:06 +0100422STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000423 bool has_single_quote = false;
424 bool has_double_quote = false;
425 for (int i = 0; i < len; i++) {
426 if (str[i] == '\'') {
427 has_single_quote = true;
428 } else if (str[i] == '"') {
429 has_double_quote = true;
430 }
431 }
432 if (bytes) {
433 vstr_printf(vstr, "b");
434 }
435 bool quote_single = false;
436 if (has_single_quote && !has_double_quote) {
437 vstr_printf(vstr, "\"");
438 } else {
439 quote_single = true;
440 vstr_printf(vstr, "'");
441 }
442 for (int i = 0; i < len; i++) {
443 if (str[i] == '\n') {
444 vstr_printf(vstr, "\\n");
445 } else if (str[i] == '\\') {
446 vstr_printf(vstr, "\\\\");
447 } else if (str[i] == '\'' && quote_single) {
448 vstr_printf(vstr, "\\'");
449 } else {
450 vstr_printf(vstr, "%c", str[i]);
451 }
452 }
453 if (has_single_quote && !has_double_quote) {
454 vstr_printf(vstr, "\"");
455 } else {
456 vstr_printf(vstr, "'");
457 }
458}
459
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200460STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000461 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 +0100462 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000463 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 +0100464 return;
465 }
466
Damien George7d414a12015-02-08 01:57:40 +0000467 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
468 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
469 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
470 return;
471 }
472
Damiend99b0522013-12-21 18:17:45 +0000473 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200474 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
475 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
476 return;
477 }
478
Damien George42f3de92014-10-03 17:44:14 +0000479 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000480 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
481 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100482 case MP_PARSE_NODE_STRING:
483 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100484 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100485 const byte *str = qstr_data(arg, &len);
486 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
487 break;
488 }
Damiend99b0522013-12-21 18:17:45 +0000489 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100490 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000491 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
492 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
493 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100494 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100495 }
496 break;
497 default: assert(0);
498 }
499}
500
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200501STATIC 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 +0100502 int n = 0;
503 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000504 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100505 }
506 int total = n;
507 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000508 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100509 total += 1;
Damien3a205172013-10-12 15:01:56 +0100510 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100511 is_const = false;
512 }
513 }
514 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100515 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100516 is_const = false;
517 break;
518 }
519 }
520 if (total > 0 && is_const) {
521 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000522 vstr_t *vstr = vstr_new();
523 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000524 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000525 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100526 need_comma = true;
527 }
528 for (int i = 0; i < n; i++) {
529 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000530 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100531 }
Damien02f89412013-12-12 15:13:36 +0000532 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100533 need_comma = true;
534 }
535 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000536 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100537 } else {
Damien02f89412013-12-12 15:13:36 +0000538 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100539 }
Damien George0d3cb672015-01-28 23:43:01 +0000540 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000541 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100542 } else {
Damiend99b0522013-12-21 18:17:45 +0000543 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100544 compile_node(comp, pn);
545 }
546 for (int i = 0; i < n; i++) {
547 compile_node(comp, pns_list->nodes[i]);
548 }
Damien Georgeb9791222014-01-23 00:34:21 +0000549 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100550 }
551}
Damien3a205172013-10-12 15:01:56 +0100552#endif
553
554// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100555STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100556#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100557 cpython_c_tuple(comp, pn, pns_list);
558#else
559 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000560 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100561 compile_node(comp, pn);
562 total += 1;
563 }
564 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000565 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100566 for (int i = 0; i < n; i++) {
567 compile_node(comp, pns_list->nodes[i]);
568 }
569 total += n;
570 }
Damien Georgeb9791222014-01-23 00:34:21 +0000571 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100572#endif
573}
Damien429d7192013-10-04 19:53:11 +0100574
Damien George969a6b32014-12-10 22:07:04 +0000575STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100576 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000577 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100578}
579
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200580STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000581 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
582 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100583}
584
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200585STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000586 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
587 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100588}
589
Damien3ef4abb2013-10-12 16:53:13 +0100590#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100591// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200592STATIC 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 +0100593 if (node_is_const_false(pn)) {
594 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000595 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100596 }
597 return;
598 } else if (node_is_const_true(pn)) {
599 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000600 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100601 }
602 return;
Damiend99b0522013-12-21 18:17:45 +0000603 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
604 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
605 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
606 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100607 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100608 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100609 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100610 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100611 }
Damien3a205172013-10-12 15:01:56 +0100612 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000613 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100614 } else {
615 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100616 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100617 }
618 }
619 return;
Damiend99b0522013-12-21 18:17:45 +0000620 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100621 if (jump_if == false) {
622 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100623 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100624 }
625 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100626 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100627 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100628 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100629 }
Damien3a205172013-10-12 15:01:56 +0100630 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000631 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100632 }
633 return;
Damiend99b0522013-12-21 18:17:45 +0000634 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100635 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100636 return;
637 }
638 }
639
640 // nothing special, fall back to default compiling for node and jump
641 compile_node(comp, pn);
642 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000643 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100644 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000645 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100646 }
647}
Damien3a205172013-10-12 15:01:56 +0100648#endif
Damien429d7192013-10-04 19:53:11 +0100649
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200650STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100651#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100652 cpython_c_if_cond(comp, pn, jump_if, label, false);
653#else
654 if (node_is_const_false(pn)) {
655 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000656 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100657 }
658 return;
659 } else if (node_is_const_true(pn)) {
660 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000661 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100662 }
663 return;
Damiend99b0522013-12-21 18:17:45 +0000664 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
665 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
666 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
667 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100668 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100669 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100670 for (int i = 0; i < n - 1; i++) {
671 c_if_cond(comp, pns->nodes[i], true, label2);
672 }
673 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000674 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100675 } else {
676 for (int i = 0; i < n; i++) {
677 c_if_cond(comp, pns->nodes[i], true, label);
678 }
679 }
680 return;
Damiend99b0522013-12-21 18:17:45 +0000681 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100682 if (jump_if == false) {
683 for (int i = 0; i < n; i++) {
684 c_if_cond(comp, pns->nodes[i], false, label);
685 }
686 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100687 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100688 for (int i = 0; i < n - 1; i++) {
689 c_if_cond(comp, pns->nodes[i], false, label2);
690 }
691 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000692 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100693 }
694 return;
Damiend99b0522013-12-21 18:17:45 +0000695 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100696 c_if_cond(comp, pns->nodes[0], !jump_if, label);
697 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100698 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
699 // cond is something in parenthesis
700 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
701 // empty tuple, acts as false for the condition
702 if (jump_if == false) {
703 EMIT_ARG(jump, label);
704 }
705 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
706 // non-empty tuple, acts as true for the condition
707 if (jump_if == true) {
708 EMIT_ARG(jump, label);
709 }
710 } else {
711 // parenthesis around 1 item, is just that item
712 c_if_cond(comp, pns->nodes[0], jump_if, label);
713 }
714 return;
Damien3a205172013-10-12 15:01:56 +0100715 }
716 }
717
718 // nothing special, fall back to default compiling for node and jump
719 compile_node(comp, pn);
720 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000721 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100722 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000723 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100724 }
725#endif
Damien429d7192013-10-04 19:53:11 +0100726}
727
728typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100729STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100730
Damien George6be0b0a2014-08-15 14:30:52 +0100731STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100732 if (assign_kind != ASSIGN_AUG_STORE) {
733 compile_node(comp, pns->nodes[0]);
734 }
735
Damiend99b0522013-12-21 18:17:45 +0000736 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
737 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
738 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
739 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100740 if (assign_kind != ASSIGN_AUG_STORE) {
741 for (int i = 0; i < n - 1; i++) {
742 compile_node(comp, pns1->nodes[i]);
743 }
744 }
Damiend99b0522013-12-21 18:17:45 +0000745 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
746 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100747 }
Damiend99b0522013-12-21 18:17:45 +0000748 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100749 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000750 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100751 if (assign_kind == ASSIGN_AUG_STORE) {
752 EMIT(rot_three);
753 EMIT(store_subscr);
754 } else {
755 compile_node(comp, pns1->nodes[0]);
756 if (assign_kind == ASSIGN_AUG_LOAD) {
757 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100758 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100759 } else {
760 EMIT(store_subscr);
761 }
762 }
Damiend99b0522013-12-21 18:17:45 +0000763 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
764 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100765 if (assign_kind == ASSIGN_AUG_LOAD) {
766 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000767 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100768 } else {
769 if (assign_kind == ASSIGN_AUG_STORE) {
770 EMIT(rot_two);
771 }
Damien Georgeb9791222014-01-23 00:34:21 +0000772 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100773 }
774 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100775 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100776 }
777 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100778 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100779 }
780
Damiend99b0522013-12-21 18:17:45 +0000781 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100782 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100783 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100784
785 return;
786
787cannot_assign:
788 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100789}
790
Damien George0288cf02014-04-11 11:53:00 +0000791// 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 +0100792STATIC 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 +0000793 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
794
795 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000796 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000797 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
798 EMIT_ARG(unpack_ex, 0, num_tail);
799 have_star_index = 0;
800 }
Damien George963a5a32015-01-16 17:47:07 +0000801 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000802 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000803 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000804 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
805 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100806 } else {
Damien George9d181f62014-04-27 16:55:27 +0100807 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100808 return;
809 }
810 }
811 }
Damien George963a5a32015-01-16 17:47:07 +0000812 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000813 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100814 }
Damien George0288cf02014-04-11 11:53:00 +0000815 if (num_head != 0) {
816 if (0 == have_star_index) {
817 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100818 } else {
Damien George0288cf02014-04-11 11:53:00 +0000819 c_assign(comp, node_head, ASSIGN_STORE);
820 }
821 }
Damien George963a5a32015-01-16 17:47:07 +0000822 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000823 if (num_head + i == have_star_index) {
824 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
825 } else {
826 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100827 }
828 }
829}
830
831// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100832STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100833 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000834 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100835 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000836 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
837 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000838 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100839 switch (assign_kind) {
840 case ASSIGN_STORE:
841 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000842 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100843 break;
844 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000845 default:
Damien Georgeb9791222014-01-23 00:34:21 +0000846 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100847 break;
848 }
849 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100850 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100851 return;
852 }
853 } else {
Damiend99b0522013-12-21 18:17:45 +0000854 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
855 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100856 case PN_power:
857 // lhs is an index or attribute
858 c_assign_power(comp, pns, assign_kind);
859 break;
860
861 case PN_testlist_star_expr:
862 case PN_exprlist:
863 // lhs is a tuple
864 if (assign_kind != ASSIGN_STORE) {
865 goto bad_aug;
866 }
Damien George0288cf02014-04-11 11:53:00 +0000867 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100868 break;
869
870 case PN_atom_paren:
871 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000872 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100873 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100874 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000875 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
876 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100877 goto testlist_comp;
878 } else {
879 // parenthesis around 1 item, is just that item
880 pn = pns->nodes[0];
881 goto tail_recursion;
882 }
883 break;
884
885 case PN_atom_bracket:
886 // lhs is something in brackets
887 if (assign_kind != ASSIGN_STORE) {
888 goto bad_aug;
889 }
Damiend99b0522013-12-21 18:17:45 +0000890 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100891 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000892 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000893 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
894 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100895 goto testlist_comp;
896 } else {
897 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000898 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100899 }
900 break;
901
902 default:
Damien George9d181f62014-04-27 16:55:27 +0100903 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100904 }
905 return;
906
907 testlist_comp:
908 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000909 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
910 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
911 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100912 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000913 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000914 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000915 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100916 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000917 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
918 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000919 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100920 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100921 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100922 } else {
923 // sequence with 2 items
924 goto sequence_with_2_items;
925 }
926 } else {
927 // sequence with 2 items
928 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000929 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100930 }
931 return;
932 }
933 return;
934
Damien George9d181f62014-04-27 16:55:27 +0100935 cannot_assign:
936 compile_syntax_error(comp, pn, "can't assign to expression");
937 return;
938
Damien429d7192013-10-04 19:53:11 +0100939 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100940 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100941}
942
943// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100944// if we are not in CPython compatibility mode then:
945// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
946// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
947// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100948STATIC 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 +0100949 assert(n_pos_defaults >= 0);
950 assert(n_kw_defaults >= 0);
951
Damien429d7192013-10-04 19:53:11 +0100952 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000953 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100954 int nfree = 0;
955 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000956 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
957 id_info_t *id = &comp->scope_cur->id_info[i];
958 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
959 for (int j = 0; j < this_scope->id_info_len; j++) {
960 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100961 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000962#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100963 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000964#else
965 // in Micro Python we load closures using LOAD_FAST
Damien George0abb5602015-01-16 12:24:49 +0000966 EMIT_ARG(load_fast, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000967#endif
Damien318aec62013-12-10 18:28:17 +0000968 nfree += 1;
969 }
970 }
Damien429d7192013-10-04 19:53:11 +0100971 }
972 }
973 }
Damien429d7192013-10-04 19:53:11 +0100974
975 // make the function/closure
976 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100977 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100978 } else {
Damien George3558f622014-04-20 17:50:40 +0100979 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100980 }
981}
982
Damien George6be0b0a2014-08-15 14:30:52 +0100983STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000984 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100985 comp->have_star = true;
986 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000987 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
988 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100989 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100990 } else {
991 // named star
Damien429d7192013-10-04 19:53:11 +0100992 }
Damien George8b19db02014-04-11 23:25:34 +0100993 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000994
995 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100996 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000997 // TODO do we need to do anything with this?
998
999 } else {
1000 mp_parse_node_t pn_id;
1001 mp_parse_node_t pn_colon;
1002 mp_parse_node_t pn_equal;
1003 if (MP_PARSE_NODE_IS_ID(pn)) {
1004 // this parameter is just an id
1005
1006 pn_id = pn;
1007 pn_colon = MP_PARSE_NODE_NULL;
1008 pn_equal = MP_PARSE_NODE_NULL;
1009
Damien George0bb97132015-02-27 14:25:47 +00001010 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001011 // this parameter has a colon and/or equal specifier
1012
Damien George0bb97132015-02-27 14:25:47 +00001013 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1014
Damien Georgef41fdd02014-03-03 23:19:11 +00001015 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1016 pn_id = pns->nodes[0];
1017 pn_colon = pns->nodes[1];
1018 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +00001019 }
1020
1021 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1022 // this parameter does not have a default value
1023
1024 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001025 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001026 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001027 return;
1028 }
1029
1030 } else {
1031 // this parameter has a default value
1032 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1033
Damien George8b19db02014-04-11 23:25:34 +01001034 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001035 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001036#if MICROPY_EMIT_CPYTHON
1037 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1038 compile_node(comp, pn_equal);
1039#else
Damien George69b89d22014-04-11 13:38:30 +00001040 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1041 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001042 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1043 // we need to do this here before we start building the map for the default keywords
1044 if (comp->num_default_params > 0) {
1045 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001046 } else {
1047 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001048 }
Damien George69b89d22014-04-11 13:38:30 +00001049 // first default dict param, so make the map
1050 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001051 }
Damien Georgef0778a72014-06-07 22:01:00 +01001052
1053 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001054 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001055 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001056 EMIT(store_map);
1057#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001058 } else {
Damien George69b89d22014-04-11 13:38:30 +00001059 comp->num_default_params += 1;
1060 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001061 }
1062 }
1063
1064 // TODO pn_colon not implemented
1065 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001066 }
1067}
1068
1069// leaves function object on stack
1070// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001071STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001072 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001073 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001074 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001075 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001076 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001077 }
1078
1079 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001080 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001081 uint old_num_dict_params = comp->num_dict_params;
1082 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001083
1084 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001085 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001086 comp->num_dict_params = 0;
1087 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001088 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001089
Damien Georgea91ac202014-10-05 19:01:34 +01001090 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001091 return MP_QSTR_NULL;
1092 }
1093
Damien Georgee337f1e2014-03-31 15:18:37 +01001094#if !MICROPY_EMIT_CPYTHON
1095 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001096 // the default keywords args may have already made the tuple; if not, do it now
1097 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001098 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001099 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001100 }
1101#endif
1102
Damien429d7192013-10-04 19:53:11 +01001103 // get the scope for this function
1104 scope_t *fscope = (scope_t*)pns->nodes[4];
1105
1106 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001107 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001108
1109 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001110 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001111 comp->num_dict_params = old_num_dict_params;
1112 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001113
1114 // return its name (the 'f' in "def f(...):")
1115 return fscope->simple_name;
1116}
1117
1118// leaves class object on stack
1119// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001120STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001121 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001122 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001123 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001124 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001125 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001126 }
1127
1128 EMIT(load_build_class);
1129
1130 // scope for this class
1131 scope_t *cscope = (scope_t*)pns->nodes[3];
1132
1133 // compile the class
1134 close_over_variables_etc(comp, cscope, 0, 0);
1135
1136 // get its name
Damien George968bf342014-04-27 19:12:05 +01001137 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001138
1139 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001140 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1141 mp_parse_node_t parents = pns->nodes[1];
1142 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1143 parents = MP_PARSE_NODE_NULL;
1144 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001145 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001146 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001147
1148 // return its name (the 'C' in class C(...):")
1149 return cscope->simple_name;
1150}
1151
Damien6cdd3af2013-10-05 18:08:26 +01001152// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001153STATIC 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 +00001154 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001155 return false;
1156 }
1157
1158 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001159 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001160 return true;
1161 }
1162
Damiend99b0522013-12-21 18:17:45 +00001163 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001164 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001165 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001166#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001167 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001168 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001169 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001170 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001171#endif
Damien3ef4abb2013-10-12 16:53:13 +01001172#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001173 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001174 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001175#endif
Damien6cdd3af2013-10-05 18:08:26 +01001176 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001177 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001178 }
1179
1180 return true;
1181}
1182
Damien George969a6b32014-12-10 22:07:04 +00001183STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001184 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001185 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001186 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001187
Damien6cdd3af2013-10-05 18:08:26 +01001188 // inherit emit options for this function/class definition
1189 uint emit_options = comp->scope_cur->emit_options;
1190
1191 // compile each decorator
1192 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001193 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001194 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1195 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001196
1197 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001198 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001199 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001200
1201 // check for built-in decorators
1202 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1203 // this was a built-in
1204 num_built_in_decorators += 1;
1205
1206 } else {
1207 // not a built-in, compile normally
1208
1209 // compile the decorator function
1210 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001211 for (int j = 1; j < name_len; j++) {
1212 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1213 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001214 }
1215
1216 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001217 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001218 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001219 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001220 compile_node(comp, pns_decorator->nodes[1]);
1221 }
Damien429d7192013-10-04 19:53:11 +01001222 }
1223 }
1224
1225 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001226 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001227 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001228 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001229 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001230 } else {
Damien George0bb97132015-02-27 14:25:47 +00001231 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1232 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001233 }
1234
1235 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001236 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001237 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001238 }
1239
1240 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001241 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001242}
1243
Damien George969a6b32014-12-10 22:07:04 +00001244STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001245 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001246 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001247 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001248}
1249
Damien George6be0b0a2014-08-15 14:30:52 +01001250STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001251 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001252 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001253 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1254 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001255
1256 compile_node(comp, pns->nodes[0]); // base of the power node
1257
Damiend99b0522013-12-21 18:17:45 +00001258 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1259 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1260 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1261 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001262 for (int i = 0; i < n - 1; i++) {
1263 compile_node(comp, pns1->nodes[i]);
1264 }
Damiend99b0522013-12-21 18:17:45 +00001265 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1266 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001267 }
Damiend99b0522013-12-21 18:17:45 +00001268 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001269 // can't delete function calls
1270 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001271 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001272 compile_node(comp, pns1->nodes[0]);
1273 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001274 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1275 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001276 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001277 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001278 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001279 }
1280 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001281 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001282 }
1283
Damiend99b0522013-12-21 18:17:45 +00001284 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001285 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001286 }
Damiend99b0522013-12-21 18:17:45 +00001287 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1288 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1289 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1290 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001291 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1292
Damiend99b0522013-12-21 18:17:45 +00001293 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1294 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1295 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001296 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001297 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001298 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001299 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001300 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001301 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001302 c_del_stmt(comp, pns->nodes[0]);
1303 for (int i = 0; i < n; i++) {
1304 c_del_stmt(comp, pns1->nodes[i]);
1305 }
Damiend99b0522013-12-21 18:17:45 +00001306 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001307 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001308 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001309 } else {
1310 // sequence with 2 items
1311 goto sequence_with_2_items;
1312 }
1313 } else {
1314 // sequence with 2 items
1315 sequence_with_2_items:
1316 c_del_stmt(comp, pns->nodes[0]);
1317 c_del_stmt(comp, pns->nodes[1]);
1318 }
1319 } else {
1320 // tuple with 1 element
1321 c_del_stmt(comp, pn);
1322 }
1323 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001324 // TODO is there anything else to implement?
1325 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001326 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001327
1328 return;
1329
1330cannot_delete:
1331 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001332}
1333
Damien George969a6b32014-12-10 22:07:04 +00001334STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001335 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1336}
1337
Damien George969a6b32014-12-10 22:07:04 +00001338STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001339 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001340 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001341 }
Damien George090c9232014-10-17 14:08:49 +00001342 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001343 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001344}
1345
Damien George969a6b32014-12-10 22:07:04 +00001346STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001347 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001348 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001349 }
Damien George090c9232014-10-17 14:08:49 +00001350 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001351 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001352}
1353
Damien George969a6b32014-12-10 22:07:04 +00001354STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001355 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001356 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001357 return;
1358 }
Damiend99b0522013-12-21 18:17:45 +00001359 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001360 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001361 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001362 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001363 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001364 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1365 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 +01001366
Damien George6f355fd2014-04-10 14:11:31 +01001367 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001368 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1369 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1370 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001371 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001372 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1373 } else {
1374 compile_node(comp, pns->nodes[0]);
1375 }
1376 EMIT(return_value);
1377}
1378
Damien George969a6b32014-12-10 22:07:04 +00001379STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001380 compile_node(comp, pns->nodes[0]);
1381 EMIT(pop_top);
1382}
1383
Damien George969a6b32014-12-10 22:07:04 +00001384STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001385 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001386 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001387 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001388 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001389 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001390 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001391 compile_node(comp, pns->nodes[0]);
1392 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001393 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001394 } else {
1395 // raise x
1396 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001397 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001398 }
1399}
1400
Damien George635543c2014-04-10 12:56:52 +01001401// q_base holds the base of the name
1402// eg a -> q_base=a
1403// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001404STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001405 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001406 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1407 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001408 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001409 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001410 pn = pns->nodes[0];
1411 is_as = true;
1412 }
Damien George635543c2014-04-10 12:56:52 +01001413 if (MP_PARSE_NODE_IS_NULL(pn)) {
1414 // empty name (eg, from . import x)
1415 *q_base = MP_QSTR_;
1416 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1417 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001418 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001419 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001420 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001421 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001422 }
Damien George635543c2014-04-10 12:56:52 +01001423 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001424 } else {
1425 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001426 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001427 {
Damien429d7192013-10-04 19:53:11 +01001428 // a name of the form a.b.c
1429 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001430 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001431 }
Damiend99b0522013-12-21 18:17:45 +00001432 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001433 int len = n - 1;
1434 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001435 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001436 }
Damien George55baff42014-01-21 21:40:13 +00001437 byte *q_ptr;
1438 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001439 for (int i = 0; i < n; i++) {
1440 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001441 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001442 }
Damien George39dc1452014-10-03 19:52:22 +01001443 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001444 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001445 memcpy(str_dest, str_src, str_src_len);
1446 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001447 }
Damien George635543c2014-04-10 12:56:52 +01001448 qstr q_full = qstr_build_end(q_ptr);
1449 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001450 if (is_as) {
1451 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001452 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001453 }
1454 }
Damien429d7192013-10-04 19:53:11 +01001455 }
Damien429d7192013-10-04 19:53:11 +01001456 }
1457}
1458
Damien George6be0b0a2014-08-15 14:30:52 +01001459STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001460 EMIT_ARG(load_const_small_int, 0); // level 0 import
1461 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1462 qstr q_base;
1463 do_import_name(comp, pn, &q_base);
1464 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001465}
1466
Damien George969a6b32014-12-10 22:07:04 +00001467STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001468 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1469}
1470
Damien George969a6b32014-12-10 22:07:04 +00001471STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001472 mp_parse_node_t pn_import_source = pns->nodes[0];
1473
1474 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1475 uint import_level = 0;
1476 do {
1477 mp_parse_node_t pn_rel;
1478 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)) {
1479 // This covers relative imports with dots only like "from .. import"
1480 pn_rel = pn_import_source;
1481 pn_import_source = MP_PARSE_NODE_NULL;
1482 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1483 // This covers relative imports starting with dot(s) like "from .foo import"
1484 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1485 pn_rel = pns_2b->nodes[0];
1486 pn_import_source = pns_2b->nodes[1];
1487 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1488 } else {
1489 // Not a relative import
1490 break;
1491 }
1492
1493 // get the list of . and/or ...'s
1494 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001495 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001496
1497 // count the total number of .'s
1498 for (int i = 0; i < n; i++) {
1499 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1500 import_level++;
1501 } else {
1502 // should be an MP_TOKEN_ELLIPSIS
1503 import_level += 3;
1504 }
1505 }
1506 } while (0);
1507
Damiend99b0522013-12-21 18:17:45 +00001508 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001509 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001510
1511 // build the "fromlist" tuple
1512#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001513 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001514#else
Damien George708c0732014-04-27 19:23:46 +01001515 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001516 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001517#endif
1518
1519 // do the import
Damien George635543c2014-04-10 12:56:52 +01001520 qstr dummy_q;
1521 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001522 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001523
Damien429d7192013-10-04 19:53:11 +01001524 } else {
Damien George635543c2014-04-10 12:56:52 +01001525 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001526
1527 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001528 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001529 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001530#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001531 {
1532 vstr_t *vstr = vstr_new();
1533 vstr_printf(vstr, "(");
1534 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001535 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1536 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1537 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001538 if (i > 0) {
1539 vstr_printf(vstr, ", ");
1540 }
1541 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001542 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001543 const byte *str = qstr_data(id2, &len);
1544 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001545 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001546 }
Damien02f89412013-12-12 15:13:36 +00001547 if (n == 1) {
1548 vstr_printf(vstr, ",");
1549 }
1550 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001551 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001552 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001553 }
Damiendb4c3612013-12-10 17:27:24 +00001554#else
1555 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001556 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1557 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1558 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001559 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001560 }
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001562#endif
1563
1564 // do the import
Damien George635543c2014-04-10 12:56:52 +01001565 qstr dummy_q;
1566 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001567 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001568 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1569 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1570 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001571 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001572 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001573 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001574 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001575 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001576 }
1577 }
1578 EMIT(pop_top);
1579 }
1580}
1581
Damien George584ba672014-12-21 17:26:45 +00001582STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1583 bool added;
1584 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1585 if (!added) {
1586 compile_syntax_error(comp, pn, "identifier already used");
1587 return;
1588 }
1589 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1590
1591 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1592 id_info = scope_find_global(comp->scope_cur, qst);
1593 if (id_info != NULL) {
1594 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1595 }
1596}
1597
Damien George969a6b32014-12-10 22:07:04 +00001598STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001599 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001600 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001601 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001602 for (int i = 0; i < n; i++) {
1603 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001604 }
1605 }
1606}
1607
Damien George584ba672014-12-21 17:26:45 +00001608STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1609 bool added;
1610 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1611 if (!added) {
1612 compile_syntax_error(comp, pn, "identifier already used");
1613 return;
1614 }
1615 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1616 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)) {
1617 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1618 return;
1619 }
1620 id_info->kind = ID_INFO_KIND_FREE;
1621 scope_close_over_in_parents(comp->scope_cur, qst);
1622}
1623
Damien George969a6b32014-12-10 22:07:04 +00001624STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001625 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001626 if (comp->scope_cur->kind == SCOPE_MODULE) {
1627 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1628 return;
1629 }
1630 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001631 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001632 for (int i = 0; i < n; i++) {
1633 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001634 }
1635 }
1636}
1637
Damien George969a6b32014-12-10 22:07:04 +00001638STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001639 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001640 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001641 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 +00001642 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001643 // assertion message
1644 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001645 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001646 }
Damien Georgeb9791222014-01-23 00:34:21 +00001647 EMIT_ARG(raise_varargs, 1);
1648 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001649}
1650
Damien George969a6b32014-12-10 22:07:04 +00001651STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001652 // TODO proper and/or short circuiting
1653
Damien George6f355fd2014-04-10 14:11:31 +01001654 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001655
Damien George391db862014-10-17 17:57:33 +00001656 // optimisation: don't emit anything when "if False" (not in CPython)
1657 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1658 uint l_fail = comp_next_label(comp);
1659 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001660
Damien George391db862014-10-17 17:57:33 +00001661 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001662
Damien George391db862014-10-17 17:57:33 +00001663 // optimisation: skip everything else when "if True" (not in CPython)
1664 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1665 goto done;
1666 }
1667
1668 if (
1669 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1670 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1671 // optimisation: don't jump if last instruction was return
1672 && !EMIT(last_emit_was_return_value)
1673 ) {
1674 // jump over elif/else blocks
1675 EMIT_ARG(jump, l_end);
1676 }
1677
1678 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001679 }
1680
Damien George235f9b32014-10-17 17:30:16 +00001681 // compile elif blocks (if any)
1682 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001683 int n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
Damien George235f9b32014-10-17 17:30:16 +00001684 for (int i = 0; i < n_elif; i++) {
1685 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1686 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001687
Damien George391db862014-10-17 17:57:33 +00001688 // optimisation: don't emit anything when "if False" (not in CPython)
1689 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1690 uint l_fail = comp_next_label(comp);
1691 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001692
Damien George391db862014-10-17 17:57:33 +00001693 compile_node(comp, pns_elif->nodes[1]); // elif block
1694
1695 // optimisation: skip everything else when "elif True" (not in CPython)
1696 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1697 goto done;
1698 }
1699
1700 // optimisation: don't jump if last instruction was return
1701 if (!EMIT(last_emit_was_return_value)) {
1702 EMIT_ARG(jump, l_end);
1703 }
1704 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001705 }
1706 }
1707
1708 // compile else block
1709 compile_node(comp, pns->nodes[3]); // can be null
1710
Damien George391db862014-10-17 17:57:33 +00001711done:
Damien Georgeb9791222014-01-23 00:34:21 +00001712 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001713}
1714
Damien Georgecbddb272014-02-01 20:08:18 +00001715#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001716 uint16_t old_break_label = comp->break_label; \
1717 uint16_t old_continue_label = comp->continue_label; \
1718 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001719 uint break_label = comp_next_label(comp); \
1720 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001721 comp->break_label = break_label; \
1722 comp->continue_label = continue_label; \
1723 comp->break_continue_except_level = comp->cur_except_level;
1724
1725#define END_BREAK_CONTINUE_BLOCK \
1726 comp->break_label = old_break_label; \
1727 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001728 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001729
Damien George969a6b32014-12-10 22:07:04 +00001730STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001731 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001732
Damience89a212013-10-15 22:25:17 +01001733 // compared to CPython, we have an optimised version of while loops
1734#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001735 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001736 EMIT_ARG(setup_loop, break_label);
1737 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001738 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1739 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001740 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001741 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001742 }
Damien Georgeb9791222014-01-23 00:34:21 +00001743 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001744 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1745 // this is a small hack to agree with CPython
1746 if (!node_is_const_true(pns->nodes[0])) {
1747 EMIT(pop_block);
1748 }
Damience89a212013-10-15 22:25:17 +01001749#else
Damien George391db862014-10-17 17:57:33 +00001750 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1751 uint top_label = comp_next_label(comp);
1752 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1753 EMIT_ARG(jump, continue_label);
1754 }
1755 EMIT_ARG(label_assign, top_label);
1756 compile_node(comp, pns->nodes[1]); // body
1757 EMIT_ARG(label_assign, continue_label);
1758 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1759 }
Damience89a212013-10-15 22:25:17 +01001760#endif
1761
1762 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001763 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001764
1765 compile_node(comp, pns->nodes[2]); // else
1766
Damien Georgeb9791222014-01-23 00:34:21 +00001767 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001768}
1769
Damien George2ac4af62014-08-15 16:45:41 +01001770#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001771// This function compiles an optimised for-loop of the form:
1772// for <var> in range(<start>, <end>, <step>):
1773// <body>
1774// else:
1775// <else>
1776// <var> must be an identifier and <step> must be a small-int.
1777//
1778// Semantics of for-loop require:
1779// - final failing value should not be stored in the loop variable
1780// - if the loop never runs, the loop variable should never be assigned
1781// - assignments to <var>, <end> or <step> in the body do not alter the loop
1782// (<step> is a constant for us, so no need to worry about it changing)
1783//
1784// If <end> is a small-int, then the stack during the for-loop contains just
1785// the current value of <var>. Otherwise, the stack contains <end> then the
1786// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001787STATIC 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 +00001788 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001789
Damien George6f355fd2014-04-10 14:11:31 +01001790 uint top_label = comp_next_label(comp);
1791 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001792
Damien Georgee181c0d2014-12-12 17:19:56 +00001793 // put the end value on the stack if it's not a small-int constant
1794 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1795 if (end_on_stack) {
1796 compile_node(comp, pn_end);
1797 }
1798
1799 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001800 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001801
Damien Georgeb9791222014-01-23 00:34:21 +00001802 EMIT_ARG(jump, entry_label);
1803 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001804
Damien Georgec33ce602014-12-11 17:35:23 +00001805 // duplicate next value and store it to var
1806 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001807 c_assign(comp, pn_var, ASSIGN_STORE);
1808
Damienf3822fc2013-11-09 20:12:03 +00001809 // compile body
1810 compile_node(comp, pn_body);
1811
Damien Georgeb9791222014-01-23 00:34:21 +00001812 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001813
Damien Georgee181c0d2014-12-12 17:19:56 +00001814 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001815 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001816 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001817
Damien Georgeb9791222014-01-23 00:34:21 +00001818 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001819
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001820 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001821 if (end_on_stack) {
1822 EMIT(dup_top_two);
1823 EMIT(rot_two);
1824 } else {
1825 EMIT(dup_top);
1826 compile_node(comp, pn_end);
1827 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001828 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1829 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001830 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001831 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001832 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001833 }
Damien Georgeb9791222014-01-23 00:34:21 +00001834 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001835
1836 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001837 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001838
1839 compile_node(comp, pn_else);
1840
Damien Georgeb9791222014-01-23 00:34:21 +00001841 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001842
1843 // discard final value of var that failed the loop condition
1844 EMIT(pop_top);
1845
1846 // discard <end> value if it's on the stack
1847 if (end_on_stack) {
1848 EMIT(pop_top);
1849 }
Damienf72fd0e2013-11-06 20:20:49 +00001850}
Damien George2ac4af62014-08-15 16:45:41 +01001851#endif
Damienf72fd0e2013-11-06 20:20:49 +00001852
Damien George969a6b32014-12-10 22:07:04 +00001853STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001854#if !MICROPY_EMIT_CPYTHON
1855 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1856 // this is actually slower, but uses no heap memory
1857 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001858 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 +00001859 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001860 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1861 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1862 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1863 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001864 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1865 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001866 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001867 mp_parse_node_t pn_range_start;
1868 mp_parse_node_t pn_range_end;
1869 mp_parse_node_t pn_range_step;
1870 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001871 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001872 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001873 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001874 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001875 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001876 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001877 } else if (n_args == 2) {
1878 pn_range_start = args[0];
1879 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001880 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001881 } else {
1882 pn_range_start = args[0];
1883 pn_range_end = args[1];
1884 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001885 // We need to know sign of step. This is possible only if it's constant
1886 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1887 optimize = false;
1888 }
Damienf72fd0e2013-11-06 20:20:49 +00001889 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001890 }
1891 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001892 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1893 return;
1894 }
1895 }
1896 }
1897#endif
1898
Damien Georgecbddb272014-02-01 20:08:18 +00001899 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001900 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001901
Damien George6f355fd2014-04-10 14:11:31 +01001902 uint pop_label = comp_next_label(comp);
1903 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001904
Damience89a212013-10-15 22:25:17 +01001905 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1906#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001907 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001908#endif
1909
Damien429d7192013-10-04 19:53:11 +01001910 compile_node(comp, pns->nodes[1]); // iterator
1911 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001912 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001913 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001914 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1915 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001916 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001917 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001918 }
Damien Georgeb9791222014-01-23 00:34:21 +00001919 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001920 EMIT(for_iter_end);
1921
1922 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001923 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001924
Damience89a212013-10-15 22:25:17 +01001925#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001926 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001927#endif
Damien429d7192013-10-04 19:53:11 +01001928
1929 compile_node(comp, pns->nodes[3]); // else (not tested)
1930
Damien Georgeb9791222014-01-23 00:34:21 +00001931 EMIT_ARG(label_assign, break_label);
1932 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001933}
1934
Damien George6be0b0a2014-08-15 14:30:52 +01001935STATIC 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 +01001936 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001937 uint l1 = comp_next_label(comp);
1938 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001939
Damien Georgeb9791222014-01-23 00:34:21 +00001940 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001941 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001942
Damien429d7192013-10-04 19:53:11 +01001943 compile_node(comp, pn_body); // body
1944 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001945 EMIT_ARG(jump, success_label); // jump over exception handler
1946
1947 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001948 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001949
Damien George6f355fd2014-04-10 14:11:31 +01001950 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001951
1952 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001953 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1954 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001955
1956 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001957 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001958
Damiend99b0522013-12-21 18:17:45 +00001959 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001960 // this is a catch all exception handler
1961 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001962 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001963 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001964 return;
1965 }
1966 } else {
1967 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001968 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1969 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1970 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1971 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001972 // handler binds the exception to a local
1973 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001974 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001975 }
1976 }
1977 EMIT(dup_top);
1978 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001979 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001980 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001981 }
1982
1983 EMIT(pop_top);
1984
1985 if (qstr_exception_local == 0) {
1986 EMIT(pop_top);
1987 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001988 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001989 }
1990
1991 EMIT(pop_top);
1992
Damien George6f355fd2014-04-10 14:11:31 +01001993 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001994 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001995 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001996 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001997 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001998 }
1999 compile_node(comp, pns_except->nodes[1]);
2000 if (qstr_exception_local != 0) {
2001 EMIT(pop_block);
2002 }
2003 EMIT(pop_except);
2004 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002005 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2006 EMIT_ARG(label_assign, l3);
2007 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2008 EMIT_ARG(store_id, qstr_exception_local);
2009 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002010
Damien George8dcc0c72014-03-27 10:55:21 +00002011 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002012 EMIT(end_finally);
2013 }
Damien Georgeb9791222014-01-23 00:34:21 +00002014 EMIT_ARG(jump, l2);
2015 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002016 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002017 }
2018
Damien George8dcc0c72014-03-27 10:55:21 +00002019 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002020 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002021 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002022
Damien Georgeb9791222014-01-23 00:34:21 +00002023 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002024 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002025 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002026}
2027
Damien George6be0b0a2014-08-15 14:30:52 +01002028STATIC 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 +01002029 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002030
Damien Georgeb9791222014-01-23 00:34:21 +00002031 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002032 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002033
Damien429d7192013-10-04 19:53:11 +01002034 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002035 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002036 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002037 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002038 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002039 } else {
2040 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2041 }
2042 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002043 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2044 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002045 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002046
Damien George8dcc0c72014-03-27 10:55:21 +00002047 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002048 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002049}
2050
Damien George969a6b32014-12-10 22:07:04 +00002051STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00002052 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2053 {
Damiend99b0522013-12-21 18:17:45 +00002054 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2055 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002056 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002057 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2058 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002059 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002060 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002061 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002062 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002063 // no finally
2064 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2065 } else {
2066 // have finally
Damiend99b0522013-12-21 18:17:45 +00002067 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 +01002068 }
2069 } else {
2070 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002071 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002072 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002073 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002074 }
Damien429d7192013-10-04 19:53:11 +01002075 }
2076}
2077
Damien George6be0b0a2014-08-15 14:30:52 +01002078STATIC 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 +01002079 if (n == 0) {
2080 // no more pre-bits, compile the body of the with
2081 compile_node(comp, body);
2082 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002083 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002084 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002085 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002086 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002087 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002088 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002089 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2090 } else {
2091 // this pre-bit is just an expression
2092 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002093 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002094 EMIT(pop_top);
2095 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002096 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002097 // compile additional pre-bits and the body
2098 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2099 // finish this with block
2100 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002101 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2102 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002103 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002104 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002105 EMIT(end_finally);
2106 }
2107}
2108
Damien George969a6b32014-12-10 22:07:04 +00002109STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002110 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002111 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002112 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002113 assert(n > 0);
2114
2115 // compile in a nested fashion
2116 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2117}
2118
Damien George969a6b32014-12-10 22:07:04 +00002119STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002120 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002121 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2122 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002123 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002124 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002125 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002126 EMIT(pop_top);
2127
Damien429d7192013-10-04 19:53:11 +01002128 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002129 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002130 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002131 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002132 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2133 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002134 // do nothing with a lonely constant
2135 } else {
2136 compile_node(comp, pns->nodes[0]); // just an expression
2137 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2138 }
Damien429d7192013-10-04 19:53:11 +01002139 }
2140 } else {
Damiend99b0522013-12-21 18:17:45 +00002141 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2142 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002143 if (kind == PN_expr_stmt_augassign) {
2144 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2145 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002146 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002147 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002148 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002149 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2150 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2151 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2152 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2153 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2154 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2155 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2156 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2157 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2158 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2159 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002160 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002161 }
Damien George7e5fb242014-02-01 22:18:47 +00002162 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002163 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2164 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002165 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2166 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002167 // following CPython, we store left-most first
2168 if (rhs > 0) {
2169 EMIT(dup_top);
2170 }
2171 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2172 for (int i = 0; i < rhs; i++) {
2173 if (i + 1 < rhs) {
2174 EMIT(dup_top);
2175 }
Damiend99b0522013-12-21 18:17:45 +00002176 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002177 }
Damien George0bb97132015-02-27 14:25:47 +00002178 } else {
2179 assert(kind == PN_expr_stmt_assign); // should be
Damien Georgeffae48d2014-05-08 15:58:39 +00002180 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002181 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2182 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2183 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002184 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002185 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2186 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002187 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2188 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2189 // can't optimise when it's a star expression on the lhs
2190 goto no_optimisation;
2191 }
Damien429d7192013-10-04 19:53:11 +01002192 compile_node(comp, pns10->nodes[0]); // rhs
2193 compile_node(comp, pns10->nodes[1]); // rhs
2194 EMIT(rot_two);
2195 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2196 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002197 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2198 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2199 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2200 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002201 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002202 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2203 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002204 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2205 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2206 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2207 // can't optimise when it's a star expression on the lhs
2208 goto no_optimisation;
2209 }
Damien429d7192013-10-04 19:53:11 +01002210 compile_node(comp, pns10->nodes[0]); // rhs
2211 compile_node(comp, pns10->nodes[1]); // rhs
2212 compile_node(comp, pns10->nodes[2]); // rhs
2213 EMIT(rot_three);
2214 EMIT(rot_two);
2215 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2216 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2217 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2218 } else {
Damien George495d7812014-04-08 17:51:47 +01002219 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002220 compile_node(comp, pns1->nodes[0]); // rhs
2221 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2222 }
Damien429d7192013-10-04 19:53:11 +01002223 }
2224 }
2225}
2226
Damien George6be0b0a2014-08-15 14:30:52 +01002227STATIC 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 +00002228 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002229 compile_node(comp, pns->nodes[0]);
2230 for (int i = 1; i < num_nodes; i += 1) {
2231 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002232 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002233 }
2234}
2235
Damien George969a6b32014-12-10 22:07:04 +00002236STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002237 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2238 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002239
Damien George6f355fd2014-04-10 14:11:31 +01002240 uint l_fail = comp_next_label(comp);
2241 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002242 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2243 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002244 EMIT_ARG(jump, l_end);
2245 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002246 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002247 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002248 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002249}
2250
Damien George969a6b32014-12-10 22:07:04 +00002251STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002252 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002253 //mp_parse_node_t pn_params = pns->nodes[0];
2254 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002255
Damien George36db6bc2014-05-07 17:24:22 +01002256 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002257 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002258 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 +01002259 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002260 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002261 }
2262
2263 // get the scope for this lambda
2264 scope_t *this_scope = (scope_t*)pns->nodes[2];
2265
2266 // make the lambda
2267 close_over_variables_etc(comp, this_scope, 0, 0);
2268}
2269
Damien George969a6b32014-12-10 22:07:04 +00002270STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002271 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002272 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002273 for (int i = 0; i < n; i += 1) {
2274 compile_node(comp, pns->nodes[i]);
2275 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002276 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002277 }
2278 }
Damien Georgeb9791222014-01-23 00:34:21 +00002279 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002280}
2281
Damien George969a6b32014-12-10 22:07:04 +00002282STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002283 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002284 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002285 for (int i = 0; i < n; i += 1) {
2286 compile_node(comp, pns->nodes[i]);
2287 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002288 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002289 }
2290 }
Damien Georgeb9791222014-01-23 00:34:21 +00002291 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002292}
2293
Damien George969a6b32014-12-10 22:07:04 +00002294STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002295 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002296 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002297}
2298
Damien George969a6b32014-12-10 22:07:04 +00002299STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002300 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002301 compile_node(comp, pns->nodes[0]);
2302 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002303 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002304 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002305 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002306 }
2307 for (int i = 1; i + 1 < num_nodes; i += 2) {
2308 compile_node(comp, pns->nodes[i + 1]);
2309 if (i + 2 < num_nodes) {
2310 EMIT(dup_top);
2311 EMIT(rot_three);
2312 }
Damien George7e5fb242014-02-01 22:18:47 +00002313 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002314 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002315 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002316 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2317 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2318 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2319 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2320 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2321 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002322 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002323 }
2324 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002325 } else {
2326 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002327 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2328 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002329 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002330 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002331 } else {
2332 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002333 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002334 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002335 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002336 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002337 }
Damien429d7192013-10-04 19:53:11 +01002338 }
Damien429d7192013-10-04 19:53:11 +01002339 }
2340 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002341 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002342 }
2343 }
2344 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002345 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002346 EMIT_ARG(jump, l_end);
2347 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002348 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002349 EMIT(rot_two);
2350 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002351 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002352 }
2353}
2354
Damien George969a6b32014-12-10 22:07:04 +00002355STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002356 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002357}
2358
Damien George969a6b32014-12-10 22:07:04 +00002359STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002360 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002361}
2362
Damien George969a6b32014-12-10 22:07:04 +00002363STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002364 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002365}
2366
Damien George969a6b32014-12-10 22:07:04 +00002367STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002368 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002369}
2370
Damien George969a6b32014-12-10 22:07:04 +00002371STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002372 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002373 compile_node(comp, pns->nodes[0]);
2374 for (int i = 1; i + 1 < num_nodes; i += 2) {
2375 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002376 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002377 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002378 } else {
Damien George0bb97132015-02-27 14:25:47 +00002379 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2380 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002381 }
2382 }
2383}
2384
Damien George969a6b32014-12-10 22:07:04 +00002385STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002386 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002387 compile_node(comp, pns->nodes[0]);
2388 for (int i = 1; i + 1 < num_nodes; i += 2) {
2389 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002390 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002391 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002392 } else {
Damien George0bb97132015-02-27 14:25:47 +00002393 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2394 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002395 }
2396 }
2397}
2398
Damien George969a6b32014-12-10 22:07:04 +00002399STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002400 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002401 compile_node(comp, pns->nodes[0]);
2402 for (int i = 1; i + 1 < num_nodes; i += 2) {
2403 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002404 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002405 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002406 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002407 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002408 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002409 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002410 } else {
Damien George0bb97132015-02-27 14:25:47 +00002411 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2412 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002413 }
2414 }
2415}
2416
Damien George969a6b32014-12-10 22:07:04 +00002417STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002418 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002419 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002420 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002421 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002422 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002423 } else {
Damien George0bb97132015-02-27 14:25:47 +00002424 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2425 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002426 }
2427}
2428
Damien George969a6b32014-12-10 22:07:04 +00002429STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002430 // this is to handle special super() call
2431 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2432
2433 compile_generic_all_nodes(comp, pns);
2434}
2435
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002436STATIC 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 +01002437 // function to call is on top of stack
2438
Damien George35e2a4e2014-02-05 00:51:47 +00002439#if !MICROPY_EMIT_CPYTHON
2440 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002441 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 +00002442 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002443 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002444 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002445 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002446 // first argument found; load it and call super
Damien George0abb5602015-01-16 12:24:49 +00002447 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002448 EMIT_ARG(call_function, 2, 0, 0);
2449 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002450 }
2451 }
Damien George01039b52014-12-21 17:44:27 +00002452 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002453 return;
2454 }
2455#endif
2456
Damien George2c0842b2014-04-27 16:46:51 +01002457 // get the list of arguments
2458 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002459 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002460
Damien George2c0842b2014-04-27 16:46:51 +01002461 // compile the arguments
2462 // Rather than calling compile_node on the list, we go through the list of args
2463 // explicitly here so that we can count the number of arguments and give sensible
2464 // error messages.
2465 int n_positional = n_positional_extra;
2466 uint n_keyword = 0;
2467 uint star_flags = 0;
2468 for (int i = 0; i < n_args; i++) {
2469 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2470 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2471 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2472 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2473 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2474 return;
2475 }
2476 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2477 compile_node(comp, pns_arg->nodes[0]);
2478 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2479 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2480 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2481 return;
2482 }
2483 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2484 compile_node(comp, pns_arg->nodes[0]);
2485 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2486 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2487 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2488 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2489 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2490 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2491 return;
2492 }
Damien George968bf342014-04-27 19:12:05 +01002493 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002494 compile_node(comp, pns2->nodes[0]);
2495 n_keyword += 1;
2496 } else {
2497 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2498 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2499 n_positional++;
2500 }
2501 } else {
2502 goto normal_argument;
2503 }
2504 } else {
2505 normal_argument:
2506 if (n_keyword > 0) {
2507 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2508 return;
2509 }
2510 compile_node(comp, args[i]);
2511 n_positional++;
2512 }
Damien429d7192013-10-04 19:53:11 +01002513 }
2514
Damien George2c0842b2014-04-27 16:46:51 +01002515 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002516 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002517 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002518 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002519 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002520 }
Damien429d7192013-10-04 19:53:11 +01002521}
2522
Damien George969a6b32014-12-10 22:07:04 +00002523STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002524 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002525 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002526 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 +01002527 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002528 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2529 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002530 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002531 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002532 i += 1;
2533 } else {
2534 compile_node(comp, pns->nodes[i]);
2535 }
Damien George35e2a4e2014-02-05 00:51:47 +00002536 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002537 }
2538}
2539
Damien George969a6b32014-12-10 22:07:04 +00002540STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002541 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002542 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002543}
2544
Damien George969a6b32014-12-10 22:07:04 +00002545STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002546 // a list of strings
Damien63321742013-12-10 17:41:49 +00002547
2548 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002549 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002550 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002551 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002552 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002553 int pn_kind;
2554 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2555 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2556 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2557 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2558 } else {
2559 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2560 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002561 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2562 pn_kind = MP_PARSE_NODE_STRING;
2563 } else {
2564 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2565 pn_kind = MP_PARSE_NODE_BYTES;
2566 }
Damien George40f3c022014-07-03 13:25:24 +01002567 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002568 }
Damien63321742013-12-10 17:41:49 +00002569 if (i == 0) {
2570 string_kind = pn_kind;
2571 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002572 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002573 return;
2574 }
Damien429d7192013-10-04 19:53:11 +01002575 }
Damien63321742013-12-10 17:41:49 +00002576
Damien George65ef6b72015-01-14 21:17:27 +00002577 // if we are not in the last pass, just load a dummy object
2578 if (comp->pass != MP_PASS_EMIT) {
2579 EMIT_ARG(load_const_obj, mp_const_none);
2580 return;
2581 }
2582
Damien63321742013-12-10 17:41:49 +00002583 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002584 vstr_t vstr;
2585 vstr_init_len(&vstr, n_bytes);
2586 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002587 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002588 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002589 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002590 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2591 memcpy(s_dest, s, s_len);
2592 s_dest += s_len;
2593 } else {
2594 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002595 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2596 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002597 }
Damien63321742013-12-10 17:41:49 +00002598 }
Damien George65ef6b72015-01-14 21:17:27 +00002599
2600 // load the object
Damien George05005f62015-01-21 22:48:37 +00002601 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 +01002602}
2603
2604// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002605STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002606 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2607 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2608 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002609
Damien George36db6bc2014-05-07 17:24:22 +01002610 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002611 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002612 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 +01002613 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002614 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002615 }
2616
2617 // get the scope for this comprehension
2618 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2619
2620 // compile the comprehension
2621 close_over_variables_etc(comp, this_scope, 0, 0);
2622
2623 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2624 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002625 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002626}
2627
Damien George969a6b32014-12-10 22:07:04 +00002628STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002629 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002630 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002631 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2632 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2633 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2634 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2635 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2636 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2637 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002638 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002639 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002640 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002641 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002642 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002643 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002644 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002645 // generator expression
2646 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2647 } else {
2648 // tuple with 2 items
2649 goto tuple_with_2_items;
2650 }
2651 } else {
2652 // tuple with 2 items
2653 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002654 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002655 }
2656 } else {
2657 // parenthesis around a single item, is just that item
2658 compile_node(comp, pns->nodes[0]);
2659 }
2660}
2661
Damien George969a6b32014-12-10 22:07:04 +00002662STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002663 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002664 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002665 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002666 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2667 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2668 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2669 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2670 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002671 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002672 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002673 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002674 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002675 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002676 // list of many items
2677 compile_node(comp, pns2->nodes[0]);
2678 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002679 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002680 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002681 // list comprehension
2682 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2683 } else {
2684 // list with 2 items
2685 goto list_with_2_items;
2686 }
2687 } else {
2688 // list with 2 items
2689 list_with_2_items:
2690 compile_node(comp, pns2->nodes[0]);
2691 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002692 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002693 }
2694 } else {
2695 // list with 1 item
2696 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002697 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002698 }
2699}
2700
Damien George969a6b32014-12-10 22:07:04 +00002701STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002702 mp_parse_node_t pn = pns->nodes[0];
2703 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002704 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002705 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002706 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2707 pns = (mp_parse_node_struct_t*)pn;
2708 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002709 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002710 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002711 compile_node(comp, pn);
2712 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002713 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2714 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2715 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2716 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002717 // dict/set with multiple elements
2718
2719 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002720 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002721 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002722
2723 // first element sets whether it's a dict or set
2724 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002725 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002726 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002727 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002728 compile_node(comp, pns->nodes[0]);
2729 EMIT(store_map);
2730 is_dict = true;
2731 } else {
2732 // a set
2733 compile_node(comp, pns->nodes[0]); // 1st value of set
2734 is_dict = false;
2735 }
2736
2737 // process rest of elements
2738 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002739 mp_parse_node_t pn_i = nodes[i];
2740 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2741 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002742 if (is_dict) {
2743 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002744 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002745 return;
2746 }
2747 EMIT(store_map);
2748 } else {
2749 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002750 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002751 return;
2752 }
2753 }
2754 }
2755
Damien Georgee37dcaa2014-12-27 17:07:16 +00002756 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002757 // if it's a set, build it
2758 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002759 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002760 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002761 #endif
Damien George0bb97132015-02-27 14:25:47 +00002762 } else {
2763 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002764 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002765 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002766 // a dictionary comprehension
2767 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2768 } else {
2769 // a set comprehension
2770 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2771 }
Damien429d7192013-10-04 19:53:11 +01002772 }
2773 } else {
2774 // set with one element
2775 goto set_with_one_element;
2776 }
2777 } else {
2778 // set with one element
2779 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002780 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002781 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002782 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002783 #else
2784 assert(0);
2785 #endif
Damien429d7192013-10-04 19:53:11 +01002786 }
2787}
2788
Damien George969a6b32014-12-10 22:07:04 +00002789STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002790 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002791}
2792
Damien George969a6b32014-12-10 22:07:04 +00002793STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002794 // object who's index we want is on top of stack
2795 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002796 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002797}
2798
Damien George969a6b32014-12-10 22:07:04 +00002799STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002800 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002801 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002802}
2803
Damien George83204f32014-12-27 17:20:41 +00002804#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002805STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002806 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2807 mp_parse_node_t pn = pns->nodes[0];
2808 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002809 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002810 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2811 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002812 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2813 pns = (mp_parse_node_struct_t*)pn;
2814 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002815 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002816 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002817 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002818 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002819 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002820 } else {
2821 // [?::x]
2822 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002823 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002824 }
Damiend99b0522013-12-21 18:17:45 +00002825 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002826 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002827 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2828 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2829 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2830 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002831 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002832 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002833 } else {
2834 // [?:x:x]
2835 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002836 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002837 }
2838 } else {
2839 // [?:x]
2840 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002841 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002842 }
2843 } else {
2844 // [?:x]
2845 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002846 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002847 }
2848}
2849
Damien George969a6b32014-12-10 22:07:04 +00002850STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002851 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002852 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2853 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002854}
2855
Damien George969a6b32014-12-10 22:07:04 +00002856STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002857 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002858 compile_subscript_3_helper(comp, pns);
2859}
Damien George83204f32014-12-27 17:20:41 +00002860#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002861
Damien George969a6b32014-12-10 22:07:04 +00002862STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002863 // if this is called then we are compiling a dict key:value pair
2864 compile_node(comp, pns->nodes[1]); // value
2865 compile_node(comp, pns->nodes[0]); // key
2866}
2867
Damien George969a6b32014-12-10 22:07:04 +00002868STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002869 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002870 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002871 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002872}
2873
Damien George969a6b32014-12-10 22:07:04 +00002874STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002875 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002876 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002877 return;
2878 }
Damiend99b0522013-12-21 18:17:45 +00002879 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002880 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002881 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002882 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2883 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002884 compile_node(comp, pns->nodes[0]);
2885 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002886 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002887 EMIT(yield_from);
2888 } else {
2889 compile_node(comp, pns->nodes[0]);
2890 EMIT(yield_value);
2891 }
2892}
2893
Damien George65ef6b72015-01-14 21:17:27 +00002894STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2895 // only create and load the actual str object on the last pass
2896 if (comp->pass != MP_PASS_EMIT) {
2897 EMIT_ARG(load_const_obj, mp_const_none);
2898 } else {
2899 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2900 }
2901}
2902
2903STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2904 // only create and load the actual bytes object on the last pass
2905 if (comp->pass != MP_PASS_EMIT) {
2906 EMIT_ARG(load_const_obj, mp_const_none);
2907 } else {
2908 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2909 }
2910}
2911
Damien George7d414a12015-02-08 01:57:40 +00002912STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2913 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2914}
2915
Damiend99b0522013-12-21 18:17:45 +00002916typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002917STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002918#define nc NULL
2919#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002920#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002921#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002922#undef nc
2923#undef c
2924#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002925 NULL,
2926 compile_string,
2927 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002928 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002929};
2930
Damien George6be0b0a2014-08-15 14:30:52 +01002931STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002932 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002933 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002934 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002935 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002936 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002937 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002938 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002939 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002940 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002941 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2942 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damien Georged2d64f02015-01-14 21:32:42 +00002943 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002944 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002945 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002946 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002947 // do nothing
2948 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002949 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002950 }
2951 break;
Damien429d7192013-10-04 19:53:11 +01002952 }
2953 } else {
Damiend99b0522013-12-21 18:17:45 +00002954 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002955 EMIT_ARG(set_line_number, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002956 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2957 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002958#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002959 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2960 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002961#endif
Damien George65ef6b72015-01-14 21:17:27 +00002962 compile_syntax_error(comp, pn, "internal compiler error");
2963 } else {
2964 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002965 }
2966 }
2967}
2968
Damien George2ac4af62014-08-15 16:45:41 +01002969STATIC 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 +01002970 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002971 qstr param_name = MP_QSTR_NULL;
2972 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002973 if (MP_PARSE_NODE_IS_ID(pn)) {
2974 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002975 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002976 // comes after a star, so counts as a keyword-only parameter
2977 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002978 } else {
Damien George2827d622014-04-27 15:50:52 +01002979 // comes before a star, so counts as a positional parameter
2980 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002981 }
Damienb14de212013-10-06 00:28:28 +01002982 } else {
Damiend99b0522013-12-21 18:17:45 +00002983 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2984 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2985 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2986 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002987 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002988 // comes after a star, so counts as a keyword-only parameter
2989 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002990 } else {
Damien George2827d622014-04-27 15:50:52 +01002991 // comes before a star, so counts as a positional parameter
2992 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002993 }
Damiend99b0522013-12-21 18:17:45 +00002994 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002995 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002996 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002997 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002998 // bare star
2999 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003000 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003001 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003002 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003003 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003004 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00003005 } else {
3006 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01003007 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003008 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003009 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3010 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003011 }
Damien George0bb97132015-02-27 14:25:47 +00003012 } else {
3013 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00003014 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003015 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003016 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003017 }
Damien429d7192013-10-04 19:53:11 +01003018 }
3019
Damien George2827d622014-04-27 15:50:52 +01003020 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003021 bool added;
3022 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3023 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003024 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003025 return;
3026 }
Damien429d7192013-10-04 19:53:11 +01003027 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003028 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003029 }
3030}
3031
Damien George2827d622014-04-27 15:50:52 +01003032STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003033 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003034}
3035
Damien George2827d622014-04-27 15:50:52 +01003036STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003037 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3038}
3039
3040STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3041 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3042 // no annotation
3043 return;
3044 }
3045
3046 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3047 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3048 // named parameter with possible annotation
3049 // fallthrough
3050 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3051 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3052 // named star with possible annotation
3053 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3054 // fallthrough
3055 } else {
3056 // no annotation
3057 return;
3058 }
3059 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3060 // double star with possible annotation
3061 // fallthrough
3062 } else {
3063 // no annotation
3064 return;
3065 }
3066
3067 mp_parse_node_t pn_annotation = pns->nodes[1];
3068
3069 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3070 #if MICROPY_EMIT_NATIVE
3071 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3072 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3073 assert(id_info != NULL);
3074
3075 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3076 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3077 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3078 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3079 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003080 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003081 }
3082 }
3083 #endif // MICROPY_EMIT_NATIVE
3084 }
Damien429d7192013-10-04 19:53:11 +01003085}
3086
Damien George6be0b0a2014-08-15 14:30:52 +01003087STATIC 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 +01003088 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003089 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003090 // no more nested if/for; compile inner expression
3091 compile_node(comp, pn_inner_expr);
3092 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003093 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003094 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003095 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003096 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003097 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003098 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003099 #endif
Damien429d7192013-10-04 19:53:11 +01003100 } else {
3101 EMIT(yield_value);
3102 EMIT(pop_top);
3103 }
Damiend99b0522013-12-21 18:17:45 +00003104 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003105 // if condition
Damiend99b0522013-12-21 18:17:45 +00003106 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003107 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3108 pn_iter = pns_comp_if->nodes[1];
3109 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00003110 } else {
3111 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01003112 // for loop
Damiend99b0522013-12-21 18:17:45 +00003113 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003114 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003115 uint l_end2 = comp_next_label(comp);
3116 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003117 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003118 EMIT_ARG(label_assign, l_top2);
3119 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003120 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3121 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 +00003122 EMIT_ARG(jump, l_top2);
3123 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003124 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01003125 }
3126}
3127
Damien George1463c1f2014-04-25 23:52:57 +01003128STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3129#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003130 // see http://www.python.org/dev/peps/pep-0257/
3131
3132 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003133 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003134 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003135 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003136 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003137 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3138 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003139 for (int i = 0; i < num_nodes; i++) {
3140 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003141 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 +00003142 // not a newline, so this is the first statement; finish search
3143 break;
3144 }
3145 }
3146 // 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 +00003147 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003148 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003149 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003150 } else {
3151 return;
3152 }
3153
3154 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003155 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3156 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003157 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3158 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3159 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3160 // compile the doc string
3161 compile_node(comp, pns->nodes[0]);
3162 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003163 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003164 }
3165 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003166#else
3167 (void)comp;
3168 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003169#endif
Damien429d7192013-10-04 19:53:11 +01003170}
3171
Damien George1463c1f2014-04-25 23:52:57 +01003172STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003173 comp->pass = pass;
3174 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003175 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003176 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003177
Damien George36db6bc2014-05-07 17:24:22 +01003178 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003179 // reset maximum stack sizes in scope
3180 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003181 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003182 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003183 }
3184
Damien5ac1b2e2013-10-18 19:58:12 +01003185#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003186 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003187 scope_print_info(scope);
3188 }
Damien5ac1b2e2013-10-18 19:58:12 +01003189#endif
Damien429d7192013-10-04 19:53:11 +01003190
3191 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003192 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3193 assert(scope->kind == SCOPE_MODULE);
3194 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3195 compile_node(comp, pns->nodes[0]); // compile the expression
3196 EMIT(return_value);
3197 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003198 if (!comp->is_repl) {
3199 check_for_doc_string(comp, scope->pn);
3200 }
Damien429d7192013-10-04 19:53:11 +01003201 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003202 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003203 EMIT(return_value);
3204 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003205 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3206 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3207 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003208
3209 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003210 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003211 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003212 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003213 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003214 } else {
3215 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003216
Damien George2ac4af62014-08-15 16:45:41 +01003217 // argument annotations
3218 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3219
3220 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003221 mp_parse_node_t pn_annotation = pns->nodes[2];
3222 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3223 #if MICROPY_EMIT_NATIVE
3224 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3225 // nodes[2] can be null or a test-expr
3226 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3227 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3228 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3229 } else {
3230 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3231 }
Damien George2ac4af62014-08-15 16:45:41 +01003232 }
Damien Georgea5190a72014-08-15 22:39:08 +01003233 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003234 }
Damien George2ac4af62014-08-15 16:45:41 +01003235 }
Damien429d7192013-10-04 19:53:11 +01003236
3237 compile_node(comp, pns->nodes[3]); // 3 is function body
3238 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003239 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003240 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003241 EMIT(return_value);
3242 }
3243 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003244 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3245 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3246 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003247
3248 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003249 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003250 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003251 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003252 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3253 }
3254
3255 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003256
3257 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3258 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3259 EMIT(pop_top);
3260 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3261 }
Damien429d7192013-10-04 19:53:11 +01003262 EMIT(return_value);
3263 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3264 // a bit of a hack at the moment
3265
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_NUM_NODES(pns) == 2);
3269 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3270 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003271
Damien George708c0732014-04-27 19:23:46 +01003272 // We need a unique name for the comprehension argument (the iterator).
3273 // CPython uses .0, but we should be able to use anything that won't
3274 // clash with a user defined variable. Best to use an existing qstr,
3275 // so we use the blank qstr.
3276#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003277 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003278#else
3279 qstr qstr_arg = MP_QSTR_;
3280#endif
Damien George36db6bc2014-05-07 17:24:22 +01003281 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003282 bool added;
3283 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3284 assert(added);
3285 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003286 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003287 }
3288
3289 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003290 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003291 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003292 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003293 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003294 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003295 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003296 #endif
Damien429d7192013-10-04 19:53:11 +01003297 }
3298
Damien George6f355fd2014-04-10 14:11:31 +01003299 uint l_end = comp_next_label(comp);
3300 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003301 EMIT_ARG(load_id, qstr_arg);
3302 EMIT_ARG(label_assign, l_top);
3303 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003304 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3305 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003306 EMIT_ARG(jump, l_top);
3307 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003308 EMIT(for_iter_end);
3309
3310 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003311 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003312 }
3313 EMIT(return_value);
3314 } else {
3315 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003316 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3317 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3318 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003319
Damien George36db6bc2014-05-07 17:24:22 +01003320 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003321 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003322 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003323 assert(added);
3324 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003325 }
3326
Damien Georgeb9791222014-01-23 00:34:21 +00003327 EMIT_ARG(load_id, MP_QSTR___name__);
3328 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003329 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 +00003330 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003331
3332 check_for_doc_string(comp, pns->nodes[2]);
3333 compile_node(comp, pns->nodes[2]); // 2 is class body
3334
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003335 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003336 assert(id != NULL);
3337 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003338 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003339 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003340#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003341 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003342#else
Damien George0abb5602015-01-16 12:24:49 +00003343 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003344#endif
Damien429d7192013-10-04 19:53:11 +01003345 }
3346 EMIT(return_value);
3347 }
3348
Damien415eb6f2013-10-05 12:19:06 +01003349 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003350
3351 // make sure we match all the exception levels
3352 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003353}
3354
Damien George094d4502014-04-02 17:31:27 +01003355#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003356// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003357STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003358 comp->pass = pass;
3359 comp->scope_cur = scope;
3360 comp->next_label = 1;
3361
3362 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003363 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003364 return;
3365 }
3366
Damien George36db6bc2014-05-07 17:24:22 +01003367 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003368 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003369 }
3370
Damien826005c2013-10-05 23:17:28 +01003371 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003372 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3373 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3374 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003375
Damiend99b0522013-12-21 18:17:45 +00003376 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003377
Damiena2f2f7d2013-10-06 00:14:13 +01003378 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003379 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003380 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003381 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003382 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003383 if (comp->compile_error != MP_OBJ_NULL) {
3384 goto inline_asm_error;
3385 }
Damiena2f2f7d2013-10-06 00:14:13 +01003386 }
3387
Damiend99b0522013-12-21 18:17:45 +00003388 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003389
Damiend99b0522013-12-21 18:17:45 +00003390 mp_parse_node_t pn_body = pns->nodes[3]; // body
3391 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003392 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003393
Damien Georgecbd2f742014-01-19 11:48:48 +00003394 /*
Damien George36db6bc2014-05-07 17:24:22 +01003395 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003396 //printf("----\n");
3397 scope_print_info(scope);
3398 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003399 */
Damien826005c2013-10-05 23:17:28 +01003400
3401 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003402 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3403 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003404 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3405 // no instructions
3406 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003407 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003408 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003409 not_an_instruction:
Damien Georgea26dc502014-04-12 17:54:52 +01003410 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3411 return;
3412 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003413
3414 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003415 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003416 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3417 goto not_an_instruction;
3418 }
Damiend99b0522013-12-21 18:17:45 +00003419 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003420 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3421 goto not_an_instruction;
3422 }
3423 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3424 goto not_an_instruction;
3425 }
3426 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3427 goto not_an_instruction;
3428 }
Damiend99b0522013-12-21 18:17:45 +00003429 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003430
3431 // parse node looks like an instruction
3432 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003433 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3434 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3435 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003436 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003437
3438 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003439 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003440 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003441 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003442 return;
3443 }
Damien George6f355fd2014-04-10 14:11:31 +01003444 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003445 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003446 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003447 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003448 } else if (op == MP_QSTR_align) {
3449 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3450 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3451 return;
3452 }
Damien George36db6bc2014-05-07 17:24:22 +01003453 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003454 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3455 }
3456 } else if (op == MP_QSTR_data) {
3457 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3458 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3459 return;
3460 }
Damien George36db6bc2014-05-07 17:24:22 +01003461 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003462 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003463 for (uint j = 1; j < n_args; j++) {
3464 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003465 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3466 return;
3467 }
Damien George50912e72015-01-20 11:55:10 +00003468 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003469 }
3470 }
Damien826005c2013-10-05 23:17:28 +01003471 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003472 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003473 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003474 }
3475 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003476
3477 if (comp->compile_error != MP_OBJ_NULL) {
3478 pns = pns2; // this is the parse node that had the error
3479 goto inline_asm_error;
3480 }
Damien826005c2013-10-05 23:17:28 +01003481 }
3482
Damien George36db6bc2014-05-07 17:24:22 +01003483 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003484 EMIT_INLINE_ASM(end_pass);
3485 }
3486
3487 if (comp->compile_error != MP_OBJ_NULL) {
3488 // inline assembler had an error; add traceback to its exception
3489 inline_asm_error:
3490 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, (mp_uint_t)pns->source_line, comp->scope_cur->simple_name);
Damienb05d7072013-10-05 13:37:10 +01003491 }
Damien429d7192013-10-04 19:53:11 +01003492}
Damien George094d4502014-04-02 17:31:27 +01003493#endif
Damien429d7192013-10-04 19:53:11 +01003494
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003495STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003496#if !MICROPY_EMIT_CPYTHON
3497 // in Micro Python we put the *x parameter after all other parameters (except **y)
3498 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3499 id_info_t *id_param = NULL;
3500 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3501 id_info_t *id = &scope->id_info[i];
3502 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3503 if (id_param != NULL) {
3504 // swap star param with last param
3505 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3506 }
3507 break;
3508 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3509 id_param = id;
3510 }
3511 }
3512 }
3513#endif
3514
Damien429d7192013-10-04 19:53:11 +01003515 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003516 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003517 scope->num_locals = 0;
3518 for (int i = 0; i < scope->id_info_len; i++) {
3519 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003520 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003521 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3522 continue;
3523 }
3524 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3525 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3526 }
Damien George2827d622014-04-27 15:50:52 +01003527 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003528 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003529 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003530 }
3531 }
3532
3533 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003534#if MICROPY_EMIT_CPYTHON
3535 int num_cell = 0;
3536#endif
Damien9ecbcff2013-12-11 00:41:43 +00003537 for (int i = 0; i < scope->id_info_len; i++) {
3538 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003539#if MICROPY_EMIT_CPYTHON
3540 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003541 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003542 id->local_num = num_cell;
3543 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003544 }
Damien George6baf76e2013-12-30 22:32:17 +00003545#else
3546 // in Micro Python the cells come right after the fast locals
3547 // parameters are not counted here, since they remain at the start
3548 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003549 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003550 id->local_num = scope->num_locals;
3551 scope->num_locals += 1;
3552 }
3553#endif
Damien9ecbcff2013-12-11 00:41:43 +00003554 }
Damien9ecbcff2013-12-11 00:41:43 +00003555
3556 // compute the index of free vars (freevars[idx] in CPython)
3557 // make sure they are in the order of the parent scope
3558 if (scope->parent != NULL) {
3559 int num_free = 0;
3560 for (int i = 0; i < scope->parent->id_info_len; i++) {
3561 id_info_t *id = &scope->parent->id_info[i];
3562 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3563 for (int j = 0; j < scope->id_info_len; j++) {
3564 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003565 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003566 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003567#if MICROPY_EMIT_CPYTHON
3568 // in CPython the frees are numbered after the cells
3569 id2->local_num = num_cell + num_free;
3570#else
3571 // in Micro Python the frees come first, before the params
3572 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003573#endif
3574 num_free += 1;
3575 }
3576 }
3577 }
Damien429d7192013-10-04 19:53:11 +01003578 }
Damien George6baf76e2013-12-30 22:32:17 +00003579#if !MICROPY_EMIT_CPYTHON
3580 // in Micro Python shift all other locals after the free locals
3581 if (num_free > 0) {
3582 for (int i = 0; i < scope->id_info_len; i++) {
3583 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003584 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003585 id->local_num += num_free;
3586 }
3587 }
Damien George2827d622014-04-27 15:50:52 +01003588 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 +00003589 scope->num_locals += num_free;
3590 }
3591#endif
Damien429d7192013-10-04 19:53:11 +01003592 }
3593
Damien George8725f8f2014-02-15 19:33:11 +00003594 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003595
3596#if MICROPY_EMIT_CPYTHON
3597 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003598 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) {
3599 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003600 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003601 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003602 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 +00003603 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003604 }
3605 }
Damien George882b3632014-04-02 15:56:31 +01003606#endif
3607
Damien429d7192013-10-04 19:53:11 +01003608 int num_free = 0;
3609 for (int i = 0; i < scope->id_info_len; i++) {
3610 id_info_t *id = &scope->id_info[i];
3611 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3612 num_free += 1;
3613 }
3614 }
3615 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003616 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003617 }
3618}
3619
Damien George65cad122014-04-06 11:48:15 +01003620mp_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 +01003621 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003622 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003623 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003624 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003625
Damien826005c2013-10-05 23:17:28 +01003626 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003627 mp_map_t consts;
3628 mp_map_init(&consts, 0);
3629 pn = fold_constants(comp, pn, &consts);
3630 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003631
3632 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003633 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003634
Damien826005c2013-10-05 23:17:28 +01003635 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003636 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003637 comp->emit_method_table = &emit_pass1_method_table;
3638 comp->emit_inline_asm = NULL;
3639 comp->emit_inline_asm_method_table = NULL;
3640 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003641 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003642 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003643#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003644 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003645 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003646#endif
Damien826005c2013-10-05 23:17:28 +01003647 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003648 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003649 }
3650
3651 // update maximim number of labels needed
3652 if (comp->next_label > max_num_labels) {
3653 max_num_labels = comp->next_label;
3654 }
Damien429d7192013-10-04 19:53:11 +01003655 }
3656
Damien826005c2013-10-05 23:17:28 +01003657 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003658 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 +00003659 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003660 }
3661
Damien826005c2013-10-05 23:17:28 +01003662 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003663 emit_pass1_free(comp->emit);
3664
Damien826005c2013-10-05 23:17:28 +01003665 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003666#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003667 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003668#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003669 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003670#endif
Damien3ef4abb2013-10-12 16:53:13 +01003671#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003672 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003673#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003674#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003675 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003676 if (false) {
3677 // dummy
3678
Damien3ef4abb2013-10-12 16:53:13 +01003679#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003680 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003681 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003682 if (emit_inline_thumb == NULL) {
3683 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3684 }
3685 comp->emit = NULL;
3686 comp->emit_method_table = NULL;
3687 comp->emit_inline_asm = emit_inline_thumb;
3688 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003689 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003690 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003691 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003692 }
Damienc025ebb2013-10-12 14:30:21 +01003693#endif
3694
Damien826005c2013-10-05 23:17:28 +01003695 } else {
Damienc025ebb2013-10-12 14:30:21 +01003696
3697 // choose the emit type
3698
Damien3ef4abb2013-10-12 16:53:13 +01003699#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003700 comp->emit = emit_cpython_new(max_num_labels);
3701 comp->emit_method_table = &emit_cpython_method_table;
3702#else
Damien826005c2013-10-05 23:17:28 +01003703 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003704
3705#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003706 case MP_EMIT_OPT_NATIVE_PYTHON:
3707 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003708#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003709 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003710 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003711 }
Damien13ed3a62013-10-08 09:05:10 +01003712 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003713#elif MICROPY_EMIT_X86
3714 if (emit_native == NULL) {
3715 emit_native = emit_native_x86_new(max_num_labels);
3716 }
3717 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003718#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003719 if (emit_native == NULL) {
3720 emit_native = emit_native_thumb_new(max_num_labels);
3721 }
3722 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003723#elif MICROPY_EMIT_ARM
3724 if (emit_native == NULL) {
3725 emit_native = emit_native_arm_new(max_num_labels);
3726 }
3727 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003728#endif
3729 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003730 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003731 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003732#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003733
Damien826005c2013-10-05 23:17:28 +01003734 default:
3735 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003736 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003737 }
3738 comp->emit = emit_bc;
3739 comp->emit_method_table = &emit_bc_method_table;
3740 break;
3741 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003742#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003743
Damien George1e1779e2015-01-14 00:20:28 +00003744 // need a pass to compute stack size
3745 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3746
Damien George36db6bc2014-05-07 17:24:22 +01003747 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003748 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003749 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3750 }
3751
3752 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003753 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003754 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003755 }
Damien6cdd3af2013-10-05 18:08:26 +01003756 }
Damien429d7192013-10-04 19:53:11 +01003757 }
3758
Damien George41d02b62014-01-24 22:42:28 +00003759 // free the emitters
3760#if !MICROPY_EMIT_CPYTHON
3761 if (emit_bc != NULL) {
3762 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003763 }
Damien George41d02b62014-01-24 22:42:28 +00003764#if MICROPY_EMIT_NATIVE
3765 if (emit_native != NULL) {
3766#if MICROPY_EMIT_X64
3767 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003768#elif MICROPY_EMIT_X86
3769 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003770#elif MICROPY_EMIT_THUMB
3771 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003772#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003773 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003774#endif
3775 }
3776#endif
3777#if MICROPY_EMIT_INLINE_THUMB
3778 if (emit_inline_thumb != NULL) {
3779 emit_inline_thumb_free(emit_inline_thumb);
3780 }
3781#endif
3782#endif // !MICROPY_EMIT_CPYTHON
3783
Damien George52b5d762014-09-23 15:31:56 +00003784 // free the parse tree
3785 mp_parse_node_free(pn);
3786
Damien George41d02b62014-01-24 22:42:28 +00003787 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003788 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003789 for (scope_t *s = module_scope; s;) {
3790 scope_t *next = s->next;
3791 scope_free(s);
3792 s = next;
3793 }
Damien5ac1b2e2013-10-18 19:58:12 +01003794
Damien George41d02b62014-01-24 22:42:28 +00003795 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003796 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003797 m_del_obj(compiler_t, comp);
3798
Damien Georgea91ac202014-10-05 19:01:34 +01003799 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003800 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003801 } else {
3802#if MICROPY_EMIT_CPYTHON
3803 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003804 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003805 return mp_const_true;
3806#else
3807 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003808 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003809#endif
3810 }
Damien429d7192013-10-04 19:53:11 +01003811}