blob: 7245ff0a343832c9e43eaaa90320e7ad6cf5613b [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);
Damien George63f38322015-02-28 15:04:06 +0000642 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100643}
Damien3a205172013-10-12 15:01:56 +0100644#endif
Damien429d7192013-10-04 19:53:11 +0100645
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200646STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100647#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100648 cpython_c_if_cond(comp, pn, jump_if, label, false);
649#else
650 if (node_is_const_false(pn)) {
651 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000652 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100653 }
654 return;
655 } else if (node_is_const_true(pn)) {
656 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000657 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100658 }
659 return;
Damiend99b0522013-12-21 18:17:45 +0000660 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
661 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
662 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
663 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100664 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000665 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100666 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100667 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000668 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100669 }
Damien George0b2fd912015-02-28 14:37:54 +0000670 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000671 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100672 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000673 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100674 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000675 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100676 }
677 }
678 return;
Damiend99b0522013-12-21 18:17:45 +0000679 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100680 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000681 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100682 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000683 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100684 }
Damiend99b0522013-12-21 18:17:45 +0000685 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100686 c_if_cond(comp, pns->nodes[0], !jump_if, label);
687 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100688 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
689 // cond is something in parenthesis
690 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
691 // empty tuple, acts as false for the condition
692 if (jump_if == false) {
693 EMIT_ARG(jump, label);
694 }
695 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
696 // non-empty tuple, acts as true for the condition
697 if (jump_if == true) {
698 EMIT_ARG(jump, label);
699 }
700 } else {
701 // parenthesis around 1 item, is just that item
702 c_if_cond(comp, pns->nodes[0], jump_if, label);
703 }
704 return;
Damien3a205172013-10-12 15:01:56 +0100705 }
706 }
707
708 // nothing special, fall back to default compiling for node and jump
709 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000710 EMIT_ARG(pop_jump_if, jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100711#endif
Damien429d7192013-10-04 19:53:11 +0100712}
713
714typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100715STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100716
Damien George6be0b0a2014-08-15 14:30:52 +0100717STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100718 if (assign_kind != ASSIGN_AUG_STORE) {
719 compile_node(comp, pns->nodes[0]);
720 }
721
Damiend99b0522013-12-21 18:17:45 +0000722 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
723 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
724 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
725 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100726 if (assign_kind != ASSIGN_AUG_STORE) {
727 for (int i = 0; i < n - 1; i++) {
728 compile_node(comp, pns1->nodes[i]);
729 }
730 }
Damiend99b0522013-12-21 18:17:45 +0000731 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
732 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100733 }
Damiend99b0522013-12-21 18:17:45 +0000734 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100735 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000736 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100737 if (assign_kind == ASSIGN_AUG_STORE) {
738 EMIT(rot_three);
739 EMIT(store_subscr);
740 } else {
741 compile_node(comp, pns1->nodes[0]);
742 if (assign_kind == ASSIGN_AUG_LOAD) {
743 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100744 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100745 } else {
746 EMIT(store_subscr);
747 }
748 }
Damiend99b0522013-12-21 18:17:45 +0000749 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
750 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100751 if (assign_kind == ASSIGN_AUG_LOAD) {
752 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000753 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100754 } else {
755 if (assign_kind == ASSIGN_AUG_STORE) {
756 EMIT(rot_two);
757 }
Damien Georgeb9791222014-01-23 00:34:21 +0000758 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100759 }
760 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100761 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100762 }
763 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100764 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100765 }
766
Damiend99b0522013-12-21 18:17:45 +0000767 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100768 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100769 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100770
771 return;
772
773cannot_assign:
774 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100775}
776
Damien George0288cf02014-04-11 11:53:00 +0000777// 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 +0100778STATIC 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 +0000779 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
780
781 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000782 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000783 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
784 EMIT_ARG(unpack_ex, 0, num_tail);
785 have_star_index = 0;
786 }
Damien George963a5a32015-01-16 17:47:07 +0000787 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000788 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000789 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000790 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
791 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100792 } else {
Damien George9d181f62014-04-27 16:55:27 +0100793 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100794 return;
795 }
796 }
797 }
Damien George963a5a32015-01-16 17:47:07 +0000798 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000799 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100800 }
Damien George0288cf02014-04-11 11:53:00 +0000801 if (num_head != 0) {
802 if (0 == have_star_index) {
803 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100804 } else {
Damien George0288cf02014-04-11 11:53:00 +0000805 c_assign(comp, node_head, ASSIGN_STORE);
806 }
807 }
Damien George963a5a32015-01-16 17:47:07 +0000808 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000809 if (num_head + i == have_star_index) {
810 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
811 } else {
812 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100813 }
814 }
815}
816
817// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100818STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100819 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000820 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100821 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000822 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
823 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000824 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100825 switch (assign_kind) {
826 case ASSIGN_STORE:
827 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000828 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100829 break;
830 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000831 default:
Damien Georgeb9791222014-01-23 00:34:21 +0000832 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100833 break;
834 }
835 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100836 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100837 return;
838 }
839 } else {
Damiend99b0522013-12-21 18:17:45 +0000840 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
841 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100842 case PN_power:
843 // lhs is an index or attribute
844 c_assign_power(comp, pns, assign_kind);
845 break;
846
847 case PN_testlist_star_expr:
848 case PN_exprlist:
849 // lhs is a tuple
850 if (assign_kind != ASSIGN_STORE) {
851 goto bad_aug;
852 }
Damien George0288cf02014-04-11 11:53:00 +0000853 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100854 break;
855
856 case PN_atom_paren:
857 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000858 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100859 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100860 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000861 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
862 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100863 goto testlist_comp;
864 } else {
865 // parenthesis around 1 item, is just that item
866 pn = pns->nodes[0];
867 goto tail_recursion;
868 }
869 break;
870
871 case PN_atom_bracket:
872 // lhs is something in brackets
873 if (assign_kind != ASSIGN_STORE) {
874 goto bad_aug;
875 }
Damiend99b0522013-12-21 18:17:45 +0000876 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100877 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000878 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000879 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
880 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100881 goto testlist_comp;
882 } else {
883 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000884 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100885 }
886 break;
887
888 default:
Damien George9d181f62014-04-27 16:55:27 +0100889 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100890 }
891 return;
892
893 testlist_comp:
894 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000895 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
896 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
897 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100898 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000899 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000900 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000901 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100902 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000903 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
904 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000905 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100906 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100907 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100908 } else {
909 // sequence with 2 items
910 goto sequence_with_2_items;
911 }
912 } else {
913 // sequence with 2 items
914 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000915 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100916 }
917 return;
918 }
919 return;
920
Damien George9d181f62014-04-27 16:55:27 +0100921 cannot_assign:
922 compile_syntax_error(comp, pn, "can't assign to expression");
923 return;
924
Damien429d7192013-10-04 19:53:11 +0100925 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100926 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100927}
928
929// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100930// if we are not in CPython compatibility mode then:
931// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
932// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
933// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100934STATIC 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 +0100935 assert(n_pos_defaults >= 0);
936 assert(n_kw_defaults >= 0);
937
Damien429d7192013-10-04 19:53:11 +0100938 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000939 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100940 int nfree = 0;
941 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000942 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
943 id_info_t *id = &comp->scope_cur->id_info[i];
944 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
945 for (int j = 0; j < this_scope->id_info_len; j++) {
946 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100947 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000948#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100949 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000950#else
951 // in Micro Python we load closures using LOAD_FAST
Damien George0abb5602015-01-16 12:24:49 +0000952 EMIT_ARG(load_fast, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000953#endif
Damien318aec62013-12-10 18:28:17 +0000954 nfree += 1;
955 }
956 }
Damien429d7192013-10-04 19:53:11 +0100957 }
958 }
959 }
Damien429d7192013-10-04 19:53:11 +0100960
961 // make the function/closure
962 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100963 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100964 } else {
Damien George3558f622014-04-20 17:50:40 +0100965 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100966 }
967}
968
Damien George6be0b0a2014-08-15 14:30:52 +0100969STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000970 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100971 comp->have_star = true;
972 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000973 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
974 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100975 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100976 } else {
977 // named star
Damien429d7192013-10-04 19:53:11 +0100978 }
Damien George8b19db02014-04-11 23:25:34 +0100979 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000980
981 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100982 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000983 // TODO do we need to do anything with this?
984
985 } else {
986 mp_parse_node_t pn_id;
987 mp_parse_node_t pn_colon;
988 mp_parse_node_t pn_equal;
989 if (MP_PARSE_NODE_IS_ID(pn)) {
990 // this parameter is just an id
991
992 pn_id = pn;
993 pn_colon = MP_PARSE_NODE_NULL;
994 pn_equal = MP_PARSE_NODE_NULL;
995
Damien George0bb97132015-02-27 14:25:47 +0000996 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000997 // this parameter has a colon and/or equal specifier
998
Damien George0bb97132015-02-27 14:25:47 +0000999 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1000
Damien Georgef41fdd02014-03-03 23:19:11 +00001001 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1002 pn_id = pns->nodes[0];
1003 pn_colon = pns->nodes[1];
1004 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +00001005 }
1006
1007 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1008 // this parameter does not have a default value
1009
1010 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001011 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001012 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001013 return;
1014 }
1015
1016 } else {
1017 // this parameter has a default value
1018 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1019
Damien George8b19db02014-04-11 23:25:34 +01001020 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001021 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001022#if MICROPY_EMIT_CPYTHON
1023 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1024 compile_node(comp, pn_equal);
1025#else
Damien George69b89d22014-04-11 13:38:30 +00001026 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1027 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001028 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1029 // we need to do this here before we start building the map for the default keywords
1030 if (comp->num_default_params > 0) {
1031 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001032 } else {
1033 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001034 }
Damien George69b89d22014-04-11 13:38:30 +00001035 // first default dict param, so make the map
1036 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001037 }
Damien Georgef0778a72014-06-07 22:01:00 +01001038
1039 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001040 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001041 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001042 EMIT(store_map);
1043#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001044 } else {
Damien George69b89d22014-04-11 13:38:30 +00001045 comp->num_default_params += 1;
1046 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001047 }
1048 }
1049
1050 // TODO pn_colon not implemented
1051 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001052 }
1053}
1054
1055// leaves function object on stack
1056// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001057STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001058 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001059 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001060 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001061 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001062 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001063 }
1064
1065 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001066 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001067 uint old_num_dict_params = comp->num_dict_params;
1068 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001069
1070 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001071 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001072 comp->num_dict_params = 0;
1073 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001074 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001075
Damien Georgea91ac202014-10-05 19:01:34 +01001076 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001077 return MP_QSTR_NULL;
1078 }
1079
Damien Georgee337f1e2014-03-31 15:18:37 +01001080#if !MICROPY_EMIT_CPYTHON
1081 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001082 // the default keywords args may have already made the tuple; if not, do it now
1083 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001084 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001085 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001086 }
1087#endif
1088
Damien429d7192013-10-04 19:53:11 +01001089 // get the scope for this function
1090 scope_t *fscope = (scope_t*)pns->nodes[4];
1091
1092 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001093 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001094
1095 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001096 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001097 comp->num_dict_params = old_num_dict_params;
1098 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001099
1100 // return its name (the 'f' in "def f(...):")
1101 return fscope->simple_name;
1102}
1103
1104// leaves class object on stack
1105// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001106STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001107 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001108 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001109 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001110 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001111 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001112 }
1113
1114 EMIT(load_build_class);
1115
1116 // scope for this class
1117 scope_t *cscope = (scope_t*)pns->nodes[3];
1118
1119 // compile the class
1120 close_over_variables_etc(comp, cscope, 0, 0);
1121
1122 // get its name
Damien George968bf342014-04-27 19:12:05 +01001123 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001124
1125 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001126 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1127 mp_parse_node_t parents = pns->nodes[1];
1128 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1129 parents = MP_PARSE_NODE_NULL;
1130 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001131 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001132 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001133
1134 // return its name (the 'C' in class C(...):")
1135 return cscope->simple_name;
1136}
1137
Damien6cdd3af2013-10-05 18:08:26 +01001138// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001139STATIC 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 +00001140 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001141 return false;
1142 }
1143
1144 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001145 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001146 return true;
1147 }
1148
Damiend99b0522013-12-21 18:17:45 +00001149 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001150 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001151 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001152#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001153 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001154 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001155 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001156 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001157#endif
Damien3ef4abb2013-10-12 16:53:13 +01001158#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001159 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001160 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001161#endif
Damien6cdd3af2013-10-05 18:08:26 +01001162 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001163 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001164 }
1165
1166 return true;
1167}
1168
Damien George969a6b32014-12-10 22:07:04 +00001169STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001170 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001171 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001172 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001173
Damien6cdd3af2013-10-05 18:08:26 +01001174 // inherit emit options for this function/class definition
1175 uint emit_options = comp->scope_cur->emit_options;
1176
1177 // compile each decorator
1178 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001179 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001180 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1181 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001182
1183 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001184 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001185 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001186
1187 // check for built-in decorators
1188 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1189 // this was a built-in
1190 num_built_in_decorators += 1;
1191
1192 } else {
1193 // not a built-in, compile normally
1194
1195 // compile the decorator function
1196 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001197 for (int j = 1; j < name_len; j++) {
1198 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1199 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001200 }
1201
1202 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001203 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001204 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001205 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001206 compile_node(comp, pns_decorator->nodes[1]);
1207 }
Damien429d7192013-10-04 19:53:11 +01001208 }
1209 }
1210
1211 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001212 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001213 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001214 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001215 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001216 } else {
Damien George0bb97132015-02-27 14:25:47 +00001217 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1218 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001219 }
1220
1221 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001222 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001223 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001224 }
1225
1226 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001227 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001228}
1229
Damien George969a6b32014-12-10 22:07:04 +00001230STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001231 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001232 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001233 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001234}
1235
Damien George6be0b0a2014-08-15 14:30:52 +01001236STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001237 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001238 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001239 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1240 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001241
1242 compile_node(comp, pns->nodes[0]); // base of the power node
1243
Damiend99b0522013-12-21 18:17:45 +00001244 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1245 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1246 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1247 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001248 for (int i = 0; i < n - 1; i++) {
1249 compile_node(comp, pns1->nodes[i]);
1250 }
Damiend99b0522013-12-21 18:17:45 +00001251 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1252 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001253 }
Damiend99b0522013-12-21 18:17:45 +00001254 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001255 // can't delete function calls
1256 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001257 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001258 compile_node(comp, pns1->nodes[0]);
1259 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001260 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1261 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001262 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001263 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001264 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001265 }
1266 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001267 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001268 }
1269
Damiend99b0522013-12-21 18:17:45 +00001270 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001271 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001272 }
Damiend99b0522013-12-21 18:17:45 +00001273 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1274 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1275 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1276 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001277 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1278
Damiend99b0522013-12-21 18:17:45 +00001279 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1280 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1281 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001282 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001283 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001284 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001285 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001286 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001287 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001288 c_del_stmt(comp, pns->nodes[0]);
1289 for (int i = 0; i < n; i++) {
1290 c_del_stmt(comp, pns1->nodes[i]);
1291 }
Damiend99b0522013-12-21 18:17:45 +00001292 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001293 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001294 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001295 } else {
1296 // sequence with 2 items
1297 goto sequence_with_2_items;
1298 }
1299 } else {
1300 // sequence with 2 items
1301 sequence_with_2_items:
1302 c_del_stmt(comp, pns->nodes[0]);
1303 c_del_stmt(comp, pns->nodes[1]);
1304 }
1305 } else {
1306 // tuple with 1 element
1307 c_del_stmt(comp, pn);
1308 }
1309 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001310 // TODO is there anything else to implement?
1311 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001312 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001313
1314 return;
1315
1316cannot_delete:
1317 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001318}
1319
Damien George969a6b32014-12-10 22:07:04 +00001320STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001321 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1322}
1323
Damien George969a6b32014-12-10 22:07:04 +00001324STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001325 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001326 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001327 }
Damien George090c9232014-10-17 14:08:49 +00001328 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001329 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001330}
1331
Damien George969a6b32014-12-10 22:07:04 +00001332STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001333 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001334 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001335 }
Damien George090c9232014-10-17 14:08:49 +00001336 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001337 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001338}
1339
Damien George969a6b32014-12-10 22:07:04 +00001340STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001341 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001342 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001343 return;
1344 }
Damiend99b0522013-12-21 18:17:45 +00001345 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001346 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001347 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001348 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001349 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001350 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1351 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 +01001352
Damien George6f355fd2014-04-10 14:11:31 +01001353 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001354 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1355 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1356 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001357 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001358 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1359 } else {
1360 compile_node(comp, pns->nodes[0]);
1361 }
1362 EMIT(return_value);
1363}
1364
Damien George969a6b32014-12-10 22:07:04 +00001365STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001366 compile_node(comp, pns->nodes[0]);
1367 EMIT(pop_top);
1368}
1369
Damien George969a6b32014-12-10 22:07:04 +00001370STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001371 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001372 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001373 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001374 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001375 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001376 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001377 compile_node(comp, pns->nodes[0]);
1378 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001379 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001380 } else {
1381 // raise x
1382 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001383 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001384 }
1385}
1386
Damien George635543c2014-04-10 12:56:52 +01001387// q_base holds the base of the name
1388// eg a -> q_base=a
1389// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001390STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001391 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001392 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1393 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001394 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001395 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001396 pn = pns->nodes[0];
1397 is_as = true;
1398 }
Damien George635543c2014-04-10 12:56:52 +01001399 if (MP_PARSE_NODE_IS_NULL(pn)) {
1400 // empty name (eg, from . import x)
1401 *q_base = MP_QSTR_;
1402 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1403 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001404 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001405 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001406 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001407 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001408 }
Damien George635543c2014-04-10 12:56:52 +01001409 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001410 } else {
1411 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001412 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001413 {
Damien429d7192013-10-04 19:53:11 +01001414 // a name of the form a.b.c
1415 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001416 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001417 }
Damiend99b0522013-12-21 18:17:45 +00001418 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001419 int len = n - 1;
1420 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001421 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001422 }
Damien George55baff42014-01-21 21:40:13 +00001423 byte *q_ptr;
1424 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001425 for (int i = 0; i < n; i++) {
1426 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001427 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001428 }
Damien George39dc1452014-10-03 19:52:22 +01001429 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001430 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001431 memcpy(str_dest, str_src, str_src_len);
1432 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001433 }
Damien George635543c2014-04-10 12:56:52 +01001434 qstr q_full = qstr_build_end(q_ptr);
1435 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001436 if (is_as) {
1437 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001438 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001439 }
1440 }
Damien429d7192013-10-04 19:53:11 +01001441 }
Damien429d7192013-10-04 19:53:11 +01001442 }
1443}
1444
Damien George6be0b0a2014-08-15 14:30:52 +01001445STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001446 EMIT_ARG(load_const_small_int, 0); // level 0 import
1447 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1448 qstr q_base;
1449 do_import_name(comp, pn, &q_base);
1450 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001451}
1452
Damien George969a6b32014-12-10 22:07:04 +00001453STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001454 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1455}
1456
Damien George969a6b32014-12-10 22:07:04 +00001457STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001458 mp_parse_node_t pn_import_source = pns->nodes[0];
1459
1460 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1461 uint import_level = 0;
1462 do {
1463 mp_parse_node_t pn_rel;
1464 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)) {
1465 // This covers relative imports with dots only like "from .. import"
1466 pn_rel = pn_import_source;
1467 pn_import_source = MP_PARSE_NODE_NULL;
1468 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1469 // This covers relative imports starting with dot(s) like "from .foo import"
1470 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1471 pn_rel = pns_2b->nodes[0];
1472 pn_import_source = pns_2b->nodes[1];
1473 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1474 } else {
1475 // Not a relative import
1476 break;
1477 }
1478
1479 // get the list of . and/or ...'s
1480 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001481 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001482
1483 // count the total number of .'s
1484 for (int i = 0; i < n; i++) {
1485 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1486 import_level++;
1487 } else {
1488 // should be an MP_TOKEN_ELLIPSIS
1489 import_level += 3;
1490 }
1491 }
1492 } while (0);
1493
Damiend99b0522013-12-21 18:17:45 +00001494 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001495 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001496
1497 // build the "fromlist" tuple
1498#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001499 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001500#else
Damien George708c0732014-04-27 19:23:46 +01001501 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001502 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001503#endif
1504
1505 // do the import
Damien George635543c2014-04-10 12:56:52 +01001506 qstr dummy_q;
1507 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001508 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001509
Damien429d7192013-10-04 19:53:11 +01001510 } else {
Damien George635543c2014-04-10 12:56:52 +01001511 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001512
1513 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001514 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001515 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001516#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001517 {
1518 vstr_t *vstr = vstr_new();
1519 vstr_printf(vstr, "(");
1520 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001521 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1522 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1523 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001524 if (i > 0) {
1525 vstr_printf(vstr, ", ");
1526 }
1527 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001528 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001529 const byte *str = qstr_data(id2, &len);
1530 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001531 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001532 }
Damien02f89412013-12-12 15:13:36 +00001533 if (n == 1) {
1534 vstr_printf(vstr, ",");
1535 }
1536 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001537 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001538 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001539 }
Damiendb4c3612013-12-10 17:27:24 +00001540#else
1541 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001542 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1543 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1544 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001545 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001546 }
Damien Georgeb9791222014-01-23 00:34:21 +00001547 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001548#endif
1549
1550 // do the import
Damien George635543c2014-04-10 12:56:52 +01001551 qstr dummy_q;
1552 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001553 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001554 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1555 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1556 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001557 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001558 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001559 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001560 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001562 }
1563 }
1564 EMIT(pop_top);
1565 }
1566}
1567
Damien George584ba672014-12-21 17:26:45 +00001568STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1569 bool added;
1570 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1571 if (!added) {
1572 compile_syntax_error(comp, pn, "identifier already used");
1573 return;
1574 }
1575 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1576
1577 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1578 id_info = scope_find_global(comp->scope_cur, qst);
1579 if (id_info != NULL) {
1580 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1581 }
1582}
1583
Damien George969a6b32014-12-10 22:07:04 +00001584STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001585 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001586 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001587 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001588 for (int i = 0; i < n; i++) {
1589 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001590 }
1591 }
1592}
1593
Damien George584ba672014-12-21 17:26:45 +00001594STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1595 bool added;
1596 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1597 if (!added) {
1598 compile_syntax_error(comp, pn, "identifier already used");
1599 return;
1600 }
1601 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1602 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)) {
1603 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1604 return;
1605 }
1606 id_info->kind = ID_INFO_KIND_FREE;
1607 scope_close_over_in_parents(comp->scope_cur, qst);
1608}
1609
Damien George969a6b32014-12-10 22:07:04 +00001610STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001611 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001612 if (comp->scope_cur->kind == SCOPE_MODULE) {
1613 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1614 return;
1615 }
1616 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001617 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001618 for (int i = 0; i < n; i++) {
1619 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001620 }
1621 }
1622}
1623
Damien George969a6b32014-12-10 22:07:04 +00001624STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001625 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001626 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001627 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 +00001628 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001629 // assertion message
1630 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001631 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001632 }
Damien Georgeb9791222014-01-23 00:34:21 +00001633 EMIT_ARG(raise_varargs, 1);
1634 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001635}
1636
Damien George969a6b32014-12-10 22:07:04 +00001637STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001638 // TODO proper and/or short circuiting
1639
Damien George6f355fd2014-04-10 14:11:31 +01001640 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001641
Damien George391db862014-10-17 17:57:33 +00001642 // optimisation: don't emit anything when "if False" (not in CPython)
1643 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1644 uint l_fail = comp_next_label(comp);
1645 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001646
Damien George391db862014-10-17 17:57:33 +00001647 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001648
Damien George391db862014-10-17 17:57:33 +00001649 // optimisation: skip everything else when "if True" (not in CPython)
1650 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1651 goto done;
1652 }
1653
1654 if (
1655 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1656 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1657 // optimisation: don't jump if last instruction was return
1658 && !EMIT(last_emit_was_return_value)
1659 ) {
1660 // jump over elif/else blocks
1661 EMIT_ARG(jump, l_end);
1662 }
1663
1664 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001665 }
1666
Damien George235f9b32014-10-17 17:30:16 +00001667 // compile elif blocks (if any)
1668 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001669 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 +00001670 for (int i = 0; i < n_elif; i++) {
1671 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1672 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001673
Damien George391db862014-10-17 17:57:33 +00001674 // optimisation: don't emit anything when "if False" (not in CPython)
1675 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1676 uint l_fail = comp_next_label(comp);
1677 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001678
Damien George391db862014-10-17 17:57:33 +00001679 compile_node(comp, pns_elif->nodes[1]); // elif block
1680
1681 // optimisation: skip everything else when "elif True" (not in CPython)
1682 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1683 goto done;
1684 }
1685
1686 // optimisation: don't jump if last instruction was return
1687 if (!EMIT(last_emit_was_return_value)) {
1688 EMIT_ARG(jump, l_end);
1689 }
1690 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001691 }
1692 }
1693
1694 // compile else block
1695 compile_node(comp, pns->nodes[3]); // can be null
1696
Damien George391db862014-10-17 17:57:33 +00001697done:
Damien Georgeb9791222014-01-23 00:34:21 +00001698 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001699}
1700
Damien Georgecbddb272014-02-01 20:08:18 +00001701#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001702 uint16_t old_break_label = comp->break_label; \
1703 uint16_t old_continue_label = comp->continue_label; \
1704 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001705 uint break_label = comp_next_label(comp); \
1706 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001707 comp->break_label = break_label; \
1708 comp->continue_label = continue_label; \
1709 comp->break_continue_except_level = comp->cur_except_level;
1710
1711#define END_BREAK_CONTINUE_BLOCK \
1712 comp->break_label = old_break_label; \
1713 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001714 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001715
Damien George969a6b32014-12-10 22:07:04 +00001716STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001717 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001718
Damience89a212013-10-15 22:25:17 +01001719 // compared to CPython, we have an optimised version of while loops
1720#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001721 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001722 EMIT_ARG(setup_loop, break_label);
1723 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001724 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1725 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001726 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001727 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001728 }
Damien Georgeb9791222014-01-23 00:34:21 +00001729 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001730 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1731 // this is a small hack to agree with CPython
1732 if (!node_is_const_true(pns->nodes[0])) {
1733 EMIT(pop_block);
1734 }
Damience89a212013-10-15 22:25:17 +01001735#else
Damien George391db862014-10-17 17:57:33 +00001736 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1737 uint top_label = comp_next_label(comp);
1738 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1739 EMIT_ARG(jump, continue_label);
1740 }
1741 EMIT_ARG(label_assign, top_label);
1742 compile_node(comp, pns->nodes[1]); // body
1743 EMIT_ARG(label_assign, continue_label);
1744 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1745 }
Damience89a212013-10-15 22:25:17 +01001746#endif
1747
1748 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001749 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001750
1751 compile_node(comp, pns->nodes[2]); // else
1752
Damien Georgeb9791222014-01-23 00:34:21 +00001753 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001754}
1755
Damien George2ac4af62014-08-15 16:45:41 +01001756#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001757// This function compiles an optimised for-loop of the form:
1758// for <var> in range(<start>, <end>, <step>):
1759// <body>
1760// else:
1761// <else>
1762// <var> must be an identifier and <step> must be a small-int.
1763//
1764// Semantics of for-loop require:
1765// - final failing value should not be stored in the loop variable
1766// - if the loop never runs, the loop variable should never be assigned
1767// - assignments to <var>, <end> or <step> in the body do not alter the loop
1768// (<step> is a constant for us, so no need to worry about it changing)
1769//
1770// If <end> is a small-int, then the stack during the for-loop contains just
1771// the current value of <var>. Otherwise, the stack contains <end> then the
1772// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001773STATIC 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 +00001774 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001775
Damien George6f355fd2014-04-10 14:11:31 +01001776 uint top_label = comp_next_label(comp);
1777 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001778
Damien Georgee181c0d2014-12-12 17:19:56 +00001779 // put the end value on the stack if it's not a small-int constant
1780 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1781 if (end_on_stack) {
1782 compile_node(comp, pn_end);
1783 }
1784
1785 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001786 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001787
Damien Georgeb9791222014-01-23 00:34:21 +00001788 EMIT_ARG(jump, entry_label);
1789 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001790
Damien Georgec33ce602014-12-11 17:35:23 +00001791 // duplicate next value and store it to var
1792 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001793 c_assign(comp, pn_var, ASSIGN_STORE);
1794
Damienf3822fc2013-11-09 20:12:03 +00001795 // compile body
1796 compile_node(comp, pn_body);
1797
Damien Georgeb9791222014-01-23 00:34:21 +00001798 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001799
Damien Georgee181c0d2014-12-12 17:19:56 +00001800 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001801 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001802 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001803
Damien Georgeb9791222014-01-23 00:34:21 +00001804 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001805
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001806 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001807 if (end_on_stack) {
1808 EMIT(dup_top_two);
1809 EMIT(rot_two);
1810 } else {
1811 EMIT(dup_top);
1812 compile_node(comp, pn_end);
1813 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001814 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1815 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001816 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001817 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001818 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001819 }
Damien George63f38322015-02-28 15:04:06 +00001820 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001821
1822 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001823 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001824
1825 compile_node(comp, pn_else);
1826
Damien Georgeb9791222014-01-23 00:34:21 +00001827 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001828
1829 // discard final value of var that failed the loop condition
1830 EMIT(pop_top);
1831
1832 // discard <end> value if it's on the stack
1833 if (end_on_stack) {
1834 EMIT(pop_top);
1835 }
Damienf72fd0e2013-11-06 20:20:49 +00001836}
Damien George2ac4af62014-08-15 16:45:41 +01001837#endif
Damienf72fd0e2013-11-06 20:20:49 +00001838
Damien George969a6b32014-12-10 22:07:04 +00001839STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001840#if !MICROPY_EMIT_CPYTHON
1841 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1842 // this is actually slower, but uses no heap memory
1843 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001844 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 +00001845 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001846 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1847 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1848 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1849 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001850 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1851 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001852 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001853 mp_parse_node_t pn_range_start;
1854 mp_parse_node_t pn_range_end;
1855 mp_parse_node_t pn_range_step;
1856 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001857 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001858 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001859 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001860 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001861 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001862 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001863 } else if (n_args == 2) {
1864 pn_range_start = args[0];
1865 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001866 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001867 } else {
1868 pn_range_start = args[0];
1869 pn_range_end = args[1];
1870 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001871 // We need to know sign of step. This is possible only if it's constant
1872 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1873 optimize = false;
1874 }
Damienf72fd0e2013-11-06 20:20:49 +00001875 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001876 }
1877 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001878 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1879 return;
1880 }
1881 }
1882 }
1883#endif
1884
Damien Georgecbddb272014-02-01 20:08:18 +00001885 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001886 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001887
Damien George6f355fd2014-04-10 14:11:31 +01001888 uint pop_label = comp_next_label(comp);
1889 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001890
Damience89a212013-10-15 22:25:17 +01001891 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1892#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001893 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001894#endif
1895
Damien429d7192013-10-04 19:53:11 +01001896 compile_node(comp, pns->nodes[1]); // iterator
1897 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001898 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001899 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001900 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1901 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001902 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001903 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001904 }
Damien Georgeb9791222014-01-23 00:34:21 +00001905 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001906 EMIT(for_iter_end);
1907
1908 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001909 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001910
Damience89a212013-10-15 22:25:17 +01001911#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001912 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001913#endif
Damien429d7192013-10-04 19:53:11 +01001914
1915 compile_node(comp, pns->nodes[3]); // else (not tested)
1916
Damien Georgeb9791222014-01-23 00:34:21 +00001917 EMIT_ARG(label_assign, break_label);
1918 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001919}
1920
Damien George6be0b0a2014-08-15 14:30:52 +01001921STATIC 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 +01001922 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001923 uint l1 = comp_next_label(comp);
1924 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001925
Damien Georgeb9791222014-01-23 00:34:21 +00001926 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001927 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001928
Damien429d7192013-10-04 19:53:11 +01001929 compile_node(comp, pn_body); // body
1930 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001931 EMIT_ARG(jump, success_label); // jump over exception handler
1932
1933 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001934 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001935
Damien George6f355fd2014-04-10 14:11:31 +01001936 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001937
1938 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001939 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1940 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001941
1942 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001943 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001944
Damiend99b0522013-12-21 18:17:45 +00001945 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001946 // this is a catch all exception handler
1947 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001948 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001949 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001950 return;
1951 }
1952 } else {
1953 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001954 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1955 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1956 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1957 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001958 // handler binds the exception to a local
1959 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001960 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001961 }
1962 }
1963 EMIT(dup_top);
1964 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001965 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001966 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001967 }
1968
1969 EMIT(pop_top);
1970
1971 if (qstr_exception_local == 0) {
1972 EMIT(pop_top);
1973 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001974 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001975 }
1976
1977 EMIT(pop_top);
1978
Damien George6f355fd2014-04-10 14:11:31 +01001979 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001980 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001981 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001983 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001984 }
1985 compile_node(comp, pns_except->nodes[1]);
1986 if (qstr_exception_local != 0) {
1987 EMIT(pop_block);
1988 }
1989 EMIT(pop_except);
1990 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001991 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1992 EMIT_ARG(label_assign, l3);
1993 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1994 EMIT_ARG(store_id, qstr_exception_local);
1995 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001996
Damien George8dcc0c72014-03-27 10:55:21 +00001997 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001998 EMIT(end_finally);
1999 }
Damien Georgeb9791222014-01-23 00:34:21 +00002000 EMIT_ARG(jump, l2);
2001 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002002 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002003 }
2004
Damien George8dcc0c72014-03-27 10:55:21 +00002005 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002006 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002007 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002008
Damien Georgeb9791222014-01-23 00:34:21 +00002009 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002010 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002011 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002012}
2013
Damien George6be0b0a2014-08-15 14:30:52 +01002014STATIC 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 +01002015 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002016
Damien Georgeb9791222014-01-23 00:34:21 +00002017 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002018 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002019
Damien429d7192013-10-04 19:53:11 +01002020 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002021 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002022 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002023 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002024 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002025 } else {
2026 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2027 }
2028 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002029 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2030 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002031 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002032
Damien George8dcc0c72014-03-27 10:55:21 +00002033 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002034 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002035}
2036
Damien George969a6b32014-12-10 22:07:04 +00002037STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00002038 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2039 {
Damiend99b0522013-12-21 18:17:45 +00002040 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2041 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002042 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002043 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2044 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002045 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002046 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002047 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002048 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002049 // no finally
2050 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2051 } else {
2052 // have finally
Damiend99b0522013-12-21 18:17:45 +00002053 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 +01002054 }
2055 } else {
2056 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002057 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002058 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002059 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002060 }
Damien429d7192013-10-04 19:53:11 +01002061 }
2062}
2063
Damien George6be0b0a2014-08-15 14:30:52 +01002064STATIC 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 +01002065 if (n == 0) {
2066 // no more pre-bits, compile the body of the with
2067 compile_node(comp, body);
2068 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002069 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002070 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002071 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002072 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002073 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002074 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002075 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2076 } else {
2077 // this pre-bit is just an expression
2078 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002079 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002080 EMIT(pop_top);
2081 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002082 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002083 // compile additional pre-bits and the body
2084 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2085 // finish this with block
2086 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002087 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2088 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002089 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002090 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002091 EMIT(end_finally);
2092 }
2093}
2094
Damien George969a6b32014-12-10 22:07:04 +00002095STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002096 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002097 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002098 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002099 assert(n > 0);
2100
2101 // compile in a nested fashion
2102 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2103}
2104
Damien George969a6b32014-12-10 22:07:04 +00002105STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002106 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002107 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2108 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002109 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002110 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002111 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002112 EMIT(pop_top);
2113
Damien429d7192013-10-04 19:53:11 +01002114 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002115 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002116 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002117 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002118 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2119 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002120 // do nothing with a lonely constant
2121 } else {
2122 compile_node(comp, pns->nodes[0]); // just an expression
2123 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2124 }
Damien429d7192013-10-04 19:53:11 +01002125 }
2126 } else {
Damiend99b0522013-12-21 18:17:45 +00002127 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2128 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002129 if (kind == PN_expr_stmt_augassign) {
2130 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2131 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002132 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002133 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002134 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002135 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2136 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2137 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2138 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2139 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2140 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2141 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2142 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2143 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2144 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2145 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002146 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002147 }
Damien George7e5fb242014-02-01 22:18:47 +00002148 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002149 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2150 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002151 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2152 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002153 // following CPython, we store left-most first
2154 if (rhs > 0) {
2155 EMIT(dup_top);
2156 }
2157 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2158 for (int i = 0; i < rhs; i++) {
2159 if (i + 1 < rhs) {
2160 EMIT(dup_top);
2161 }
Damiend99b0522013-12-21 18:17:45 +00002162 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002163 }
Damien George0bb97132015-02-27 14:25:47 +00002164 } else {
2165 assert(kind == PN_expr_stmt_assign); // should be
Damien Georgeffae48d2014-05-08 15:58:39 +00002166 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002167 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2168 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2169 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002170 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002171 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2172 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002173 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2174 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2175 // can't optimise when it's a star expression on the lhs
2176 goto no_optimisation;
2177 }
Damien429d7192013-10-04 19:53:11 +01002178 compile_node(comp, pns10->nodes[0]); // rhs
2179 compile_node(comp, pns10->nodes[1]); // rhs
2180 EMIT(rot_two);
2181 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2182 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002183 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2184 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2185 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2186 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002187 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002188 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2189 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002190 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2191 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2192 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2193 // can't optimise when it's a star expression on the lhs
2194 goto no_optimisation;
2195 }
Damien429d7192013-10-04 19:53:11 +01002196 compile_node(comp, pns10->nodes[0]); // rhs
2197 compile_node(comp, pns10->nodes[1]); // rhs
2198 compile_node(comp, pns10->nodes[2]); // rhs
2199 EMIT(rot_three);
2200 EMIT(rot_two);
2201 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2202 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2203 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2204 } else {
Damien George495d7812014-04-08 17:51:47 +01002205 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002206 compile_node(comp, pns1->nodes[0]); // rhs
2207 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2208 }
Damien429d7192013-10-04 19:53:11 +01002209 }
2210 }
2211}
2212
Damien George6be0b0a2014-08-15 14:30:52 +01002213STATIC 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 +00002214 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002215 compile_node(comp, pns->nodes[0]);
2216 for (int i = 1; i < num_nodes; i += 1) {
2217 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002218 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002219 }
2220}
2221
Damien George969a6b32014-12-10 22:07:04 +00002222STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002223 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2224 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002225
Damien George6f355fd2014-04-10 14:11:31 +01002226 uint l_fail = comp_next_label(comp);
2227 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002228 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2229 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002230 EMIT_ARG(jump, l_end);
2231 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002232 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002233 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002234 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002235}
2236
Damien George969a6b32014-12-10 22:07:04 +00002237STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002238 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002239 //mp_parse_node_t pn_params = pns->nodes[0];
2240 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002241
Damien George36db6bc2014-05-07 17:24:22 +01002242 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002243 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002244 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 +01002245 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002246 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002247 }
2248
2249 // get the scope for this lambda
2250 scope_t *this_scope = (scope_t*)pns->nodes[2];
2251
2252 // make the lambda
2253 close_over_variables_etc(comp, this_scope, 0, 0);
2254}
2255
Damien George969a6b32014-12-10 22:07:04 +00002256STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002257 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002258 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002259 for (int i = 0; i < n; i += 1) {
2260 compile_node(comp, pns->nodes[i]);
2261 if (i + 1 < n) {
Damien George63f38322015-02-28 15:04:06 +00002262 EMIT_ARG(jump_if_or_pop, true, l_end);
Damien429d7192013-10-04 19:53:11 +01002263 }
2264 }
Damien Georgeb9791222014-01-23 00:34:21 +00002265 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002266}
2267
Damien George969a6b32014-12-10 22:07:04 +00002268STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002269 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002270 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002271 for (int i = 0; i < n; i += 1) {
2272 compile_node(comp, pns->nodes[i]);
2273 if (i + 1 < n) {
Damien George63f38322015-02-28 15:04:06 +00002274 EMIT_ARG(jump_if_or_pop, false, l_end);
Damien429d7192013-10-04 19:53:11 +01002275 }
2276 }
Damien Georgeb9791222014-01-23 00:34:21 +00002277 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002278}
2279
Damien George969a6b32014-12-10 22:07:04 +00002280STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002281 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002282 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002283}
2284
Damien George969a6b32014-12-10 22:07:04 +00002285STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002286 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002287 compile_node(comp, pns->nodes[0]);
2288 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002289 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002290 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002291 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002292 }
2293 for (int i = 1; i + 1 < num_nodes; i += 2) {
2294 compile_node(comp, pns->nodes[i + 1]);
2295 if (i + 2 < num_nodes) {
2296 EMIT(dup_top);
2297 EMIT(rot_three);
2298 }
Damien George7e5fb242014-02-01 22:18:47 +00002299 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002300 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002301 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002302 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2303 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2304 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2305 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2306 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2307 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002308 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002309 }
2310 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002311 } else {
2312 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002313 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2314 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002315 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002316 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002317 } else {
2318 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002319 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002320 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002321 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002322 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002323 }
Damien429d7192013-10-04 19:53:11 +01002324 }
Damien429d7192013-10-04 19:53:11 +01002325 }
2326 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002327 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002328 }
2329 }
2330 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002331 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002332 EMIT_ARG(jump, l_end);
2333 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002334 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002335 EMIT(rot_two);
2336 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002337 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002338 }
2339}
2340
Damien George969a6b32014-12-10 22:07:04 +00002341STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002342 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002343}
2344
Damien George969a6b32014-12-10 22:07:04 +00002345STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002346 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002347}
2348
Damien George969a6b32014-12-10 22:07:04 +00002349STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002350 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002351}
2352
Damien George969a6b32014-12-10 22:07:04 +00002353STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002354 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002355}
2356
Damien George969a6b32014-12-10 22:07:04 +00002357STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002358 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002359 compile_node(comp, pns->nodes[0]);
2360 for (int i = 1; i + 1 < num_nodes; i += 2) {
2361 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002362 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002363 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002364 } else {
Damien George0bb97132015-02-27 14:25:47 +00002365 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2366 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002367 }
2368 }
2369}
2370
Damien George969a6b32014-12-10 22:07:04 +00002371STATIC void compile_arith_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_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002377 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
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_MINUS)); // should be
2380 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002381 }
2382 }
2383}
2384
Damien George969a6b32014-12-10 22:07:04 +00002385STATIC void compile_term(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_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002391 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002392 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002393 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002394 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002395 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002396 } else {
Damien George0bb97132015-02-27 14:25:47 +00002397 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2398 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002399 }
2400 }
2401}
2402
Damien George969a6b32014-12-10 22:07:04 +00002403STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002404 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002405 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002406 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002407 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002408 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002409 } else {
Damien George0bb97132015-02-27 14:25:47 +00002410 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2411 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002412 }
2413}
2414
Damien George969a6b32014-12-10 22:07:04 +00002415STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002416 // this is to handle special super() call
2417 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2418
2419 compile_generic_all_nodes(comp, pns);
2420}
2421
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002422STATIC 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 +01002423 // function to call is on top of stack
2424
Damien George35e2a4e2014-02-05 00:51:47 +00002425#if !MICROPY_EMIT_CPYTHON
2426 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002427 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 +00002428 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002429 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002430 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002431 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002432 // first argument found; load it and call super
Damien George0abb5602015-01-16 12:24:49 +00002433 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002434 EMIT_ARG(call_function, 2, 0, 0);
2435 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002436 }
2437 }
Damien George01039b52014-12-21 17:44:27 +00002438 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002439 return;
2440 }
2441#endif
2442
Damien George2c0842b2014-04-27 16:46:51 +01002443 // get the list of arguments
2444 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002445 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002446
Damien George2c0842b2014-04-27 16:46:51 +01002447 // compile the arguments
2448 // Rather than calling compile_node on the list, we go through the list of args
2449 // explicitly here so that we can count the number of arguments and give sensible
2450 // error messages.
2451 int n_positional = n_positional_extra;
2452 uint n_keyword = 0;
2453 uint star_flags = 0;
2454 for (int i = 0; i < n_args; i++) {
2455 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2456 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2457 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2458 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2459 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2460 return;
2461 }
2462 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2463 compile_node(comp, pns_arg->nodes[0]);
2464 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2465 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2466 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2467 return;
2468 }
2469 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2470 compile_node(comp, pns_arg->nodes[0]);
2471 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2472 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2473 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2474 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2475 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2476 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2477 return;
2478 }
Damien George968bf342014-04-27 19:12:05 +01002479 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002480 compile_node(comp, pns2->nodes[0]);
2481 n_keyword += 1;
2482 } else {
2483 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2484 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2485 n_positional++;
2486 }
2487 } else {
2488 goto normal_argument;
2489 }
2490 } else {
2491 normal_argument:
2492 if (n_keyword > 0) {
2493 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2494 return;
2495 }
2496 compile_node(comp, args[i]);
2497 n_positional++;
2498 }
Damien429d7192013-10-04 19:53:11 +01002499 }
2500
Damien George2c0842b2014-04-27 16:46:51 +01002501 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002502 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002503 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002504 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002505 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002506 }
Damien429d7192013-10-04 19:53:11 +01002507}
2508
Damien George969a6b32014-12-10 22:07:04 +00002509STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002510 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002511 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002512 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 +01002513 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002514 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2515 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002516 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002517 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002518 i += 1;
2519 } else {
2520 compile_node(comp, pns->nodes[i]);
2521 }
Damien George35e2a4e2014-02-05 00:51:47 +00002522 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002523 }
2524}
2525
Damien George969a6b32014-12-10 22:07:04 +00002526STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002527 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002528 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002529}
2530
Damien George969a6b32014-12-10 22:07:04 +00002531STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002532 // a list of strings
Damien63321742013-12-10 17:41:49 +00002533
2534 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002535 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002536 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002537 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002538 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002539 int pn_kind;
2540 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2541 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2542 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2543 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2544 } else {
2545 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2546 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002547 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2548 pn_kind = MP_PARSE_NODE_STRING;
2549 } else {
2550 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2551 pn_kind = MP_PARSE_NODE_BYTES;
2552 }
Damien George40f3c022014-07-03 13:25:24 +01002553 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002554 }
Damien63321742013-12-10 17:41:49 +00002555 if (i == 0) {
2556 string_kind = pn_kind;
2557 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002558 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002559 return;
2560 }
Damien429d7192013-10-04 19:53:11 +01002561 }
Damien63321742013-12-10 17:41:49 +00002562
Damien George65ef6b72015-01-14 21:17:27 +00002563 // if we are not in the last pass, just load a dummy object
2564 if (comp->pass != MP_PASS_EMIT) {
2565 EMIT_ARG(load_const_obj, mp_const_none);
2566 return;
2567 }
2568
Damien63321742013-12-10 17:41:49 +00002569 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002570 vstr_t vstr;
2571 vstr_init_len(&vstr, n_bytes);
2572 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002573 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002574 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002575 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002576 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2577 memcpy(s_dest, s, s_len);
2578 s_dest += s_len;
2579 } else {
2580 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002581 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2582 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002583 }
Damien63321742013-12-10 17:41:49 +00002584 }
Damien George65ef6b72015-01-14 21:17:27 +00002585
2586 // load the object
Damien George05005f62015-01-21 22:48:37 +00002587 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 +01002588}
2589
2590// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002591STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002592 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2593 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2594 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002595
Damien George36db6bc2014-05-07 17:24:22 +01002596 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002597 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002598 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 +01002599 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002600 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002601 }
2602
2603 // get the scope for this comprehension
2604 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2605
2606 // compile the comprehension
2607 close_over_variables_etc(comp, this_scope, 0, 0);
2608
2609 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2610 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002611 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002612}
2613
Damien George969a6b32014-12-10 22:07:04 +00002614STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002615 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002616 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002617 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2618 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2619 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2620 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2621 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2622 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2623 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002624 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002625 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002626 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002627 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002628 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002629 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002630 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002631 // generator expression
2632 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2633 } else {
2634 // tuple with 2 items
2635 goto tuple_with_2_items;
2636 }
2637 } else {
2638 // tuple with 2 items
2639 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002640 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002641 }
2642 } else {
2643 // parenthesis around a single item, is just that item
2644 compile_node(comp, pns->nodes[0]);
2645 }
2646}
2647
Damien George969a6b32014-12-10 22:07:04 +00002648STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002649 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002650 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002651 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002652 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2653 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2654 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2655 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2656 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002657 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002658 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002659 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002660 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002661 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002662 // list of many items
2663 compile_node(comp, pns2->nodes[0]);
2664 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002665 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002666 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002667 // list comprehension
2668 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2669 } else {
2670 // list with 2 items
2671 goto list_with_2_items;
2672 }
2673 } else {
2674 // list with 2 items
2675 list_with_2_items:
2676 compile_node(comp, pns2->nodes[0]);
2677 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002678 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002679 }
2680 } else {
2681 // list with 1 item
2682 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002683 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002684 }
2685}
2686
Damien George969a6b32014-12-10 22:07:04 +00002687STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002688 mp_parse_node_t pn = pns->nodes[0];
2689 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002690 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002691 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002692 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2693 pns = (mp_parse_node_struct_t*)pn;
2694 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002695 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002696 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002697 compile_node(comp, pn);
2698 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002699 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2700 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2701 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2702 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002703 // dict/set with multiple elements
2704
2705 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002706 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002707 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002708
2709 // first element sets whether it's a dict or set
2710 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002711 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002712 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002713 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002714 compile_node(comp, pns->nodes[0]);
2715 EMIT(store_map);
2716 is_dict = true;
2717 } else {
2718 // a set
2719 compile_node(comp, pns->nodes[0]); // 1st value of set
2720 is_dict = false;
2721 }
2722
2723 // process rest of elements
2724 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002725 mp_parse_node_t pn_i = nodes[i];
2726 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2727 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002728 if (is_dict) {
2729 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002730 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002731 return;
2732 }
2733 EMIT(store_map);
2734 } else {
2735 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002736 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002737 return;
2738 }
2739 }
2740 }
2741
Damien Georgee37dcaa2014-12-27 17:07:16 +00002742 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002743 // if it's a set, build it
2744 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002745 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002746 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002747 #endif
Damien George0bb97132015-02-27 14:25:47 +00002748 } else {
2749 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002750 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002751 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002752 // a dictionary comprehension
2753 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2754 } else {
2755 // a set comprehension
2756 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2757 }
Damien429d7192013-10-04 19:53:11 +01002758 }
2759 } else {
2760 // set with one element
2761 goto set_with_one_element;
2762 }
2763 } else {
2764 // set with one element
2765 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002766 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002767 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002768 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002769 #else
2770 assert(0);
2771 #endif
Damien429d7192013-10-04 19:53:11 +01002772 }
2773}
2774
Damien George969a6b32014-12-10 22:07:04 +00002775STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002776 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002777}
2778
Damien George969a6b32014-12-10 22:07:04 +00002779STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002780 // object who's index we want is on top of stack
2781 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002782 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002783}
2784
Damien George969a6b32014-12-10 22:07:04 +00002785STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002786 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002787 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002788}
2789
Damien George83204f32014-12-27 17:20:41 +00002790#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002791STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002792 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2793 mp_parse_node_t pn = pns->nodes[0];
2794 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002795 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002796 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2797 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002798 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2799 pns = (mp_parse_node_struct_t*)pn;
2800 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002801 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002802 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002803 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002804 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002805 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002806 } else {
2807 // [?::x]
2808 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002809 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002810 }
Damiend99b0522013-12-21 18:17:45 +00002811 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002812 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002813 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2814 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2815 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2816 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002817 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002818 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002819 } else {
2820 // [?:x:x]
2821 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002822 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002823 }
2824 } else {
2825 // [?:x]
2826 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002827 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002828 }
2829 } else {
2830 // [?:x]
2831 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002832 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002833 }
2834}
2835
Damien George969a6b32014-12-10 22:07:04 +00002836STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002837 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002838 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2839 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002840}
2841
Damien George969a6b32014-12-10 22:07:04 +00002842STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002843 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002844 compile_subscript_3_helper(comp, pns);
2845}
Damien George83204f32014-12-27 17:20:41 +00002846#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002847
Damien George969a6b32014-12-10 22:07:04 +00002848STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002849 // if this is called then we are compiling a dict key:value pair
2850 compile_node(comp, pns->nodes[1]); // value
2851 compile_node(comp, pns->nodes[0]); // key
2852}
2853
Damien George969a6b32014-12-10 22:07:04 +00002854STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002855 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002856 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002857 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002858}
2859
Damien George969a6b32014-12-10 22:07:04 +00002860STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002861 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002862 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002863 return;
2864 }
Damiend99b0522013-12-21 18:17:45 +00002865 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002866 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002867 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002868 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2869 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002870 compile_node(comp, pns->nodes[0]);
2871 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002872 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002873 EMIT(yield_from);
2874 } else {
2875 compile_node(comp, pns->nodes[0]);
2876 EMIT(yield_value);
2877 }
2878}
2879
Damien George65ef6b72015-01-14 21:17:27 +00002880STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2881 // only create and load the actual str object on the last pass
2882 if (comp->pass != MP_PASS_EMIT) {
2883 EMIT_ARG(load_const_obj, mp_const_none);
2884 } else {
2885 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2886 }
2887}
2888
2889STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2890 // only create and load the actual bytes object on the last pass
2891 if (comp->pass != MP_PASS_EMIT) {
2892 EMIT_ARG(load_const_obj, mp_const_none);
2893 } else {
2894 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2895 }
2896}
2897
Damien George7d414a12015-02-08 01:57:40 +00002898STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2899 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2900}
2901
Damiend99b0522013-12-21 18:17:45 +00002902typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002903STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002904#define nc NULL
2905#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002906#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002907#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002908#undef nc
2909#undef c
2910#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002911 NULL,
2912 compile_string,
2913 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002914 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002915};
2916
Damien George6be0b0a2014-08-15 14:30:52 +01002917STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002918 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002919 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002920 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002921 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002922 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002923 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002924 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002925 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002926 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002927 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2928 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damien Georged2d64f02015-01-14 21:32:42 +00002929 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002930 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002931 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002932 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002933 // do nothing
2934 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002935 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002936 }
2937 break;
Damien429d7192013-10-04 19:53:11 +01002938 }
2939 } else {
Damiend99b0522013-12-21 18:17:45 +00002940 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002941 EMIT_ARG(set_line_number, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002942 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2943 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002944#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002945 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2946 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002947#endif
Damien George65ef6b72015-01-14 21:17:27 +00002948 compile_syntax_error(comp, pn, "internal compiler error");
2949 } else {
2950 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002951 }
2952 }
2953}
2954
Damien George2ac4af62014-08-15 16:45:41 +01002955STATIC 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 +01002956 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002957 qstr param_name = MP_QSTR_NULL;
2958 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002959 if (MP_PARSE_NODE_IS_ID(pn)) {
2960 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002961 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002962 // comes after a star, so counts as a keyword-only parameter
2963 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002964 } else {
Damien George2827d622014-04-27 15:50:52 +01002965 // comes before a star, so counts as a positional parameter
2966 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002967 }
Damienb14de212013-10-06 00:28:28 +01002968 } else {
Damiend99b0522013-12-21 18:17:45 +00002969 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2970 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2971 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2972 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002973 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002974 // comes after a star, so counts as a keyword-only parameter
2975 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002976 } else {
Damien George2827d622014-04-27 15:50:52 +01002977 // comes before a star, so counts as a positional parameter
2978 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002979 }
Damiend99b0522013-12-21 18:17:45 +00002980 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002981 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002982 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002983 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002984 // bare star
2985 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002986 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002987 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002988 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002989 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002990 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002991 } else {
2992 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002993 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002994 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002995 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2996 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002997 }
Damien George0bb97132015-02-27 14:25:47 +00002998 } else {
2999 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00003000 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003001 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003002 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003003 }
Damien429d7192013-10-04 19:53:11 +01003004 }
3005
Damien George2827d622014-04-27 15:50:52 +01003006 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003007 bool added;
3008 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3009 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003010 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003011 return;
3012 }
Damien429d7192013-10-04 19:53:11 +01003013 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003014 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003015 }
3016}
3017
Damien George2827d622014-04-27 15:50:52 +01003018STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003019 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003020}
3021
Damien George2827d622014-04-27 15:50:52 +01003022STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003023 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3024}
3025
3026STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3027 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3028 // no annotation
3029 return;
3030 }
3031
3032 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3033 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3034 // named parameter with possible annotation
3035 // fallthrough
3036 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3037 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3038 // named star with possible annotation
3039 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3040 // fallthrough
3041 } else {
3042 // no annotation
3043 return;
3044 }
3045 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3046 // double star with possible annotation
3047 // fallthrough
3048 } else {
3049 // no annotation
3050 return;
3051 }
3052
3053 mp_parse_node_t pn_annotation = pns->nodes[1];
3054
3055 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3056 #if MICROPY_EMIT_NATIVE
3057 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3058 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3059 assert(id_info != NULL);
3060
3061 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3062 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3063 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3064 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3065 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003066 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003067 }
3068 }
3069 #endif // MICROPY_EMIT_NATIVE
3070 }
Damien429d7192013-10-04 19:53:11 +01003071}
3072
Damien George6be0b0a2014-08-15 14:30:52 +01003073STATIC 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 +01003074 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003075 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003076 // no more nested if/for; compile inner expression
3077 compile_node(comp, pn_inner_expr);
3078 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003079 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003080 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003081 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003082 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003083 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003084 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003085 #endif
Damien429d7192013-10-04 19:53:11 +01003086 } else {
3087 EMIT(yield_value);
3088 EMIT(pop_top);
3089 }
Damiend99b0522013-12-21 18:17:45 +00003090 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003091 // if condition
Damiend99b0522013-12-21 18:17:45 +00003092 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003093 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3094 pn_iter = pns_comp_if->nodes[1];
3095 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00003096 } else {
3097 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01003098 // for loop
Damiend99b0522013-12-21 18:17:45 +00003099 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003100 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003101 uint l_end2 = comp_next_label(comp);
3102 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003103 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003104 EMIT_ARG(label_assign, l_top2);
3105 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003106 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3107 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 +00003108 EMIT_ARG(jump, l_top2);
3109 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003110 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01003111 }
3112}
3113
Damien George1463c1f2014-04-25 23:52:57 +01003114STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3115#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003116 // see http://www.python.org/dev/peps/pep-0257/
3117
3118 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003119 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003120 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003121 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003122 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003123 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3124 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003125 for (int i = 0; i < num_nodes; i++) {
3126 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003127 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 +00003128 // not a newline, so this is the first statement; finish search
3129 break;
3130 }
3131 }
3132 // 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 +00003133 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003134 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003135 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003136 } else {
3137 return;
3138 }
3139
3140 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003141 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3142 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003143 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3144 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3145 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3146 // compile the doc string
3147 compile_node(comp, pns->nodes[0]);
3148 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003149 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003150 }
3151 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003152#else
3153 (void)comp;
3154 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003155#endif
Damien429d7192013-10-04 19:53:11 +01003156}
3157
Damien George1463c1f2014-04-25 23:52:57 +01003158STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003159 comp->pass = pass;
3160 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003161 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003162 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003163
Damien George36db6bc2014-05-07 17:24:22 +01003164 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003165 // reset maximum stack sizes in scope
3166 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003167 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003168 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003169 }
3170
Damien5ac1b2e2013-10-18 19:58:12 +01003171#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003172 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003173 scope_print_info(scope);
3174 }
Damien5ac1b2e2013-10-18 19:58:12 +01003175#endif
Damien429d7192013-10-04 19:53:11 +01003176
3177 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003178 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3179 assert(scope->kind == SCOPE_MODULE);
3180 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3181 compile_node(comp, pns->nodes[0]); // compile the expression
3182 EMIT(return_value);
3183 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003184 if (!comp->is_repl) {
3185 check_for_doc_string(comp, scope->pn);
3186 }
Damien429d7192013-10-04 19:53:11 +01003187 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003188 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003189 EMIT(return_value);
3190 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003191 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3192 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3193 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003194
3195 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003196 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003197 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003198 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003199 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003200 } else {
3201 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003202
Damien George2ac4af62014-08-15 16:45:41 +01003203 // argument annotations
3204 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3205
3206 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003207 mp_parse_node_t pn_annotation = pns->nodes[2];
3208 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3209 #if MICROPY_EMIT_NATIVE
3210 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3211 // nodes[2] can be null or a test-expr
3212 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3213 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3214 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3215 } else {
3216 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3217 }
Damien George2ac4af62014-08-15 16:45:41 +01003218 }
Damien Georgea5190a72014-08-15 22:39:08 +01003219 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003220 }
Damien George2ac4af62014-08-15 16:45:41 +01003221 }
Damien429d7192013-10-04 19:53:11 +01003222
3223 compile_node(comp, pns->nodes[3]); // 3 is function body
3224 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003225 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003226 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003227 EMIT(return_value);
3228 }
3229 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003230 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3231 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3232 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003233
3234 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003235 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003236 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003237 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003238 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3239 }
3240
3241 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003242
3243 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3244 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3245 EMIT(pop_top);
3246 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3247 }
Damien429d7192013-10-04 19:53:11 +01003248 EMIT(return_value);
3249 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3250 // a bit of a hack at the moment
3251
Damiend99b0522013-12-21 18:17:45 +00003252 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3253 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3254 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3255 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3256 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003257
Damien George708c0732014-04-27 19:23:46 +01003258 // We need a unique name for the comprehension argument (the iterator).
3259 // CPython uses .0, but we should be able to use anything that won't
3260 // clash with a user defined variable. Best to use an existing qstr,
3261 // so we use the blank qstr.
3262#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003263 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003264#else
3265 qstr qstr_arg = MP_QSTR_;
3266#endif
Damien George36db6bc2014-05-07 17:24:22 +01003267 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003268 bool added;
3269 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3270 assert(added);
3271 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003272 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003273 }
3274
3275 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003276 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003277 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003278 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003279 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003280 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003281 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003282 #endif
Damien429d7192013-10-04 19:53:11 +01003283 }
3284
Damien George6f355fd2014-04-10 14:11:31 +01003285 uint l_end = comp_next_label(comp);
3286 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003287 EMIT_ARG(load_id, qstr_arg);
3288 EMIT_ARG(label_assign, l_top);
3289 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003290 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3291 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003292 EMIT_ARG(jump, l_top);
3293 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003294 EMIT(for_iter_end);
3295
3296 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003297 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003298 }
3299 EMIT(return_value);
3300 } else {
3301 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003302 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3303 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3304 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003305
Damien George36db6bc2014-05-07 17:24:22 +01003306 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003307 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003308 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003309 assert(added);
3310 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003311 }
3312
Damien Georgeb9791222014-01-23 00:34:21 +00003313 EMIT_ARG(load_id, MP_QSTR___name__);
3314 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003315 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 +00003316 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003317
3318 check_for_doc_string(comp, pns->nodes[2]);
3319 compile_node(comp, pns->nodes[2]); // 2 is class body
3320
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003321 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003322 assert(id != NULL);
3323 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003324 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003325 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003326#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003327 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003328#else
Damien George0abb5602015-01-16 12:24:49 +00003329 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003330#endif
Damien429d7192013-10-04 19:53:11 +01003331 }
3332 EMIT(return_value);
3333 }
3334
Damien415eb6f2013-10-05 12:19:06 +01003335 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003336
3337 // make sure we match all the exception levels
3338 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003339}
3340
Damien George094d4502014-04-02 17:31:27 +01003341#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003342// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003343STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003344 comp->pass = pass;
3345 comp->scope_cur = scope;
3346 comp->next_label = 1;
3347
3348 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003349 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003350 return;
3351 }
3352
Damien George36db6bc2014-05-07 17:24:22 +01003353 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003354 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003355 }
3356
Damien826005c2013-10-05 23:17:28 +01003357 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003358 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3359 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3360 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003361
Damiend99b0522013-12-21 18:17:45 +00003362 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003363
Damiena2f2f7d2013-10-06 00:14:13 +01003364 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003365 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003366 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003367 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003368 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003369 if (comp->compile_error != MP_OBJ_NULL) {
3370 goto inline_asm_error;
3371 }
Damiena2f2f7d2013-10-06 00:14:13 +01003372 }
3373
Damiend99b0522013-12-21 18:17:45 +00003374 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003375
Damiend99b0522013-12-21 18:17:45 +00003376 mp_parse_node_t pn_body = pns->nodes[3]; // body
3377 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003378 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003379
Damien Georgecbd2f742014-01-19 11:48:48 +00003380 /*
Damien George36db6bc2014-05-07 17:24:22 +01003381 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003382 //printf("----\n");
3383 scope_print_info(scope);
3384 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003385 */
Damien826005c2013-10-05 23:17:28 +01003386
3387 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003388 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3389 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003390 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3391 // no instructions
3392 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003393 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003394 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003395 not_an_instruction:
Damien Georgea26dc502014-04-12 17:54:52 +01003396 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3397 return;
3398 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003399
3400 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003401 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003402 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3403 goto not_an_instruction;
3404 }
Damiend99b0522013-12-21 18:17:45 +00003405 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003406 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3407 goto not_an_instruction;
3408 }
3409 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3410 goto not_an_instruction;
3411 }
3412 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3413 goto not_an_instruction;
3414 }
Damiend99b0522013-12-21 18:17:45 +00003415 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003416
3417 // parse node looks like an instruction
3418 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003419 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3420 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3421 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003422 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003423
3424 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003425 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003426 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003427 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003428 return;
3429 }
Damien George6f355fd2014-04-10 14:11:31 +01003430 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003431 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003432 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003433 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003434 } else if (op == MP_QSTR_align) {
3435 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3436 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3437 return;
3438 }
Damien George36db6bc2014-05-07 17:24:22 +01003439 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003440 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3441 }
3442 } else if (op == MP_QSTR_data) {
3443 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3444 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3445 return;
3446 }
Damien George36db6bc2014-05-07 17:24:22 +01003447 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003448 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003449 for (uint j = 1; j < n_args; j++) {
3450 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003451 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3452 return;
3453 }
Damien George50912e72015-01-20 11:55:10 +00003454 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003455 }
3456 }
Damien826005c2013-10-05 23:17:28 +01003457 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003458 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003459 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003460 }
3461 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003462
3463 if (comp->compile_error != MP_OBJ_NULL) {
3464 pns = pns2; // this is the parse node that had the error
3465 goto inline_asm_error;
3466 }
Damien826005c2013-10-05 23:17:28 +01003467 }
3468
Damien George36db6bc2014-05-07 17:24:22 +01003469 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003470 EMIT_INLINE_ASM(end_pass);
3471 }
3472
3473 if (comp->compile_error != MP_OBJ_NULL) {
3474 // inline assembler had an error; add traceback to its exception
3475 inline_asm_error:
3476 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 +01003477 }
Damien429d7192013-10-04 19:53:11 +01003478}
Damien George094d4502014-04-02 17:31:27 +01003479#endif
Damien429d7192013-10-04 19:53:11 +01003480
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003481STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003482#if !MICROPY_EMIT_CPYTHON
3483 // in Micro Python we put the *x parameter after all other parameters (except **y)
3484 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3485 id_info_t *id_param = NULL;
3486 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3487 id_info_t *id = &scope->id_info[i];
3488 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3489 if (id_param != NULL) {
3490 // swap star param with last param
3491 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3492 }
3493 break;
3494 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3495 id_param = id;
3496 }
3497 }
3498 }
3499#endif
3500
Damien429d7192013-10-04 19:53:11 +01003501 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003502 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003503 scope->num_locals = 0;
3504 for (int i = 0; i < scope->id_info_len; i++) {
3505 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003506 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003507 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3508 continue;
3509 }
3510 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3511 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3512 }
Damien George2827d622014-04-27 15:50:52 +01003513 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003514 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003515 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003516 }
3517 }
3518
3519 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003520#if MICROPY_EMIT_CPYTHON
3521 int num_cell = 0;
3522#endif
Damien9ecbcff2013-12-11 00:41:43 +00003523 for (int i = 0; i < scope->id_info_len; i++) {
3524 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003525#if MICROPY_EMIT_CPYTHON
3526 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003527 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003528 id->local_num = num_cell;
3529 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003530 }
Damien George6baf76e2013-12-30 22:32:17 +00003531#else
3532 // in Micro Python the cells come right after the fast locals
3533 // parameters are not counted here, since they remain at the start
3534 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003535 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003536 id->local_num = scope->num_locals;
3537 scope->num_locals += 1;
3538 }
3539#endif
Damien9ecbcff2013-12-11 00:41:43 +00003540 }
Damien9ecbcff2013-12-11 00:41:43 +00003541
3542 // compute the index of free vars (freevars[idx] in CPython)
3543 // make sure they are in the order of the parent scope
3544 if (scope->parent != NULL) {
3545 int num_free = 0;
3546 for (int i = 0; i < scope->parent->id_info_len; i++) {
3547 id_info_t *id = &scope->parent->id_info[i];
3548 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3549 for (int j = 0; j < scope->id_info_len; j++) {
3550 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003551 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003552 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003553#if MICROPY_EMIT_CPYTHON
3554 // in CPython the frees are numbered after the cells
3555 id2->local_num = num_cell + num_free;
3556#else
3557 // in Micro Python the frees come first, before the params
3558 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003559#endif
3560 num_free += 1;
3561 }
3562 }
3563 }
Damien429d7192013-10-04 19:53:11 +01003564 }
Damien George6baf76e2013-12-30 22:32:17 +00003565#if !MICROPY_EMIT_CPYTHON
3566 // in Micro Python shift all other locals after the free locals
3567 if (num_free > 0) {
3568 for (int i = 0; i < scope->id_info_len; i++) {
3569 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003570 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003571 id->local_num += num_free;
3572 }
3573 }
Damien George2827d622014-04-27 15:50:52 +01003574 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 +00003575 scope->num_locals += num_free;
3576 }
3577#endif
Damien429d7192013-10-04 19:53:11 +01003578 }
3579
Damien George8725f8f2014-02-15 19:33:11 +00003580 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003581
3582#if MICROPY_EMIT_CPYTHON
3583 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003584 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) {
3585 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003586 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003587 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003588 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 +00003589 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003590 }
3591 }
Damien George882b3632014-04-02 15:56:31 +01003592#endif
3593
Damien429d7192013-10-04 19:53:11 +01003594 int num_free = 0;
3595 for (int i = 0; i < scope->id_info_len; i++) {
3596 id_info_t *id = &scope->id_info[i];
3597 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3598 num_free += 1;
3599 }
3600 }
3601 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003602 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003603 }
3604}
3605
Damien George65cad122014-04-06 11:48:15 +01003606mp_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 +01003607 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003608 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003609 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003610 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003611
Damien826005c2013-10-05 23:17:28 +01003612 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003613 mp_map_t consts;
3614 mp_map_init(&consts, 0);
3615 pn = fold_constants(comp, pn, &consts);
3616 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003617
3618 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003619 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003620
Damien826005c2013-10-05 23:17:28 +01003621 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003622 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003623 comp->emit_method_table = &emit_pass1_method_table;
3624 comp->emit_inline_asm = NULL;
3625 comp->emit_inline_asm_method_table = NULL;
3626 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003627 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003628 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003629#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003630 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003631 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003632#endif
Damien826005c2013-10-05 23:17:28 +01003633 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003634 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003635 }
3636
3637 // update maximim number of labels needed
3638 if (comp->next_label > max_num_labels) {
3639 max_num_labels = comp->next_label;
3640 }
Damien429d7192013-10-04 19:53:11 +01003641 }
3642
Damien826005c2013-10-05 23:17:28 +01003643 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003644 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 +00003645 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003646 }
3647
Damien826005c2013-10-05 23:17:28 +01003648 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003649 emit_pass1_free(comp->emit);
3650
Damien826005c2013-10-05 23:17:28 +01003651 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003652#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003653 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003654#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003655 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003656#endif
Damien3ef4abb2013-10-12 16:53:13 +01003657#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003658 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003659#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003660#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003661 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003662 if (false) {
3663 // dummy
3664
Damien3ef4abb2013-10-12 16:53:13 +01003665#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003666 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003667 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003668 if (emit_inline_thumb == NULL) {
3669 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3670 }
3671 comp->emit = NULL;
3672 comp->emit_method_table = NULL;
3673 comp->emit_inline_asm = emit_inline_thumb;
3674 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003675 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003676 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003677 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003678 }
Damienc025ebb2013-10-12 14:30:21 +01003679#endif
3680
Damien826005c2013-10-05 23:17:28 +01003681 } else {
Damienc025ebb2013-10-12 14:30:21 +01003682
3683 // choose the emit type
3684
Damien3ef4abb2013-10-12 16:53:13 +01003685#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003686 comp->emit = emit_cpython_new(max_num_labels);
3687 comp->emit_method_table = &emit_cpython_method_table;
3688#else
Damien826005c2013-10-05 23:17:28 +01003689 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003690
3691#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003692 case MP_EMIT_OPT_NATIVE_PYTHON:
3693 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003694#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003695 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003696 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003697 }
Damien13ed3a62013-10-08 09:05:10 +01003698 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003699#elif MICROPY_EMIT_X86
3700 if (emit_native == NULL) {
3701 emit_native = emit_native_x86_new(max_num_labels);
3702 }
3703 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003704#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003705 if (emit_native == NULL) {
3706 emit_native = emit_native_thumb_new(max_num_labels);
3707 }
3708 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003709#elif MICROPY_EMIT_ARM
3710 if (emit_native == NULL) {
3711 emit_native = emit_native_arm_new(max_num_labels);
3712 }
3713 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003714#endif
3715 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003716 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003717 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003718#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003719
Damien826005c2013-10-05 23:17:28 +01003720 default:
3721 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003722 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003723 }
3724 comp->emit = emit_bc;
3725 comp->emit_method_table = &emit_bc_method_table;
3726 break;
3727 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003728#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003729
Damien George1e1779e2015-01-14 00:20:28 +00003730 // need a pass to compute stack size
3731 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3732
Damien George36db6bc2014-05-07 17:24:22 +01003733 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003734 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003735 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3736 }
3737
3738 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003739 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003740 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003741 }
Damien6cdd3af2013-10-05 18:08:26 +01003742 }
Damien429d7192013-10-04 19:53:11 +01003743 }
3744
Damien George41d02b62014-01-24 22:42:28 +00003745 // free the emitters
3746#if !MICROPY_EMIT_CPYTHON
3747 if (emit_bc != NULL) {
3748 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003749 }
Damien George41d02b62014-01-24 22:42:28 +00003750#if MICROPY_EMIT_NATIVE
3751 if (emit_native != NULL) {
3752#if MICROPY_EMIT_X64
3753 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003754#elif MICROPY_EMIT_X86
3755 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003756#elif MICROPY_EMIT_THUMB
3757 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003758#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003759 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003760#endif
3761 }
3762#endif
3763#if MICROPY_EMIT_INLINE_THUMB
3764 if (emit_inline_thumb != NULL) {
3765 emit_inline_thumb_free(emit_inline_thumb);
3766 }
3767#endif
3768#endif // !MICROPY_EMIT_CPYTHON
3769
Damien George52b5d762014-09-23 15:31:56 +00003770 // free the parse tree
3771 mp_parse_node_free(pn);
3772
Damien George41d02b62014-01-24 22:42:28 +00003773 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003774 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003775 for (scope_t *s = module_scope; s;) {
3776 scope_t *next = s->next;
3777 scope_free(s);
3778 s = next;
3779 }
Damien5ac1b2e2013-10-18 19:58:12 +01003780
Damien George41d02b62014-01-24 22:42:28 +00003781 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003782 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003783 m_del_obj(compiler_t, comp);
3784
Damien Georgea91ac202014-10-05 19:01:34 +01003785 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003786 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003787 } else {
3788#if MICROPY_EMIT_CPYTHON
3789 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003790 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003791 return mp_const_true;
3792#else
3793 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003794 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003795#endif
3796 }
Damien429d7192013-10-04 19:53:11 +01003797}