blob: 62b6f05108606e687bf23956cf7bb4211e0f8e5d [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
36#include "py/smallint.h"
37#include "py/runtime.h"
38#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010039
40// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien George41125902015-03-26 16:44:14 +000052#define NEED_METHOD_TABLE (MICROPY_EMIT_CPYTHON || MICROPY_EMIT_NATIVE)
53
54#if NEED_METHOD_TABLE
55
56// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000057#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
58#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000059#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
60#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien Georgeb9791222014-01-23 00:34:21 +000061#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
62#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 +010063
Damien George41125902015-03-26 16:44:14 +000064#else
65
66// if we only have the bytecode emitter enabled then we can do a direct call to the functions
67#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
68#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
69#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
70#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
71
72#endif
73
Damien Georgea91ac202014-10-05 19:01:34 +010074// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010075typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000076 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010077
Damien George78035b92014-04-09 12:27:39 +010078 uint8_t is_repl;
79 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010080 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010081 uint8_t have_star;
82
83 // try to keep compiler clean from nlr
84 // this is set to an exception object if we have a compile error
85 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010086
Damien George6f355fd2014-04-10 14:11:31 +010087 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010088
Damien George69b89d22014-04-11 13:38:30 +000089 uint16_t num_dict_params;
90 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010091
Damien Georgea91ac202014-10-05 19:01:34 +010092 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
93 uint16_t continue_label;
94 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000095 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010096
Damien429d7192013-10-04 19:53:11 +010097 scope_t *scope_head;
98 scope_t *scope_cur;
99
Damien6cdd3af2013-10-05 18:08:26 +0100100 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000101 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100102 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000103 #endif
Damien826005c2013-10-05 23:17:28 +0100104
Damien Georgea77ffe62015-03-14 12:59:31 +0000105 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +0100106 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
107 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien Georgea77ffe62015-03-14 12:59:31 +0000108 #endif
Damien429d7192013-10-04 19:53:11 +0100109} compiler_t;
110
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100111STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +0100112 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
113 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100114 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George8dfbd2d2015-02-13 01:00:51 +0000115 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 +0100116 } else {
Damien Georgea91ac202014-10-05 19:01:34 +0100117 // we don't have a line number, so just pass 0
Damien George8dfbd2d2015-02-13 01:00:51 +0000118 mp_obj_exception_add_traceback(exc, comp->source_file, 0, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100119 }
Damien Georgea91ac202014-10-05 19:01:34 +0100120 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000121}
122
Damien Georgeddd1e182015-01-10 14:07:24 +0000123#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100124STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300125 #if MICROPY_PY_UCTYPES
126 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
127 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100128 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100129 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100130};
Damien Georgeddd1e182015-01-10 14:07:24 +0000131STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
132#endif
Damien George57e99eb2014-04-10 22:42:11 +0100133
Damien Georgeffae48d2014-05-08 15:58:39 +0000134// this function is essentially a simple preprocessor
135STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
136 if (0) {
137 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100138#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000139 } else if (MP_PARSE_NODE_IS_ID(pn)) {
140 // lookup identifier in table of dynamic constants
141 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
142 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
143 if (elem != NULL) {
144 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
145 }
146#endif
147 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000148 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100149
Damien Georgeffae48d2014-05-08 15:58:39 +0000150 // fold some parse nodes before folding their arguments
151 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100152#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000153 case PN_expr_stmt:
154 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
155 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
156 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
157 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
158 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
159 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
160 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
161 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
162 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
163 ) {
164 // code to assign dynamic constants: id = const(value)
165
166 // get the id
167 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
168
169 // get the value
170 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
171 pn_value = fold_constants(comp, pn_value, consts);
172 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
173 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
174 break;
175 }
Damien George40f3c022014-07-03 13:25:24 +0100176 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000177
178 // store the value in the table of dynamic constants
179 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
180 if (elem->value != MP_OBJ_NULL) {
181 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
182 break;
183 }
184 elem->value = MP_OBJ_NEW_SMALL_INT(value);
185
186 // replace const(value) with value
187 pns1->nodes[0] = pn_value;
188
189 // finished folding this assignment
190 return pn;
191 }
192 }
193 }
194 break;
195#endif
Damien George5042bce2014-05-25 22:06:06 +0100196 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000197 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000198 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100199 return pn;
Damien429d7192013-10-04 19:53:11 +0100200 }
201
Damien Georgeffae48d2014-05-08 15:58:39 +0000202 // fold arguments
203 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
204 for (int i = 0; i < n; i++) {
205 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
206 }
207
208 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000209 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100210 case PN_atom_paren:
211 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
212 // (int)
213 pn = pns->nodes[0];
214 }
215 break;
216
217 case PN_expr:
218 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
219 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100220 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
221 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100222 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
223 }
224 break;
225
226 case PN_and_expr:
227 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
228 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100229 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
230 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100231 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
232 }
233 break;
234
Damien429d7192013-10-04 19:53:11 +0100235 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000236 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 +0100237 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
238 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000239 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100240 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000241 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 +0100242 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
243 }
Damien George0bb97132015-02-27 14:25:47 +0000244 } else {
245 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100246 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000247 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200248 // Shifting to big amounts is underfined behavior
249 // in C and is CPU-dependent; propagate sign bit.
250 arg1 = BITS_PER_WORD - 1;
251 }
Damiend99b0522013-12-21 18:17:45 +0000252 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100253 }
254 }
255 break;
256
257 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100258 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000259 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100260 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
261 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000262 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100263 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000264 arg0 += arg1;
Damien George0bb97132015-02-27 14:25:47 +0000265 } else {
266 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100267 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000268 arg0 -= arg1;
Damien0efb3a12013-10-12 16:16:56 +0100269 }
Damien Georged1e355e2014-05-28 14:51:12 +0100270 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100271 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000272 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100273 }
274 }
275 break;
276
277 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000278 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 +0100279 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
280 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000281 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100282 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000283 if (!mp_small_int_mul_overflow(arg0, arg1)) {
284 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100285 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000286 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
287 }
288 }
Damiend99b0522013-12-21 18:17:45 +0000289 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100290 // int / int
291 // pass
Damiend99b0522013-12-21 18:17:45 +0000292 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100293 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000294 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damien George0bb97132015-02-27 14:25:47 +0000295 } else {
296 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300297 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100298 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000299 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 +0300300 }
Damien429d7192013-10-04 19:53:11 +0100301 }
302 }
303 break;
304
305 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000306 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100307 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000308 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100309 // +int
Damiend99b0522013-12-21 18:17:45 +0000310 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
311 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100312 // -int
Damiend99b0522013-12-21 18:17:45 +0000313 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
Damien George0bb97132015-02-27 14:25:47 +0000314 } else {
315 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100316 // ~int
Damiend99b0522013-12-21 18:17:45 +0000317 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100318 }
319 }
320 break;
321
322 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100323 if (0) {
324#if MICROPY_EMIT_CPYTHON
325 } 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 +0100326 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100327 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000328 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
329 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200330 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100331 if (power >= 0) {
332 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200333 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100334 for (; power > 0; power--) {
335 ans *= base;
336 }
Damiend99b0522013-12-21 18:17:45 +0000337 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100338 }
339 }
Damien George57e99eb2014-04-10 22:42:11 +0100340#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000341#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100342 } 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])) {
343 // id.id
344 // look it up in constant table, see if it can be replaced with an integer
345 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
346 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
347 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
348 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
349 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
350 if (elem != NULL) {
351 mp_obj_t dest[2];
352 mp_load_method_maybe(elem->value, q_attr, dest);
353 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100354 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000355 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100356 }
357 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000358#endif
Damien429d7192013-10-04 19:53:11 +0100359 }
360 break;
361 }
362 }
363
364 return pn;
365}
366
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200367STATIC 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 +0100368STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000369STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100370
Damien George6f355fd2014-04-10 14:11:31 +0100371STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100372 return comp->next_label++;
373}
374
Damien George8dcc0c72014-03-27 10:55:21 +0000375STATIC void compile_increase_except_level(compiler_t *comp) {
376 comp->cur_except_level += 1;
377 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
378 comp->scope_cur->exc_stack_size = comp->cur_except_level;
379 }
380}
381
382STATIC void compile_decrease_except_level(compiler_t *comp) {
383 assert(comp->cur_except_level > 0);
384 comp->cur_except_level -= 1;
385}
386
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200387STATIC 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 +0100388 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100389 scope->parent = comp->scope_cur;
390 scope->next = NULL;
391 if (comp->scope_head == NULL) {
392 comp->scope_head = scope;
393 } else {
394 scope_t *s = comp->scope_head;
395 while (s->next != NULL) {
396 s = s->next;
397 }
398 s->next = scope;
399 }
400 return scope;
401}
402
Damien George963a5a32015-01-16 17:47:07 +0000403STATIC 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)) {
404 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000405 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
406 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100407 for (int i = 0; i < num_nodes; i++) {
408 f(comp, pns->nodes[i]);
409 }
Damiend99b0522013-12-21 18:17:45 +0000410 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100411 f(comp, pn);
412 }
413}
414
Damien George969a6b32014-12-10 22:07:04 +0000415STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000416 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100417 for (int i = 0; i < num_nodes; i++) {
418 compile_node(comp, pns->nodes[i]);
419 }
420}
421
Damien George542bd6b2015-03-26 14:42:40 +0000422STATIC void compile_load_id(compiler_t *comp, qstr qst) {
423 if (comp->pass == MP_PASS_SCOPE) {
424 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
425 } else {
Damien George41125902015-03-26 16:44:14 +0000426 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000427 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000428 #else
429 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
430 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000431 }
432}
433
434STATIC void compile_store_id(compiler_t *comp, qstr qst) {
435 if (comp->pass == MP_PASS_SCOPE) {
436 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
437 } else {
Damien George41125902015-03-26 16:44:14 +0000438 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000439 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000440 #else
441 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
442 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000443 }
444}
445
446STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
447 if (comp->pass == MP_PASS_SCOPE) {
448 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
449 } else {
Damien George41125902015-03-26 16:44:14 +0000450 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000451 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000452 #else
453 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
454 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000455 }
456}
457
Damien3ef4abb2013-10-12 16:53:13 +0100458#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200459STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100460 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
461 return true;
462 }
Damien George4c81ba82015-01-13 16:21:23 +0000463 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
464 return true;
465 }
Damien George7d414a12015-02-08 01:57:40 +0000466 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
467 return true;
468 }
Damiend99b0522013-12-21 18:17:45 +0000469 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100470 return false;
471 }
Damiend99b0522013-12-21 18:17:45 +0000472 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100473 return false;
474 }
475 return true;
476}
477
Damien George5042bce2014-05-25 22:06:06 +0100478STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000479 bool has_single_quote = false;
480 bool has_double_quote = false;
481 for (int i = 0; i < len; i++) {
482 if (str[i] == '\'') {
483 has_single_quote = true;
484 } else if (str[i] == '"') {
485 has_double_quote = true;
486 }
487 }
488 if (bytes) {
489 vstr_printf(vstr, "b");
490 }
491 bool quote_single = false;
492 if (has_single_quote && !has_double_quote) {
493 vstr_printf(vstr, "\"");
494 } else {
495 quote_single = true;
496 vstr_printf(vstr, "'");
497 }
498 for (int i = 0; i < len; i++) {
499 if (str[i] == '\n') {
500 vstr_printf(vstr, "\\n");
501 } else if (str[i] == '\\') {
502 vstr_printf(vstr, "\\\\");
503 } else if (str[i] == '\'' && quote_single) {
504 vstr_printf(vstr, "\\'");
505 } else {
506 vstr_printf(vstr, "%c", str[i]);
507 }
508 }
509 if (has_single_quote && !has_double_quote) {
510 vstr_printf(vstr, "\"");
511 } else {
512 vstr_printf(vstr, "'");
513 }
514}
515
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200516STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000517 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 +0100518 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000519 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 +0100520 return;
521 }
522
Damien George7d414a12015-02-08 01:57:40 +0000523 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
524 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
525 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
526 return;
527 }
528
Damiend99b0522013-12-21 18:17:45 +0000529 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200530 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
531 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
532 return;
533 }
534
Damien George42f3de92014-10-03 17:44:14 +0000535 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000536 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
537 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100538 case MP_PARSE_NODE_STRING:
539 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100540 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100541 const byte *str = qstr_data(arg, &len);
542 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
543 break;
544 }
Damiend99b0522013-12-21 18:17:45 +0000545 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100546 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000547 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
548 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
549 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100550 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100551 }
552 break;
553 default: assert(0);
554 }
555}
556
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200557STATIC 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 +0100558 int n = 0;
559 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000560 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100561 }
562 int total = n;
563 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000564 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100565 total += 1;
Damien3a205172013-10-12 15:01:56 +0100566 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100567 is_const = false;
568 }
569 }
570 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100571 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100572 is_const = false;
573 break;
574 }
575 }
576 if (total > 0 && is_const) {
577 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000578 vstr_t *vstr = vstr_new();
579 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000580 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000581 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100582 need_comma = true;
583 }
584 for (int i = 0; i < n; i++) {
585 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000586 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100587 }
Damien02f89412013-12-12 15:13:36 +0000588 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100589 need_comma = true;
590 }
591 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000592 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100593 } else {
Damien02f89412013-12-12 15:13:36 +0000594 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100595 }
Damien George0d3cb672015-01-28 23:43:01 +0000596 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000597 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100598 } else {
Damiend99b0522013-12-21 18:17:45 +0000599 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100600 compile_node(comp, pn);
601 }
602 for (int i = 0; i < n; i++) {
603 compile_node(comp, pns_list->nodes[i]);
604 }
Damien Georgeb9791222014-01-23 00:34:21 +0000605 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100606 }
607}
Damien3a205172013-10-12 15:01:56 +0100608#endif
609
610// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100611STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100612#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100613 cpython_c_tuple(comp, pn, pns_list);
614#else
615 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000616 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100617 compile_node(comp, pn);
618 total += 1;
619 }
620 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000621 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100622 for (int i = 0; i < n; i++) {
623 compile_node(comp, pns_list->nodes[i]);
624 }
625 total += n;
626 }
Damien Georgeb9791222014-01-23 00:34:21 +0000627 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100628#endif
629}
Damien429d7192013-10-04 19:53:11 +0100630
Damien George969a6b32014-12-10 22:07:04 +0000631STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100632 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000633 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100634}
635
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200636STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000637 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
638 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100639}
640
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200641STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000642 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
643 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100644}
645
Damien3ef4abb2013-10-12 16:53:13 +0100646#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100647// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200648STATIC 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 +0100649 if (node_is_const_false(pn)) {
650 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000651 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100652 }
653 return;
654 } else if (node_is_const_true(pn)) {
655 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000656 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100657 }
658 return;
Damiend99b0522013-12-21 18:17:45 +0000659 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
660 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
661 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
662 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100663 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100664 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100665 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100666 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100667 }
Damien3a205172013-10-12 15:01:56 +0100668 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000669 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100670 } else {
671 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100672 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100673 }
674 }
675 return;
Damiend99b0522013-12-21 18:17:45 +0000676 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100677 if (jump_if == false) {
678 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100679 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100680 }
681 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100682 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100683 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100684 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100685 }
Damien3a205172013-10-12 15:01:56 +0100686 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000687 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100688 }
689 return;
Damiend99b0522013-12-21 18:17:45 +0000690 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100691 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100692 return;
693 }
694 }
695
696 // nothing special, fall back to default compiling for node and jump
697 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000698 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100699}
Damien3a205172013-10-12 15:01:56 +0100700#endif
Damien429d7192013-10-04 19:53:11 +0100701
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200702STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100703#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100704 cpython_c_if_cond(comp, pn, jump_if, label, false);
705#else
706 if (node_is_const_false(pn)) {
707 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000708 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100709 }
710 return;
711 } else if (node_is_const_true(pn)) {
712 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000713 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100714 }
715 return;
Damiend99b0522013-12-21 18:17:45 +0000716 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
717 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
718 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
719 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100720 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000721 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100722 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100723 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000724 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100725 }
Damien George0b2fd912015-02-28 14:37:54 +0000726 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000727 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100728 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000729 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100730 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000731 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100732 }
733 }
734 return;
Damiend99b0522013-12-21 18:17:45 +0000735 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100736 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000737 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100738 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000739 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100740 }
Damiend99b0522013-12-21 18:17:45 +0000741 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100742 c_if_cond(comp, pns->nodes[0], !jump_if, label);
743 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100744 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
745 // cond is something in parenthesis
746 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
747 // empty tuple, acts as false for the condition
748 if (jump_if == false) {
749 EMIT_ARG(jump, label);
750 }
751 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
752 // non-empty tuple, acts as true for the condition
753 if (jump_if == true) {
754 EMIT_ARG(jump, label);
755 }
756 } else {
757 // parenthesis around 1 item, is just that item
758 c_if_cond(comp, pns->nodes[0], jump_if, label);
759 }
760 return;
Damien3a205172013-10-12 15:01:56 +0100761 }
762 }
763
764 // nothing special, fall back to default compiling for node and jump
765 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000766 EMIT_ARG(pop_jump_if, jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100767#endif
Damien429d7192013-10-04 19:53:11 +0100768}
769
770typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100771STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100772
Damien George6be0b0a2014-08-15 14:30:52 +0100773STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100774 if (assign_kind != ASSIGN_AUG_STORE) {
775 compile_node(comp, pns->nodes[0]);
776 }
777
Damiend99b0522013-12-21 18:17:45 +0000778 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
779 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
780 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
781 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100782 if (assign_kind != ASSIGN_AUG_STORE) {
783 for (int i = 0; i < n - 1; i++) {
784 compile_node(comp, pns1->nodes[i]);
785 }
786 }
Damiend99b0522013-12-21 18:17:45 +0000787 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
788 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100789 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000790 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100791 if (assign_kind == ASSIGN_AUG_STORE) {
792 EMIT(rot_three);
793 EMIT(store_subscr);
794 } else {
795 compile_node(comp, pns1->nodes[0]);
796 if (assign_kind == ASSIGN_AUG_LOAD) {
797 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100798 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100799 } else {
800 EMIT(store_subscr);
801 }
802 }
Damiend99b0522013-12-21 18:17:45 +0000803 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
804 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100805 if (assign_kind == ASSIGN_AUG_LOAD) {
806 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000807 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100808 } else {
809 if (assign_kind == ASSIGN_AUG_STORE) {
810 EMIT(rot_two);
811 }
Damien Georgeb9791222014-01-23 00:34:21 +0000812 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100813 }
814 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100815 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100816 }
817 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100818 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100819 }
820
Damiend99b0522013-12-21 18:17:45 +0000821 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100822 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100823 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100824
825 return;
826
827cannot_assign:
828 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100829}
830
Damien George0288cf02014-04-11 11:53:00 +0000831// 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 +0100832STATIC 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 +0000833 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
834
835 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000836 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000837 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
838 EMIT_ARG(unpack_ex, 0, num_tail);
839 have_star_index = 0;
840 }
Damien George963a5a32015-01-16 17:47:07 +0000841 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000842 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000843 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000844 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
845 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100846 } else {
Damien George9d181f62014-04-27 16:55:27 +0100847 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100848 return;
849 }
850 }
851 }
Damien George963a5a32015-01-16 17:47:07 +0000852 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000853 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100854 }
Damien George0288cf02014-04-11 11:53:00 +0000855 if (num_head != 0) {
856 if (0 == have_star_index) {
857 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100858 } else {
Damien George0288cf02014-04-11 11:53:00 +0000859 c_assign(comp, node_head, ASSIGN_STORE);
860 }
861 }
Damien George963a5a32015-01-16 17:47:07 +0000862 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000863 if (num_head + i == have_star_index) {
864 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
865 } else {
866 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100867 }
868 }
869}
870
871// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100872STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100873 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000874 assert(!MP_PARSE_NODE_IS_NULL(pn));
875 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000876 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000877 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100878 switch (assign_kind) {
879 case ASSIGN_STORE:
880 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000881 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100882 break;
883 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000884 default:
Damien George542bd6b2015-03-26 14:42:40 +0000885 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100886 break;
887 }
888 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100889 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100890 return;
891 }
892 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000893 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000894 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
895 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100896 case PN_power:
897 // lhs is an index or attribute
898 c_assign_power(comp, pns, assign_kind);
899 break;
900
901 case PN_testlist_star_expr:
902 case PN_exprlist:
903 // lhs is a tuple
904 if (assign_kind != ASSIGN_STORE) {
905 goto bad_aug;
906 }
Damien George0288cf02014-04-11 11:53:00 +0000907 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100908 break;
909
910 case PN_atom_paren:
911 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000912 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100913 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100914 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000915 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000916 if (assign_kind != ASSIGN_STORE) {
917 goto bad_aug;
918 }
Damiend99b0522013-12-21 18:17:45 +0000919 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100920 goto testlist_comp;
921 } else {
922 // parenthesis around 1 item, is just that item
923 pn = pns->nodes[0];
924 goto tail_recursion;
925 }
926 break;
927
928 case PN_atom_bracket:
929 // lhs is something in brackets
930 if (assign_kind != ASSIGN_STORE) {
931 goto bad_aug;
932 }
Damiend99b0522013-12-21 18:17:45 +0000933 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100934 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000935 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000936 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
937 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100938 goto testlist_comp;
939 } else {
940 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000941 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100942 }
943 break;
944
945 default:
Damien George9d181f62014-04-27 16:55:27 +0100946 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100947 }
948 return;
949
950 testlist_comp:
951 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000952 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
953 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
954 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100955 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000956 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000957 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000958 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100959 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000960 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
961 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000962 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100963 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100964 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100965 } else {
966 // sequence with 2 items
967 goto sequence_with_2_items;
968 }
969 } else {
970 // sequence with 2 items
971 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000972 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100973 }
974 return;
975 }
976 return;
977
Damien George9d181f62014-04-27 16:55:27 +0100978 cannot_assign:
979 compile_syntax_error(comp, pn, "can't assign to expression");
980 return;
981
Damien429d7192013-10-04 19:53:11 +0100982 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100983 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100984}
985
986// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100987// if we are not in CPython compatibility mode then:
988// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
989// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
990// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100991STATIC 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 +0100992 assert(n_pos_defaults >= 0);
993 assert(n_kw_defaults >= 0);
994
Damien429d7192013-10-04 19:53:11 +0100995 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000996 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100997 int nfree = 0;
998 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000999 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
1000 id_info_t *id = &comp->scope_cur->id_info[i];
1001 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
1002 for (int j = 0; j < this_scope->id_info_len; j++) {
1003 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01001004 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +00001005#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +01001006 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001007#else
1008 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +00001009 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001010#endif
Damien318aec62013-12-10 18:28:17 +00001011 nfree += 1;
1012 }
1013 }
Damien429d7192013-10-04 19:53:11 +01001014 }
1015 }
1016 }
Damien429d7192013-10-04 19:53:11 +01001017
1018 // make the function/closure
1019 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001020 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001021 } else {
Damien George3558f622014-04-20 17:50:40 +01001022 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001023 }
1024}
1025
Damien George6be0b0a2014-08-15 14:30:52 +01001026STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001027 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001028 comp->have_star = true;
1029 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001030 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1031 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001032 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001033 } else {
1034 // named star
Damien429d7192013-10-04 19:53:11 +01001035 }
Damien George8b19db02014-04-11 23:25:34 +01001036 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001037
1038 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001039 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001040 // TODO do we need to do anything with this?
1041
1042 } else {
1043 mp_parse_node_t pn_id;
1044 mp_parse_node_t pn_colon;
1045 mp_parse_node_t pn_equal;
1046 if (MP_PARSE_NODE_IS_ID(pn)) {
1047 // this parameter is just an id
1048
1049 pn_id = pn;
1050 pn_colon = MP_PARSE_NODE_NULL;
1051 pn_equal = MP_PARSE_NODE_NULL;
1052
Damien George0bb97132015-02-27 14:25:47 +00001053 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001054 // this parameter has a colon and/or equal specifier
1055
Damien George0bb97132015-02-27 14:25:47 +00001056 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1057
Damien Georgef41fdd02014-03-03 23:19:11 +00001058 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1059 pn_id = pns->nodes[0];
1060 pn_colon = pns->nodes[1];
1061 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +00001062 }
1063
1064 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1065 // this parameter does not have a default value
1066
1067 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001068 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001069 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001070 return;
1071 }
1072
1073 } else {
1074 // this parameter has a default value
1075 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1076
Damien George8b19db02014-04-11 23:25:34 +01001077 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001078 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001079#if MICROPY_EMIT_CPYTHON
1080 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1081 compile_node(comp, pn_equal);
1082#else
Damien George69b89d22014-04-11 13:38:30 +00001083 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1084 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001085 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1086 // we need to do this here before we start building the map for the default keywords
1087 if (comp->num_default_params > 0) {
1088 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001089 } else {
1090 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001091 }
Damien George69b89d22014-04-11 13:38:30 +00001092 // first default dict param, so make the map
1093 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001094 }
Damien Georgef0778a72014-06-07 22:01:00 +01001095
1096 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001097 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001098 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001099 EMIT(store_map);
1100#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001101 } else {
Damien George69b89d22014-04-11 13:38:30 +00001102 comp->num_default_params += 1;
1103 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001104 }
1105 }
1106
1107 // TODO pn_colon not implemented
1108 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001109 }
1110}
1111
1112// leaves function object on stack
1113// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001114STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001115 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001116 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001117 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001118 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001119 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001120 }
1121
1122 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001123 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001124 uint old_num_dict_params = comp->num_dict_params;
1125 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001126
1127 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001128 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001129 comp->num_dict_params = 0;
1130 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001131 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001132
Damien Georgea91ac202014-10-05 19:01:34 +01001133 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001134 return MP_QSTR_NULL;
1135 }
1136
Damien Georgee337f1e2014-03-31 15:18:37 +01001137#if !MICROPY_EMIT_CPYTHON
1138 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001139 // the default keywords args may have already made the tuple; if not, do it now
1140 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001141 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001142 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001143 }
1144#endif
1145
Damien429d7192013-10-04 19:53:11 +01001146 // get the scope for this function
1147 scope_t *fscope = (scope_t*)pns->nodes[4];
1148
1149 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001150 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001151
1152 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001153 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001154 comp->num_dict_params = old_num_dict_params;
1155 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001156
1157 // return its name (the 'f' in "def f(...):")
1158 return fscope->simple_name;
1159}
1160
1161// leaves class object on stack
1162// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001163STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001164 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001165 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001166 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001167 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001168 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001169 }
1170
1171 EMIT(load_build_class);
1172
1173 // scope for this class
1174 scope_t *cscope = (scope_t*)pns->nodes[3];
1175
1176 // compile the class
1177 close_over_variables_etc(comp, cscope, 0, 0);
1178
1179 // get its name
Damien George968bf342014-04-27 19:12:05 +01001180 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001181
1182 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001183 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1184 mp_parse_node_t parents = pns->nodes[1];
1185 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1186 parents = MP_PARSE_NODE_NULL;
1187 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001188 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001189 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001190
1191 // return its name (the 'C' in class C(...):")
1192 return cscope->simple_name;
1193}
1194
Damien6cdd3af2013-10-05 18:08:26 +01001195// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001196STATIC 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 +00001197 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001198 return false;
1199 }
1200
1201 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001202 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001203 return true;
1204 }
1205
Damiend99b0522013-12-21 18:17:45 +00001206 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001207 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001208 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001209#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001210 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001211 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001212 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001213 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001214#endif
Damien3ef4abb2013-10-12 16:53:13 +01001215#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001216 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001217 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001218#endif
Damien6cdd3af2013-10-05 18:08:26 +01001219 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001220 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001221 }
1222
1223 return true;
1224}
1225
Damien George969a6b32014-12-10 22:07:04 +00001226STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001227 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001228 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001229 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001230
Damien6cdd3af2013-10-05 18:08:26 +01001231 // inherit emit options for this function/class definition
1232 uint emit_options = comp->scope_cur->emit_options;
1233
1234 // compile each decorator
1235 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001236 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001237 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1238 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001239
1240 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001241 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001242 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001243
1244 // check for built-in decorators
1245 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1246 // this was a built-in
1247 num_built_in_decorators += 1;
1248
1249 } else {
1250 // not a built-in, compile normally
1251
1252 // compile the decorator function
1253 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001254 for (int j = 1; j < name_len; j++) {
1255 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1256 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001257 }
1258
1259 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001260 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001261 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001262 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001263 compile_node(comp, pns_decorator->nodes[1]);
1264 }
Damien429d7192013-10-04 19:53:11 +01001265 }
1266 }
1267
1268 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001269 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001270 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001271 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001272 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001273 } else {
Damien George0bb97132015-02-27 14:25:47 +00001274 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1275 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001276 }
1277
1278 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001279 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001280 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001281 }
1282
1283 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +00001284 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +01001285}
1286
Damien George969a6b32014-12-10 22:07:04 +00001287STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001288 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001289 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +00001290 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +01001291}
1292
Damien George6be0b0a2014-08-15 14:30:52 +01001293STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001294 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00001295 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001296 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1297 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001298
1299 compile_node(comp, pns->nodes[0]); // base of the power node
1300
Damiend99b0522013-12-21 18:17:45 +00001301 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1302 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1303 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1304 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001305 for (int i = 0; i < n - 1; i++) {
1306 compile_node(comp, pns1->nodes[i]);
1307 }
Damiend99b0522013-12-21 18:17:45 +00001308 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1309 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001310 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001311 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001312 compile_node(comp, pns1->nodes[0]);
1313 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001314 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1315 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001316 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001317 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001318 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001319 }
1320 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001321 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001322 }
1323
Damiend99b0522013-12-21 18:17:45 +00001324 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001325 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001326 }
Damiend99b0522013-12-21 18:17:45 +00001327 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1328 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1329 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1330 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001331 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1332
Damiend99b0522013-12-21 18:17:45 +00001333 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1334 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1335 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001336 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001337 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001338 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001339 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001340 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001341 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001342 c_del_stmt(comp, pns->nodes[0]);
1343 for (int i = 0; i < n; i++) {
1344 c_del_stmt(comp, pns1->nodes[i]);
1345 }
Damiend99b0522013-12-21 18:17:45 +00001346 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001347 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001348 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001349 } else {
1350 // sequence with 2 items
1351 goto sequence_with_2_items;
1352 }
1353 } else {
1354 // sequence with 2 items
1355 sequence_with_2_items:
1356 c_del_stmt(comp, pns->nodes[0]);
1357 c_del_stmt(comp, pns->nodes[1]);
1358 }
1359 } else {
1360 // tuple with 1 element
1361 c_del_stmt(comp, pn);
1362 }
1363 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001364 // TODO is there anything else to implement?
1365 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001366 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001367
1368 return;
1369
1370cannot_delete:
1371 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001372}
1373
Damien George969a6b32014-12-10 22:07:04 +00001374STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001375 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1376}
1377
Damien George969a6b32014-12-10 22:07:04 +00001378STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001379 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001380 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001381 }
Damien George090c9232014-10-17 14:08:49 +00001382 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001383 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001384}
1385
Damien George969a6b32014-12-10 22:07:04 +00001386STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001387 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001388 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001389 }
Damien George090c9232014-10-17 14:08:49 +00001390 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001391 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001392}
1393
Damien George969a6b32014-12-10 22:07:04 +00001394STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001395 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001396 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001397 return;
1398 }
Damiend99b0522013-12-21 18:17:45 +00001399 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001400 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001401 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001402 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001403 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001404 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1405 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 +01001406
Damien George6f355fd2014-04-10 14:11:31 +01001407 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001408 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1409 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1410 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001411 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001412 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1413 } else {
1414 compile_node(comp, pns->nodes[0]);
1415 }
1416 EMIT(return_value);
1417}
1418
Damien George969a6b32014-12-10 22:07:04 +00001419STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001420 compile_node(comp, pns->nodes[0]);
1421 EMIT(pop_top);
1422}
1423
Damien George969a6b32014-12-10 22:07:04 +00001424STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001425 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001426 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001427 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001428 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001429 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001430 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001431 compile_node(comp, pns->nodes[0]);
1432 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001433 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001434 } else {
1435 // raise x
1436 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001437 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001438 }
1439}
1440
Damien George635543c2014-04-10 12:56:52 +01001441// q_base holds the base of the name
1442// eg a -> q_base=a
1443// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001444STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001445 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001446 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1447 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001448 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001449 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001450 pn = pns->nodes[0];
1451 is_as = true;
1452 }
Damien George635543c2014-04-10 12:56:52 +01001453 if (MP_PARSE_NODE_IS_NULL(pn)) {
1454 // empty name (eg, from . import x)
1455 *q_base = MP_QSTR_;
1456 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1457 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001458 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001459 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001460 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001461 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001462 }
Damien George635543c2014-04-10 12:56:52 +01001463 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001464 } else {
1465 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001466 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001467 {
Damien429d7192013-10-04 19:53:11 +01001468 // a name of the form a.b.c
1469 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001470 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001471 }
Damiend99b0522013-12-21 18:17:45 +00001472 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001473 int len = n - 1;
1474 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001475 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001476 }
Damien George55baff42014-01-21 21:40:13 +00001477 byte *q_ptr;
1478 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001479 for (int i = 0; i < n; i++) {
1480 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001481 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001482 }
Damien George39dc1452014-10-03 19:52:22 +01001483 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001484 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001485 memcpy(str_dest, str_src, str_src_len);
1486 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001487 }
Damien George635543c2014-04-10 12:56:52 +01001488 qstr q_full = qstr_build_end(q_ptr);
1489 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001490 if (is_as) {
1491 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001492 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001493 }
1494 }
Damien429d7192013-10-04 19:53:11 +01001495 }
Damien429d7192013-10-04 19:53:11 +01001496 }
1497}
1498
Damien George6be0b0a2014-08-15 14:30:52 +01001499STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001500 EMIT_ARG(load_const_small_int, 0); // level 0 import
1501 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1502 qstr q_base;
1503 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001504 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001505}
1506
Damien George969a6b32014-12-10 22:07:04 +00001507STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001508 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1509}
1510
Damien George969a6b32014-12-10 22:07:04 +00001511STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001512 mp_parse_node_t pn_import_source = pns->nodes[0];
1513
1514 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1515 uint import_level = 0;
1516 do {
1517 mp_parse_node_t pn_rel;
1518 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)) {
1519 // This covers relative imports with dots only like "from .. import"
1520 pn_rel = pn_import_source;
1521 pn_import_source = MP_PARSE_NODE_NULL;
1522 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1523 // This covers relative imports starting with dot(s) like "from .foo import"
1524 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1525 pn_rel = pns_2b->nodes[0];
1526 pn_import_source = pns_2b->nodes[1];
1527 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1528 } else {
1529 // Not a relative import
1530 break;
1531 }
1532
1533 // get the list of . and/or ...'s
1534 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001535 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001536
1537 // count the total number of .'s
1538 for (int i = 0; i < n; i++) {
1539 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1540 import_level++;
1541 } else {
1542 // should be an MP_TOKEN_ELLIPSIS
1543 import_level += 3;
1544 }
1545 }
1546 } while (0);
1547
Damiend99b0522013-12-21 18:17:45 +00001548 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001549 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001550
1551 // build the "fromlist" tuple
1552#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001553 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001554#else
Damien George708c0732014-04-27 19:23:46 +01001555 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001556 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001557#endif
1558
1559 // do the import
Damien George635543c2014-04-10 12:56:52 +01001560 qstr dummy_q;
1561 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001562 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001563
Damien429d7192013-10-04 19:53:11 +01001564 } else {
Damien George635543c2014-04-10 12:56:52 +01001565 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001566
1567 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001568 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001569 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001570#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001571 {
1572 vstr_t *vstr = vstr_new();
1573 vstr_printf(vstr, "(");
1574 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001575 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1576 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1577 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001578 if (i > 0) {
1579 vstr_printf(vstr, ", ");
1580 }
1581 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001582 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001583 const byte *str = qstr_data(id2, &len);
1584 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001585 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001586 }
Damien02f89412013-12-12 15:13:36 +00001587 if (n == 1) {
1588 vstr_printf(vstr, ",");
1589 }
1590 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001591 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001592 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001593 }
Damiendb4c3612013-12-10 17:27:24 +00001594#else
1595 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001596 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1597 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1598 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001599 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001600 }
Damien Georgeb9791222014-01-23 00:34:21 +00001601 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001602#endif
1603
1604 // do the import
Damien George635543c2014-04-10 12:56:52 +01001605 qstr dummy_q;
1606 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001607 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001608 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1609 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1610 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001611 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001612 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001613 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001614 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001615 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001616 }
1617 }
1618 EMIT(pop_top);
1619 }
1620}
1621
Damien George584ba672014-12-21 17:26:45 +00001622STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1623 bool added;
1624 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1625 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001626 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001627 compile_syntax_error(comp, pn, "identifier already used");
1628 return;
1629 }
1630 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1631
1632 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1633 id_info = scope_find_global(comp->scope_cur, qst);
1634 if (id_info != NULL) {
1635 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1636 }
1637}
1638
Damien George969a6b32014-12-10 22:07:04 +00001639STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001640 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001641 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001642 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001643 for (int i = 0; i < n; i++) {
1644 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001645 }
1646 }
1647}
1648
Damien George584ba672014-12-21 17:26:45 +00001649STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1650 bool added;
1651 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1652 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001653 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001654 compile_syntax_error(comp, pn, "identifier already used");
1655 return;
1656 }
1657 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1658 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)) {
1659 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1660 return;
1661 }
1662 id_info->kind = ID_INFO_KIND_FREE;
1663 scope_close_over_in_parents(comp->scope_cur, qst);
1664}
1665
Damien George969a6b32014-12-10 22:07:04 +00001666STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001667 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001668 if (comp->scope_cur->kind == SCOPE_MODULE) {
1669 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1670 return;
1671 }
1672 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001673 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001674 for (int i = 0; i < n; i++) {
1675 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001676 }
1677 }
1678}
1679
Damien George969a6b32014-12-10 22:07:04 +00001680STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001681 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001682 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001683 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001684 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001685 // assertion message
1686 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001687 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001688 }
Damien Georgeb9791222014-01-23 00:34:21 +00001689 EMIT_ARG(raise_varargs, 1);
1690 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001691}
1692
Damien George969a6b32014-12-10 22:07:04 +00001693STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001694 // TODO proper and/or short circuiting
1695
Damien George6f355fd2014-04-10 14:11:31 +01001696 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001697
Damien George391db862014-10-17 17:57:33 +00001698 // optimisation: don't emit anything when "if False" (not in CPython)
1699 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1700 uint l_fail = comp_next_label(comp);
1701 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001702
Damien George391db862014-10-17 17:57:33 +00001703 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001704
Damien George391db862014-10-17 17:57:33 +00001705 // optimisation: skip everything else when "if True" (not in CPython)
1706 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1707 goto done;
1708 }
1709
1710 if (
1711 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1712 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1713 // optimisation: don't jump if last instruction was return
1714 && !EMIT(last_emit_was_return_value)
1715 ) {
1716 // jump over elif/else blocks
1717 EMIT_ARG(jump, l_end);
1718 }
1719
1720 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001721 }
1722
Damien George235f9b32014-10-17 17:30:16 +00001723 // compile elif blocks (if any)
1724 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001725 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 +00001726 for (int i = 0; i < n_elif; i++) {
1727 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1728 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001729
Damien George391db862014-10-17 17:57:33 +00001730 // optimisation: don't emit anything when "if False" (not in CPython)
1731 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1732 uint l_fail = comp_next_label(comp);
1733 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001734
Damien George391db862014-10-17 17:57:33 +00001735 compile_node(comp, pns_elif->nodes[1]); // elif block
1736
1737 // optimisation: skip everything else when "elif True" (not in CPython)
1738 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1739 goto done;
1740 }
1741
1742 // optimisation: don't jump if last instruction was return
1743 if (!EMIT(last_emit_was_return_value)) {
1744 EMIT_ARG(jump, l_end);
1745 }
1746 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001747 }
1748 }
1749
1750 // compile else block
1751 compile_node(comp, pns->nodes[3]); // can be null
1752
Damien George391db862014-10-17 17:57:33 +00001753done:
Damien Georgeb9791222014-01-23 00:34:21 +00001754 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001755}
1756
Damien Georgecbddb272014-02-01 20:08:18 +00001757#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001758 uint16_t old_break_label = comp->break_label; \
1759 uint16_t old_continue_label = comp->continue_label; \
1760 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001761 uint break_label = comp_next_label(comp); \
1762 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001763 comp->break_label = break_label; \
1764 comp->continue_label = continue_label; \
1765 comp->break_continue_except_level = comp->cur_except_level;
1766
1767#define END_BREAK_CONTINUE_BLOCK \
1768 comp->break_label = old_break_label; \
1769 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001770 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001771
Damien George969a6b32014-12-10 22:07:04 +00001772STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001773 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001774
Damience89a212013-10-15 22:25:17 +01001775 // compared to CPython, we have an optimised version of while loops
1776#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001777 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001778 EMIT_ARG(setup_loop, break_label);
1779 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001780 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1781 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001782 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001783 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001784 }
Damien Georgeb9791222014-01-23 00:34:21 +00001785 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001786 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1787 // this is a small hack to agree with CPython
1788 if (!node_is_const_true(pns->nodes[0])) {
1789 EMIT(pop_block);
1790 }
Damience89a212013-10-15 22:25:17 +01001791#else
Damien George391db862014-10-17 17:57:33 +00001792 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1793 uint top_label = comp_next_label(comp);
1794 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1795 EMIT_ARG(jump, continue_label);
1796 }
1797 EMIT_ARG(label_assign, top_label);
1798 compile_node(comp, pns->nodes[1]); // body
1799 EMIT_ARG(label_assign, continue_label);
1800 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1801 }
Damience89a212013-10-15 22:25:17 +01001802#endif
1803
1804 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001805 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001806
1807 compile_node(comp, pns->nodes[2]); // else
1808
Damien Georgeb9791222014-01-23 00:34:21 +00001809 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001810}
1811
Damien George2ac4af62014-08-15 16:45:41 +01001812#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001813// This function compiles an optimised for-loop of the form:
1814// for <var> in range(<start>, <end>, <step>):
1815// <body>
1816// else:
1817// <else>
1818// <var> must be an identifier and <step> must be a small-int.
1819//
1820// Semantics of for-loop require:
1821// - final failing value should not be stored in the loop variable
1822// - if the loop never runs, the loop variable should never be assigned
1823// - assignments to <var>, <end> or <step> in the body do not alter the loop
1824// (<step> is a constant for us, so no need to worry about it changing)
1825//
1826// If <end> is a small-int, then the stack during the for-loop contains just
1827// the current value of <var>. Otherwise, the stack contains <end> then the
1828// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001829STATIC 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 +00001830 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001831
Damien George6f355fd2014-04-10 14:11:31 +01001832 uint top_label = comp_next_label(comp);
1833 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001834
Damien Georgee181c0d2014-12-12 17:19:56 +00001835 // put the end value on the stack if it's not a small-int constant
1836 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1837 if (end_on_stack) {
1838 compile_node(comp, pn_end);
1839 }
1840
1841 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001842 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001843
Damien Georgeb9791222014-01-23 00:34:21 +00001844 EMIT_ARG(jump, entry_label);
1845 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001846
Damien Georgec33ce602014-12-11 17:35:23 +00001847 // duplicate next value and store it to var
1848 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001849 c_assign(comp, pn_var, ASSIGN_STORE);
1850
Damienf3822fc2013-11-09 20:12:03 +00001851 // compile body
1852 compile_node(comp, pn_body);
1853
Damien Georgeb9791222014-01-23 00:34:21 +00001854 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001855
Damien Georgee181c0d2014-12-12 17:19:56 +00001856 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001857 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001858 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001859
Damien Georgeb9791222014-01-23 00:34:21 +00001860 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001861
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001862 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001863 if (end_on_stack) {
1864 EMIT(dup_top_two);
1865 EMIT(rot_two);
1866 } else {
1867 EMIT(dup_top);
1868 compile_node(comp, pn_end);
1869 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001870 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1871 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001872 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001873 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001874 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001875 }
Damien George63f38322015-02-28 15:04:06 +00001876 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001877
1878 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001879 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001880
1881 compile_node(comp, pn_else);
1882
Damien Georgeb9791222014-01-23 00:34:21 +00001883 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001884
1885 // discard final value of var that failed the loop condition
1886 EMIT(pop_top);
1887
1888 // discard <end> value if it's on the stack
1889 if (end_on_stack) {
1890 EMIT(pop_top);
1891 }
Damienf72fd0e2013-11-06 20:20:49 +00001892}
Damien George2ac4af62014-08-15 16:45:41 +01001893#endif
Damienf72fd0e2013-11-06 20:20:49 +00001894
Damien George969a6b32014-12-10 22:07:04 +00001895STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001896#if !MICROPY_EMIT_CPYTHON
1897 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1898 // this is actually slower, but uses no heap memory
1899 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001900 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 +00001901 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001902 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1903 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1904 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1905 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001906 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1907 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001908 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001909 mp_parse_node_t pn_range_start;
1910 mp_parse_node_t pn_range_end;
1911 mp_parse_node_t pn_range_step;
1912 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001913 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001914 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001915 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001916 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001917 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001918 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001919 } else if (n_args == 2) {
1920 pn_range_start = args[0];
1921 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001922 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001923 } else {
1924 pn_range_start = args[0];
1925 pn_range_end = args[1];
1926 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001927 // We need to know sign of step. This is possible only if it's constant
1928 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1929 optimize = false;
1930 }
Damienf72fd0e2013-11-06 20:20:49 +00001931 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001932 }
1933 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001934 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1935 return;
1936 }
1937 }
1938 }
1939#endif
1940
Damien Georgecbddb272014-02-01 20:08:18 +00001941 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001942 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001943
Damien George6f355fd2014-04-10 14:11:31 +01001944 uint pop_label = comp_next_label(comp);
1945 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001946
Damience89a212013-10-15 22:25:17 +01001947 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1948#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001949 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001950#endif
1951
Damien429d7192013-10-04 19:53:11 +01001952 compile_node(comp, pns->nodes[1]); // iterator
1953 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001954 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001955 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001956 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1957 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001958 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001959 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001960 }
Damien Georgeb9791222014-01-23 00:34:21 +00001961 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001962 EMIT(for_iter_end);
1963
1964 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001965 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001966
Damience89a212013-10-15 22:25:17 +01001967#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001968 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001969#endif
Damien429d7192013-10-04 19:53:11 +01001970
1971 compile_node(comp, pns->nodes[3]); // else (not tested)
1972
Damien Georgeb9791222014-01-23 00:34:21 +00001973 EMIT_ARG(label_assign, break_label);
1974 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001975}
1976
Damien George6be0b0a2014-08-15 14:30:52 +01001977STATIC 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 +01001978 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001979 uint l1 = comp_next_label(comp);
1980 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001981
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001983 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001984
Damien429d7192013-10-04 19:53:11 +01001985 compile_node(comp, pn_body); // body
1986 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001987 EMIT_ARG(jump, success_label); // jump over exception handler
1988
1989 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001990 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001991
Damien George6f355fd2014-04-10 14:11:31 +01001992 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001993
1994 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001995 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1996 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001997
1998 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001999 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002000
Damiend99b0522013-12-21 18:17:45 +00002001 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002002 // this is a catch all exception handler
2003 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002004 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00002005 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002006 return;
2007 }
2008 } else {
2009 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00002010 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
2011 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
2012 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
2013 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01002014 // handler binds the exception to a local
2015 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002016 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002017 }
2018 }
2019 EMIT(dup_top);
2020 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002021 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00002022 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002023 }
2024
2025 EMIT(pop_top);
2026
2027 if (qstr_exception_local == 0) {
2028 EMIT(pop_top);
2029 } else {
Damien George542bd6b2015-03-26 14:42:40 +00002030 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002031 }
2032
2033 EMIT(pop_top);
2034
Damien George6f355fd2014-04-10 14:11:31 +01002035 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002036 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002037 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002038 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002039 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002040 }
2041 compile_node(comp, pns_except->nodes[1]);
2042 if (qstr_exception_local != 0) {
2043 EMIT(pop_block);
2044 }
2045 EMIT(pop_except);
2046 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002047 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2048 EMIT_ARG(label_assign, l3);
2049 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00002050 compile_store_id(comp, qstr_exception_local);
2051 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002052
Damien George8dcc0c72014-03-27 10:55:21 +00002053 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002054 EMIT(end_finally);
2055 }
Damien Georgeb9791222014-01-23 00:34:21 +00002056 EMIT_ARG(jump, l2);
2057 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002058 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002059 }
2060
Damien George8dcc0c72014-03-27 10:55:21 +00002061 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002062 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002063 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002064
Damien Georgeb9791222014-01-23 00:34:21 +00002065 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002066 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002067 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002068}
2069
Damien George6be0b0a2014-08-15 14:30:52 +01002070STATIC 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 +01002071 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002072
Damien Georgeb9791222014-01-23 00:34:21 +00002073 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002074 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002075
Damien429d7192013-10-04 19:53:11 +01002076 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002077 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002078 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002079 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002080 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002081 } else {
2082 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2083 }
2084 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002085 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2086 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002087 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002088
Damien George8dcc0c72014-03-27 10:55:21 +00002089 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002090 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002091}
2092
Damien George969a6b32014-12-10 22:07:04 +00002093STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00002094 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2095 {
Damiend99b0522013-12-21 18:17:45 +00002096 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2097 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002098 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002099 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2100 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002101 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002102 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002103 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002104 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002105 // no finally
2106 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2107 } else {
2108 // have finally
Damiend99b0522013-12-21 18:17:45 +00002109 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 +01002110 }
2111 } else {
2112 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002113 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002114 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002115 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002116 }
Damien429d7192013-10-04 19:53:11 +01002117 }
2118}
2119
Damien George6be0b0a2014-08-15 14:30:52 +01002120STATIC 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 +01002121 if (n == 0) {
2122 // no more pre-bits, compile the body of the with
2123 compile_node(comp, body);
2124 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002125 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002126 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002127 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002128 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002129 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002130 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002131 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2132 } else {
2133 // this pre-bit is just an expression
2134 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002135 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002136 EMIT(pop_top);
2137 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002138 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002139 // compile additional pre-bits and the body
2140 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2141 // finish this with block
2142 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002143 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2144 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002145 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002146 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002147 EMIT(end_finally);
2148 }
2149}
2150
Damien George969a6b32014-12-10 22:07:04 +00002151STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002152 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002153 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002154 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002155 assert(n > 0);
2156
2157 // compile in a nested fashion
2158 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2159}
2160
Damien George969a6b32014-12-10 22:07:04 +00002161STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002162 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002163 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2164 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00002165 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002166 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002167 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002168 EMIT(pop_top);
2169
Damien429d7192013-10-04 19:53:11 +01002170 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002171 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002172 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002173 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002174 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2175 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002176 // do nothing with a lonely constant
2177 } else {
2178 compile_node(comp, pns->nodes[0]); // just an expression
2179 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2180 }
Damien429d7192013-10-04 19:53:11 +01002181 }
2182 } else {
Damiend99b0522013-12-21 18:17:45 +00002183 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2184 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002185 if (kind == PN_expr_stmt_augassign) {
2186 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2187 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002188 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002189 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002190 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002191 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2192 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2193 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2194 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2195 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2196 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2197 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2198 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2199 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2200 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2201 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002202 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002203 }
Damien George7e5fb242014-02-01 22:18:47 +00002204 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002205 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2206 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002207 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2208 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002209 // following CPython, we store left-most first
2210 if (rhs > 0) {
2211 EMIT(dup_top);
2212 }
2213 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2214 for (int i = 0; i < rhs; i++) {
2215 if (i + 1 < rhs) {
2216 EMIT(dup_top);
2217 }
Damiend99b0522013-12-21 18:17:45 +00002218 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002219 }
Damien George0bb97132015-02-27 14:25:47 +00002220 } else {
2221 assert(kind == PN_expr_stmt_assign); // should be
Damien George42e0c592015-03-14 13:11:35 +00002222 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
2223 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002224 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2225 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2226 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002227 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002228 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2229 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002230 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2231 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2232 // can't optimise when it's a star expression on the lhs
2233 goto no_optimisation;
2234 }
Damien429d7192013-10-04 19:53:11 +01002235 compile_node(comp, pns10->nodes[0]); // rhs
2236 compile_node(comp, pns10->nodes[1]); // rhs
2237 EMIT(rot_two);
2238 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2239 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00002240 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2241 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002242 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2243 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2244 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002245 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002246 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2247 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002248 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2249 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2250 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2251 // can't optimise when it's a star expression on the lhs
2252 goto no_optimisation;
2253 }
Damien429d7192013-10-04 19:53:11 +01002254 compile_node(comp, pns10->nodes[0]); // rhs
2255 compile_node(comp, pns10->nodes[1]); // rhs
2256 compile_node(comp, pns10->nodes[2]); // rhs
2257 EMIT(rot_three);
2258 EMIT(rot_two);
2259 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2260 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2261 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2262 } else {
Damien George495d7812014-04-08 17:51:47 +01002263 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002264 compile_node(comp, pns1->nodes[0]); // rhs
2265 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2266 }
Damien429d7192013-10-04 19:53:11 +01002267 }
2268 }
2269}
2270
Damien George6be0b0a2014-08-15 14:30:52 +01002271STATIC 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 +00002272 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002273 compile_node(comp, pns->nodes[0]);
2274 for (int i = 1; i < num_nodes; i += 1) {
2275 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002276 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002277 }
2278}
2279
Damien George969a6b32014-12-10 22:07:04 +00002280STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002281 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2282 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002283
Damien George6f355fd2014-04-10 14:11:31 +01002284 uint l_fail = comp_next_label(comp);
2285 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002286 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2287 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002288 EMIT_ARG(jump, l_end);
2289 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002290 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002291 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002292 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002293}
2294
Damien George969a6b32014-12-10 22:07:04 +00002295STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002296 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002297 //mp_parse_node_t pn_params = pns->nodes[0];
2298 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002299
Damien George36db6bc2014-05-07 17:24:22 +01002300 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002301 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002302 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 +01002303 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002304 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002305 }
2306
2307 // get the scope for this lambda
2308 scope_t *this_scope = (scope_t*)pns->nodes[2];
2309
2310 // make the lambda
2311 close_over_variables_etc(comp, this_scope, 0, 0);
2312}
2313
Damien George7711afb2015-02-28 15:10:18 +00002314STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002315 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002316 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002317 for (int i = 0; i < n; i += 1) {
2318 compile_node(comp, pns->nodes[i]);
2319 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002320 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002321 }
2322 }
Damien Georgeb9791222014-01-23 00:34:21 +00002323 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002324}
2325
Damien George7711afb2015-02-28 15:10:18 +00002326STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2327 compile_or_and_test(comp, pns, true);
2328}
2329
Damien George969a6b32014-12-10 22:07:04 +00002330STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002331 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002332}
2333
Damien George969a6b32014-12-10 22:07:04 +00002334STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002335 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002336 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002337}
2338
Damien George969a6b32014-12-10 22:07:04 +00002339STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002340 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002341 compile_node(comp, pns->nodes[0]);
2342 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002343 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002344 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002345 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002346 }
2347 for (int i = 1; i + 1 < num_nodes; i += 2) {
2348 compile_node(comp, pns->nodes[i + 1]);
2349 if (i + 2 < num_nodes) {
2350 EMIT(dup_top);
2351 EMIT(rot_three);
2352 }
Damien George7e5fb242014-02-01 22:18:47 +00002353 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002354 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002355 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002356 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2357 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2358 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2359 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2360 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2361 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002362 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002363 }
2364 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002365 } else {
2366 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002367 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2368 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002369 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002370 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002371 } else {
2372 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002373 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002374 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002375 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002376 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002377 }
Damien429d7192013-10-04 19:53:11 +01002378 }
Damien429d7192013-10-04 19:53:11 +01002379 }
2380 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002381 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002382 }
2383 }
2384 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002385 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002386 EMIT_ARG(jump, l_end);
2387 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002388 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002389 EMIT(rot_two);
2390 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002391 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002392 }
2393}
2394
Damien George969a6b32014-12-10 22:07:04 +00002395STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002396 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002397}
2398
Damien George969a6b32014-12-10 22:07:04 +00002399STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002400 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002401}
2402
Damien George969a6b32014-12-10 22:07:04 +00002403STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002404 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002405}
2406
Damien George969a6b32014-12-10 22:07:04 +00002407STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002408 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002409}
2410
Damien George969a6b32014-12-10 22:07:04 +00002411STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002412 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002413 compile_node(comp, pns->nodes[0]);
2414 for (int i = 1; i + 1 < num_nodes; i += 2) {
2415 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002416 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002417 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002418 } else {
Damien George0bb97132015-02-27 14:25:47 +00002419 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2420 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002421 }
2422 }
2423}
2424
Damien George969a6b32014-12-10 22:07:04 +00002425STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002426 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002427 compile_node(comp, pns->nodes[0]);
2428 for (int i = 1; i + 1 < num_nodes; i += 2) {
2429 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002430 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002431 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002432 } else {
Damien George0bb97132015-02-27 14:25:47 +00002433 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2434 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002435 }
2436 }
2437}
2438
Damien George969a6b32014-12-10 22:07:04 +00002439STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002440 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002441 compile_node(comp, pns->nodes[0]);
2442 for (int i = 1; i + 1 < num_nodes; i += 2) {
2443 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002444 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002445 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002446 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002447 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002448 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002449 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002450 } else {
Damien George0bb97132015-02-27 14:25:47 +00002451 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2452 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002453 }
2454 }
2455}
2456
Damien George969a6b32014-12-10 22:07:04 +00002457STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002458 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002459 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002460 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002461 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002462 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002463 } else {
Damien George0bb97132015-02-27 14:25:47 +00002464 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2465 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002466 }
2467}
2468
Damien George969a6b32014-12-10 22:07:04 +00002469STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002470 // this is to handle special super() call
2471 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2472
2473 compile_generic_all_nodes(comp, pns);
2474}
2475
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002476STATIC 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 +01002477 // function to call is on top of stack
2478
Damien George35e2a4e2014-02-05 00:51:47 +00002479#if !MICROPY_EMIT_CPYTHON
2480 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002481 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George542bd6b2015-03-26 14:42:40 +00002482 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002483 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002484 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002485 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002486 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002487 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002488 EMIT_ARG(call_function, 2, 0, 0);
2489 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002490 }
2491 }
Damien George01039b52014-12-21 17:44:27 +00002492 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002493 return;
2494 }
2495#endif
2496
Damien George2c0842b2014-04-27 16:46:51 +01002497 // get the list of arguments
2498 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002499 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002500
Damien George2c0842b2014-04-27 16:46:51 +01002501 // compile the arguments
2502 // Rather than calling compile_node on the list, we go through the list of args
2503 // explicitly here so that we can count the number of arguments and give sensible
2504 // error messages.
2505 int n_positional = n_positional_extra;
2506 uint n_keyword = 0;
2507 uint star_flags = 0;
2508 for (int i = 0; i < n_args; i++) {
2509 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2510 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2511 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2512 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2513 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2514 return;
2515 }
2516 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2517 compile_node(comp, pns_arg->nodes[0]);
2518 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2519 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2520 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2521 return;
2522 }
2523 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2524 compile_node(comp, pns_arg->nodes[0]);
2525 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2526 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2527 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2528 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2529 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2530 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2531 return;
2532 }
Damien George968bf342014-04-27 19:12:05 +01002533 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002534 compile_node(comp, pns2->nodes[0]);
2535 n_keyword += 1;
2536 } else {
2537 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2538 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2539 n_positional++;
2540 }
2541 } else {
2542 goto normal_argument;
2543 }
2544 } else {
2545 normal_argument:
2546 if (n_keyword > 0) {
2547 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2548 return;
2549 }
2550 compile_node(comp, args[i]);
2551 n_positional++;
2552 }
Damien429d7192013-10-04 19:53:11 +01002553 }
2554
Damien George2c0842b2014-04-27 16:46:51 +01002555 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002556 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002557 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002558 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002559 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002560 }
Damien429d7192013-10-04 19:53:11 +01002561}
2562
Damien George969a6b32014-12-10 22:07:04 +00002563STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002564 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002565 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002566 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 +01002567 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002568 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2569 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002570 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002571 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002572 i += 1;
2573 } else {
2574 compile_node(comp, pns->nodes[i]);
2575 }
Damien George35e2a4e2014-02-05 00:51:47 +00002576 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002577 }
2578}
2579
Damien George969a6b32014-12-10 22:07:04 +00002580STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002581 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002582 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002583}
2584
Damien George969a6b32014-12-10 22:07:04 +00002585STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002586 // a list of strings
Damien63321742013-12-10 17:41:49 +00002587
2588 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002589 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002590 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002591 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002592 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002593 int pn_kind;
2594 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2595 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2596 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2597 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2598 } else {
2599 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2600 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002601 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2602 pn_kind = MP_PARSE_NODE_STRING;
2603 } else {
2604 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2605 pn_kind = MP_PARSE_NODE_BYTES;
2606 }
Damien George40f3c022014-07-03 13:25:24 +01002607 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002608 }
Damien63321742013-12-10 17:41:49 +00002609 if (i == 0) {
2610 string_kind = pn_kind;
2611 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002612 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002613 return;
2614 }
Damien429d7192013-10-04 19:53:11 +01002615 }
Damien63321742013-12-10 17:41:49 +00002616
Damien George65ef6b72015-01-14 21:17:27 +00002617 // if we are not in the last pass, just load a dummy object
2618 if (comp->pass != MP_PASS_EMIT) {
2619 EMIT_ARG(load_const_obj, mp_const_none);
2620 return;
2621 }
2622
Damien63321742013-12-10 17:41:49 +00002623 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002624 vstr_t vstr;
2625 vstr_init_len(&vstr, n_bytes);
2626 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002627 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002628 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002629 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002630 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2631 memcpy(s_dest, s, s_len);
2632 s_dest += s_len;
2633 } else {
2634 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002635 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2636 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002637 }
Damien63321742013-12-10 17:41:49 +00002638 }
Damien George65ef6b72015-01-14 21:17:27 +00002639
2640 // load the object
Damien George05005f62015-01-21 22:48:37 +00002641 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 +01002642}
2643
2644// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002645STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002646 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2647 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2648 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002649
Damien George36db6bc2014-05-07 17:24:22 +01002650 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002651 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002652 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 +01002653 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002654 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002655 }
2656
2657 // get the scope for this comprehension
2658 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2659
2660 // compile the comprehension
2661 close_over_variables_etc(comp, this_scope, 0, 0);
2662
2663 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2664 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002665 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002666}
2667
Damien George969a6b32014-12-10 22:07:04 +00002668STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002669 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002670 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002671 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2672 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2673 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2674 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2675 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2676 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2677 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002678 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002679 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002680 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002681 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002682 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002683 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002684 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002685 // generator expression
2686 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2687 } else {
2688 // tuple with 2 items
2689 goto tuple_with_2_items;
2690 }
2691 } else {
2692 // tuple with 2 items
2693 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002694 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002695 }
2696 } else {
2697 // parenthesis around a single item, is just that item
2698 compile_node(comp, pns->nodes[0]);
2699 }
2700}
2701
Damien George969a6b32014-12-10 22:07:04 +00002702STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002703 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002704 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002705 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002706 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2707 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2708 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2709 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2710 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002711 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002712 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002713 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002714 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002715 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002716 // list of many items
2717 compile_node(comp, pns2->nodes[0]);
2718 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002719 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002720 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002721 // list comprehension
2722 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2723 } else {
2724 // list with 2 items
2725 goto list_with_2_items;
2726 }
2727 } else {
2728 // list with 2 items
2729 list_with_2_items:
2730 compile_node(comp, pns2->nodes[0]);
2731 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002732 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002733 }
2734 } else {
2735 // list with 1 item
2736 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002737 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002738 }
2739}
2740
Damien George969a6b32014-12-10 22:07:04 +00002741STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002742 mp_parse_node_t pn = pns->nodes[0];
2743 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002744 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002745 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002746 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2747 pns = (mp_parse_node_struct_t*)pn;
2748 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002749 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002750 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002751 compile_node(comp, pn);
2752 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002753 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2754 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2755 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2756 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002757 // dict/set with multiple elements
2758
2759 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002760 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002761 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002762
2763 // first element sets whether it's a dict or set
2764 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002765 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002766 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002767 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002768 compile_node(comp, pns->nodes[0]);
2769 EMIT(store_map);
2770 is_dict = true;
2771 } else {
2772 // a set
2773 compile_node(comp, pns->nodes[0]); // 1st value of set
2774 is_dict = false;
2775 }
2776
2777 // process rest of elements
2778 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002779 mp_parse_node_t pn_i = nodes[i];
2780 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2781 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002782 if (is_dict) {
2783 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002784 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002785 return;
2786 }
2787 EMIT(store_map);
2788 } else {
2789 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002790 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002791 return;
2792 }
2793 }
2794 }
2795
Damien Georgee37dcaa2014-12-27 17:07:16 +00002796 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002797 // if it's a set, build it
2798 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002799 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002800 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002801 #endif
Damien George0bb97132015-02-27 14:25:47 +00002802 } else {
2803 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002804 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002805 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002806 // a dictionary comprehension
2807 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2808 } else {
2809 // a set comprehension
2810 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2811 }
Damien429d7192013-10-04 19:53:11 +01002812 }
2813 } else {
2814 // set with one element
2815 goto set_with_one_element;
2816 }
2817 } else {
2818 // set with one element
2819 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002820 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002821 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002822 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002823 #else
2824 assert(0);
2825 #endif
Damien429d7192013-10-04 19:53:11 +01002826 }
2827}
2828
Damien George969a6b32014-12-10 22:07:04 +00002829STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002830 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002831}
2832
Damien George969a6b32014-12-10 22:07:04 +00002833STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002834 // object who's index we want is on top of stack
2835 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002836 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002837}
2838
Damien George969a6b32014-12-10 22:07:04 +00002839STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002840 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002841 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002842}
2843
Damien George83204f32014-12-27 17:20:41 +00002844#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002845STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002846 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2847 mp_parse_node_t pn = pns->nodes[0];
2848 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002849 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002850 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2851 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002852 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2853 pns = (mp_parse_node_struct_t*)pn;
2854 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002855 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002856 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002857 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002858 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002859 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002860 } else {
2861 // [?::x]
2862 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002863 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002864 }
Damiend99b0522013-12-21 18:17:45 +00002865 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002866 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002867 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2868 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2869 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2870 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002871 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002872 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002873 } else {
2874 // [?:x:x]
2875 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002876 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002877 }
2878 } else {
2879 // [?:x]
2880 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002881 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002882 }
2883 } else {
2884 // [?:x]
2885 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002886 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002887 }
2888}
2889
Damien George969a6b32014-12-10 22:07:04 +00002890STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002891 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002892 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2893 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002894}
2895
Damien George969a6b32014-12-10 22:07:04 +00002896STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002897 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002898 compile_subscript_3_helper(comp, pns);
2899}
Damien George83204f32014-12-27 17:20:41 +00002900#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002901
Damien George969a6b32014-12-10 22:07:04 +00002902STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002903 // if this is called then we are compiling a dict key:value pair
2904 compile_node(comp, pns->nodes[1]); // value
2905 compile_node(comp, pns->nodes[0]); // key
2906}
2907
Damien George969a6b32014-12-10 22:07:04 +00002908STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002909 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002910 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002911 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002912}
2913
Damien George969a6b32014-12-10 22:07:04 +00002914STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002915 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002916 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002917 return;
2918 }
Damiend99b0522013-12-21 18:17:45 +00002919 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002920 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002921 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002922 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2923 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002924 compile_node(comp, pns->nodes[0]);
2925 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002926 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002927 EMIT(yield_from);
2928 } else {
2929 compile_node(comp, pns->nodes[0]);
2930 EMIT(yield_value);
2931 }
2932}
2933
Damien George65ef6b72015-01-14 21:17:27 +00002934STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2935 // only create and load the actual str object on the last pass
2936 if (comp->pass != MP_PASS_EMIT) {
2937 EMIT_ARG(load_const_obj, mp_const_none);
2938 } else {
2939 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2940 }
2941}
2942
2943STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2944 // only create and load the actual bytes object on the last pass
2945 if (comp->pass != MP_PASS_EMIT) {
2946 EMIT_ARG(load_const_obj, mp_const_none);
2947 } else {
2948 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2949 }
2950}
2951
Damien George7d414a12015-02-08 01:57:40 +00002952STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2953 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2954}
2955
Damiend99b0522013-12-21 18:17:45 +00002956typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002957STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002958#define nc NULL
2959#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002960#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002961#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002962#undef nc
2963#undef c
2964#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002965 NULL,
2966 compile_string,
2967 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002968 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002969};
2970
Damien George6be0b0a2014-08-15 14:30:52 +01002971STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002972 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002973 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002974 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002975 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002976 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002977 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002978 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002979 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002980 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002981 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2982 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damien Georged2d64f02015-01-14 21:32:42 +00002983 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002984 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002985 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002986 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002987 // do nothing
2988 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002989 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002990 }
2991 break;
Damien429d7192013-10-04 19:53:11 +01002992 }
2993 } else {
Damiend99b0522013-12-21 18:17:45 +00002994 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002995 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002996 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2997 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002998#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002999 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
3000 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01003001#endif
Damien George65ef6b72015-01-14 21:17:27 +00003002 compile_syntax_error(comp, pn, "internal compiler error");
3003 } else {
3004 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01003005 }
3006 }
3007}
3008
Damien George2ac4af62014-08-15 16:45:41 +01003009STATIC 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 +01003010 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01003011 qstr param_name = MP_QSTR_NULL;
3012 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003013 if (MP_PARSE_NODE_IS_ID(pn)) {
3014 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01003015 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003016 // comes after a star, so counts as a keyword-only parameter
3017 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01003018 } else {
Damien George2827d622014-04-27 15:50:52 +01003019 // comes before a star, so counts as a positional parameter
3020 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003021 }
Damienb14de212013-10-06 00:28:28 +01003022 } else {
Damiend99b0522013-12-21 18:17:45 +00003023 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3024 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3025 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3026 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003027 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003028 // comes after a star, so counts as a keyword-only parameter
3029 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003030 } else {
Damien George2827d622014-04-27 15:50:52 +01003031 // comes before a star, so counts as a positional parameter
3032 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003033 }
Damiend99b0522013-12-21 18:17:45 +00003034 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003035 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003036 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003037 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003038 // bare star
3039 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003040 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003041 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003042 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003043 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003044 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00003045 } else {
3046 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01003047 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003048 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003049 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3050 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003051 }
Damien George0bb97132015-02-27 14:25:47 +00003052 } else {
3053 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00003054 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003055 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003056 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003057 }
Damien429d7192013-10-04 19:53:11 +01003058 }
3059
Damien George2827d622014-04-27 15:50:52 +01003060 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003061 bool added;
3062 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3063 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003064 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003065 return;
3066 }
Damien429d7192013-10-04 19:53:11 +01003067 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003068 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003069 }
3070}
3071
Damien George2827d622014-04-27 15:50:52 +01003072STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003073 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003074}
3075
Damien George2827d622014-04-27 15:50:52 +01003076STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003077 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3078}
3079
3080STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3081 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3082 // no annotation
3083 return;
3084 }
3085
3086 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3087 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3088 // named parameter with possible annotation
3089 // fallthrough
3090 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3091 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3092 // named star with possible annotation
3093 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3094 // fallthrough
3095 } else {
3096 // no annotation
3097 return;
3098 }
3099 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3100 // double star with possible annotation
3101 // fallthrough
3102 } else {
3103 // no annotation
3104 return;
3105 }
3106
3107 mp_parse_node_t pn_annotation = pns->nodes[1];
3108
3109 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3110 #if MICROPY_EMIT_NATIVE
3111 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3112 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3113 assert(id_info != NULL);
3114
3115 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3116 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3117 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3118 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3119 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003120 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003121 }
3122 }
3123 #endif // MICROPY_EMIT_NATIVE
3124 }
Damien429d7192013-10-04 19:53:11 +01003125}
3126
Damien George6be0b0a2014-08-15 14:30:52 +01003127STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse_node_t pn_inner_expr, int l_top, int for_depth) {
Damien429d7192013-10-04 19:53:11 +01003128 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003129 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003130 // no more nested if/for; compile inner expression
3131 compile_node(comp, pn_inner_expr);
3132 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003133 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003134 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003135 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003136 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003137 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003138 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003139 #endif
Damien429d7192013-10-04 19:53:11 +01003140 } else {
3141 EMIT(yield_value);
3142 EMIT(pop_top);
3143 }
Damiend99b0522013-12-21 18:17:45 +00003144 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003145 // if condition
Damiend99b0522013-12-21 18:17:45 +00003146 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003147 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3148 pn_iter = pns_comp_if->nodes[1];
3149 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00003150 } else {
3151 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01003152 // for loop
Damiend99b0522013-12-21 18:17:45 +00003153 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003154 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003155 uint l_end2 = comp_next_label(comp);
3156 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003157 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003158 EMIT_ARG(label_assign, l_top2);
3159 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003160 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3161 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 +00003162 EMIT_ARG(jump, l_top2);
3163 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003164 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01003165 }
3166}
3167
Damien George1463c1f2014-04-25 23:52:57 +01003168STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3169#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003170 // see http://www.python.org/dev/peps/pep-0257/
3171
3172 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003173 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003174 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003175 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003176 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003177 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3178 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003179 for (int i = 0; i < num_nodes; i++) {
3180 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003181 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 +00003182 // not a newline, so this is the first statement; finish search
3183 break;
3184 }
3185 }
3186 // 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 +00003187 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003188 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003189 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003190 } else {
3191 return;
3192 }
3193
3194 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003195 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3196 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003197 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3198 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3199 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3200 // compile the doc string
3201 compile_node(comp, pns->nodes[0]);
3202 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00003203 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003204 }
3205 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003206#else
3207 (void)comp;
3208 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003209#endif
Damien429d7192013-10-04 19:53:11 +01003210}
3211
Damien George1463c1f2014-04-25 23:52:57 +01003212STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003213 comp->pass = pass;
3214 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003215 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003216 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003217
Damien George36db6bc2014-05-07 17:24:22 +01003218 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003219 // reset maximum stack sizes in scope
3220 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003221 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003222 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003223 }
3224
Damien5ac1b2e2013-10-18 19:58:12 +01003225#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003226 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003227 scope_print_info(scope);
3228 }
Damien5ac1b2e2013-10-18 19:58:12 +01003229#endif
Damien429d7192013-10-04 19:53:11 +01003230
3231 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003232 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3233 assert(scope->kind == SCOPE_MODULE);
3234 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3235 compile_node(comp, pns->nodes[0]); // compile the expression
3236 EMIT(return_value);
3237 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003238 if (!comp->is_repl) {
3239 check_for_doc_string(comp, scope->pn);
3240 }
Damien429d7192013-10-04 19:53:11 +01003241 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003242 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003243 EMIT(return_value);
3244 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003245 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3246 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3247 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003248
3249 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003250 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003251 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003252 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003253 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003254 } else {
3255 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003256
Damien George2ac4af62014-08-15 16:45:41 +01003257 // argument annotations
3258 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3259
3260 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003261 mp_parse_node_t pn_annotation = pns->nodes[2];
3262 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3263 #if MICROPY_EMIT_NATIVE
3264 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3265 // nodes[2] can be null or a test-expr
3266 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3267 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3268 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3269 } else {
3270 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3271 }
Damien George2ac4af62014-08-15 16:45:41 +01003272 }
Damien Georgea5190a72014-08-15 22:39:08 +01003273 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003274 }
Damien George2ac4af62014-08-15 16:45:41 +01003275 }
Damien429d7192013-10-04 19:53:11 +01003276
3277 compile_node(comp, pns->nodes[3]); // 3 is function body
3278 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003279 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003280 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003281 EMIT(return_value);
3282 }
3283 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003284 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3285 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3286 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003287
3288 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003289 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003290 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003291 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003292 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3293 }
3294
3295 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003296
3297 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3298 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3299 EMIT(pop_top);
3300 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3301 }
Damien429d7192013-10-04 19:53:11 +01003302 EMIT(return_value);
3303 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3304 // a bit of a hack at the moment
3305
Damiend99b0522013-12-21 18:17:45 +00003306 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3307 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3308 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3309 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3310 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003311
Damien George708c0732014-04-27 19:23:46 +01003312 // We need a unique name for the comprehension argument (the iterator).
3313 // CPython uses .0, but we should be able to use anything that won't
3314 // clash with a user defined variable. Best to use an existing qstr,
3315 // so we use the blank qstr.
3316#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003317 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003318#else
3319 qstr qstr_arg = MP_QSTR_;
3320#endif
Damien George36db6bc2014-05-07 17:24:22 +01003321 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003322 bool added;
3323 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3324 assert(added);
3325 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003326 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003327 }
3328
3329 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003330 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003331 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003332 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003333 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003334 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003335 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003336 #endif
Damien429d7192013-10-04 19:53:11 +01003337 }
3338
Damien George6f355fd2014-04-10 14:11:31 +01003339 uint l_end = comp_next_label(comp);
3340 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00003341 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00003342 EMIT_ARG(label_assign, l_top);
3343 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003344 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3345 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003346 EMIT_ARG(jump, l_top);
3347 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003348 EMIT(for_iter_end);
3349
3350 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003351 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003352 }
3353 EMIT(return_value);
3354 } else {
3355 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003356 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3357 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3358 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003359
Damien George36db6bc2014-05-07 17:24:22 +01003360 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003361 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003362 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003363 assert(added);
3364 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003365 }
3366
Damien George542bd6b2015-03-26 14:42:40 +00003367 compile_load_id(comp, MP_QSTR___name__);
3368 compile_store_id(comp, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003369 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), false); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003370 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003371
3372 check_for_doc_string(comp, pns->nodes[2]);
3373 compile_node(comp, pns->nodes[2]); // 2 is class body
3374
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003375 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003376 assert(id != NULL);
3377 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003378 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003379 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003380#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003381 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003382#else
Damien George542bd6b2015-03-26 14:42:40 +00003383 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003384#endif
Damien429d7192013-10-04 19:53:11 +01003385 }
3386 EMIT(return_value);
3387 }
3388
Damien415eb6f2013-10-05 12:19:06 +01003389 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003390
3391 // make sure we match all the exception levels
3392 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003393}
3394
Damien George094d4502014-04-02 17:31:27 +01003395#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003396// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003397STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003398 comp->pass = pass;
3399 comp->scope_cur = scope;
3400 comp->next_label = 1;
3401
3402 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003403 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003404 return;
3405 }
3406
Damien George36db6bc2014-05-07 17:24:22 +01003407 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003408 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003409 }
3410
Damien826005c2013-10-05 23:17:28 +01003411 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003412 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3413 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3414 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003415
Damiend99b0522013-12-21 18:17:45 +00003416 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003417
Damiena2f2f7d2013-10-06 00:14:13 +01003418 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003419 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003420 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003421 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003422 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003423 if (comp->compile_error != MP_OBJ_NULL) {
3424 goto inline_asm_error;
3425 }
Damiena2f2f7d2013-10-06 00:14:13 +01003426 }
3427
Damiend99b0522013-12-21 18:17:45 +00003428 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003429
Damiend99b0522013-12-21 18:17:45 +00003430 mp_parse_node_t pn_body = pns->nodes[3]; // body
3431 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003432 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003433
Damien Georgecbd2f742014-01-19 11:48:48 +00003434 /*
Damien George36db6bc2014-05-07 17:24:22 +01003435 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003436 //printf("----\n");
3437 scope_print_info(scope);
3438 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003439 */
Damien826005c2013-10-05 23:17:28 +01003440
3441 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003442 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3443 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003444 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3445 // no instructions
3446 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003447 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003448 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003449 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003450 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003451 return;
3452 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003453
3454 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003455 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003456 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3457 goto not_an_instruction;
3458 }
Damiend99b0522013-12-21 18:17:45 +00003459 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003460 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3461 goto not_an_instruction;
3462 }
3463 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3464 goto not_an_instruction;
3465 }
3466 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3467 goto not_an_instruction;
3468 }
Damiend99b0522013-12-21 18:17:45 +00003469 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003470
3471 // parse node looks like an instruction
3472 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003473 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3474 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3475 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003476 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003477
3478 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003479 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003480 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003481 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003482 return;
3483 }
Damien George6f355fd2014-04-10 14:11:31 +01003484 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003485 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003486 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3487 compile_syntax_error(comp, nodes[i], "label redefined");
3488 return;
3489 }
Damien826005c2013-10-05 23:17:28 +01003490 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003491 } else if (op == MP_QSTR_align) {
3492 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003493 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003494 return;
3495 }
Damien George36db6bc2014-05-07 17:24:22 +01003496 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003497 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3498 }
3499 } else if (op == MP_QSTR_data) {
3500 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003501 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003502 return;
3503 }
Damien George36db6bc2014-05-07 17:24:22 +01003504 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003505 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003506 for (uint j = 1; j < n_args; j++) {
3507 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003508 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003509 return;
3510 }
Damien George50912e72015-01-20 11:55:10 +00003511 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003512 }
3513 }
Damien826005c2013-10-05 23:17:28 +01003514 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003515 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003516 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003517 }
3518 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003519
3520 if (comp->compile_error != MP_OBJ_NULL) {
3521 pns = pns2; // this is the parse node that had the error
3522 goto inline_asm_error;
3523 }
Damien826005c2013-10-05 23:17:28 +01003524 }
3525
Damien George36db6bc2014-05-07 17:24:22 +01003526 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003527 EMIT_INLINE_ASM(end_pass);
3528 }
3529
3530 if (comp->compile_error != MP_OBJ_NULL) {
3531 // inline assembler had an error; add traceback to its exception
3532 inline_asm_error:
3533 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 +01003534 }
Damien429d7192013-10-04 19:53:11 +01003535}
Damien George094d4502014-04-02 17:31:27 +01003536#endif
Damien429d7192013-10-04 19:53:11 +01003537
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003538STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003539#if !MICROPY_EMIT_CPYTHON
3540 // in Micro Python we put the *x parameter after all other parameters (except **y)
3541 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3542 id_info_t *id_param = NULL;
3543 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3544 id_info_t *id = &scope->id_info[i];
3545 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3546 if (id_param != NULL) {
3547 // swap star param with last param
3548 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3549 }
3550 break;
3551 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3552 id_param = id;
3553 }
3554 }
3555 }
3556#endif
3557
Damien429d7192013-10-04 19:53:11 +01003558 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003559 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003560 scope->num_locals = 0;
3561 for (int i = 0; i < scope->id_info_len; i++) {
3562 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003563 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003564 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3565 continue;
3566 }
3567 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3568 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3569 }
Damien George2827d622014-04-27 15:50:52 +01003570 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003571 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003572 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003573 }
3574 }
3575
3576 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003577#if MICROPY_EMIT_CPYTHON
3578 int num_cell = 0;
3579#endif
Damien9ecbcff2013-12-11 00:41:43 +00003580 for (int i = 0; i < scope->id_info_len; i++) {
3581 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003582#if MICROPY_EMIT_CPYTHON
3583 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003584 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003585 id->local_num = num_cell;
3586 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003587 }
Damien George6baf76e2013-12-30 22:32:17 +00003588#else
3589 // in Micro Python the cells come right after the fast locals
3590 // parameters are not counted here, since they remain at the start
3591 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003592 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003593 id->local_num = scope->num_locals;
3594 scope->num_locals += 1;
3595 }
3596#endif
Damien9ecbcff2013-12-11 00:41:43 +00003597 }
Damien9ecbcff2013-12-11 00:41:43 +00003598
3599 // compute the index of free vars (freevars[idx] in CPython)
3600 // make sure they are in the order of the parent scope
3601 if (scope->parent != NULL) {
3602 int num_free = 0;
3603 for (int i = 0; i < scope->parent->id_info_len; i++) {
3604 id_info_t *id = &scope->parent->id_info[i];
3605 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3606 for (int j = 0; j < scope->id_info_len; j++) {
3607 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003608 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003609 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003610#if MICROPY_EMIT_CPYTHON
3611 // in CPython the frees are numbered after the cells
3612 id2->local_num = num_cell + num_free;
3613#else
3614 // in Micro Python the frees come first, before the params
3615 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003616#endif
3617 num_free += 1;
3618 }
3619 }
3620 }
Damien429d7192013-10-04 19:53:11 +01003621 }
Damien George6baf76e2013-12-30 22:32:17 +00003622#if !MICROPY_EMIT_CPYTHON
3623 // in Micro Python shift all other locals after the free locals
3624 if (num_free > 0) {
3625 for (int i = 0; i < scope->id_info_len; i++) {
3626 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003627 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003628 id->local_num += num_free;
3629 }
3630 }
Damien George2827d622014-04-27 15:50:52 +01003631 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 +00003632 scope->num_locals += num_free;
3633 }
3634#endif
Damien429d7192013-10-04 19:53:11 +01003635 }
3636
Damien George8725f8f2014-02-15 19:33:11 +00003637 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003638
3639#if MICROPY_EMIT_CPYTHON
3640 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003641 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) {
3642 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003643 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003644 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003645 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 +00003646 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003647 }
3648 }
Damien George882b3632014-04-02 15:56:31 +01003649#endif
3650
Damien429d7192013-10-04 19:53:11 +01003651 int num_free = 0;
3652 for (int i = 0; i < scope->id_info_len; i++) {
3653 id_info_t *id = &scope->id_info[i];
3654 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3655 num_free += 1;
3656 }
3657 }
3658 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003659 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003660 }
3661}
3662
Damien George65cad122014-04-06 11:48:15 +01003663mp_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 +01003664 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003665 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003666 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003667 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003668
Damien George62a3a282015-03-01 12:04:05 +00003669 // create the module scope
3670 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3671
3672 // optimise constants (scope must be set for error messages to work)
3673 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003674 mp_map_t consts;
3675 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003676 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003677 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003678
Damien Georgea210c772015-03-26 15:49:53 +00003679 // create standard emitter; it's used at least for MP_PASS_SCOPE
3680 #if MICROPY_EMIT_CPYTHON
3681 emit_t *emit_cpython = emit_cpython_new();
3682 #else
3683 emit_t *emit_bc = emit_bc_new();
3684 #endif
3685
Damien826005c2013-10-05 23:17:28 +01003686 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003687 #if MICROPY_EMIT_CPYTHON
3688 comp->emit = emit_cpython;
3689 comp->emit_method_table = &emit_cpython_method_table;
3690 #else
3691 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003692 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003693 comp->emit_method_table = &emit_bc_method_table;
3694 #endif
Damien George41125902015-03-26 16:44:14 +00003695 #endif
Damien Georgea77ffe62015-03-14 12:59:31 +00003696 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003697 comp->emit_inline_asm = NULL;
3698 comp->emit_inline_asm_method_table = NULL;
Damien Georgea77ffe62015-03-14 12:59:31 +00003699 #endif
Damien826005c2013-10-05 23:17:28 +01003700 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003701 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003702 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003703#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003704 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003705 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003706#endif
Damien826005c2013-10-05 23:17:28 +01003707 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003708 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003709 }
3710
3711 // update maximim number of labels needed
3712 if (comp->next_label > max_num_labels) {
3713 max_num_labels = comp->next_label;
3714 }
Damien429d7192013-10-04 19:53:11 +01003715 }
3716
Damien826005c2013-10-05 23:17:28 +01003717 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003718 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 +00003719 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003720 }
3721
Damien Georgea210c772015-03-26 15:49:53 +00003722 // set max number of labels now that it's calculated
3723 #if MICROPY_EMIT_CPYTHON
3724 emit_cpython_set_max_num_labels(emit_cpython, max_num_labels);
3725 #else
3726 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
3727 #endif
3728
Damien826005c2013-10-05 23:17:28 +01003729 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003730#if !MICROPY_EMIT_CPYTHON
Damien Georgee67ed5d2014-01-04 13:55:24 +00003731#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003732 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003733#endif
Damien3ef4abb2013-10-12 16:53:13 +01003734#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003735 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003736#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003737#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003738 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003739 if (false) {
3740 // dummy
3741
Damien3ef4abb2013-10-12 16:53:13 +01003742#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003743 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003744 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003745 if (emit_inline_thumb == NULL) {
3746 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3747 }
3748 comp->emit = NULL;
3749 comp->emit_method_table = NULL;
3750 comp->emit_inline_asm = emit_inline_thumb;
3751 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003752 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003753 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003754 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003755 }
Damienc025ebb2013-10-12 14:30:21 +01003756#endif
3757
Damien826005c2013-10-05 23:17:28 +01003758 } else {
Damienc025ebb2013-10-12 14:30:21 +01003759
3760 // choose the emit type
3761
Damien3ef4abb2013-10-12 16:53:13 +01003762#if MICROPY_EMIT_CPYTHON
Damien Georgea210c772015-03-26 15:49:53 +00003763 comp->emit = emit_cpython;
Damienc025ebb2013-10-12 14:30:21 +01003764 comp->emit_method_table = &emit_cpython_method_table;
3765#else
Damien826005c2013-10-05 23:17:28 +01003766 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003767
3768#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003769 case MP_EMIT_OPT_NATIVE_PYTHON:
3770 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003771#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003772 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003773 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003774 }
Damien13ed3a62013-10-08 09:05:10 +01003775 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003776#elif MICROPY_EMIT_X86
3777 if (emit_native == NULL) {
3778 emit_native = emit_native_x86_new(max_num_labels);
3779 }
3780 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003781#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003782 if (emit_native == NULL) {
3783 emit_native = emit_native_thumb_new(max_num_labels);
3784 }
3785 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003786#elif MICROPY_EMIT_ARM
3787 if (emit_native == NULL) {
3788 emit_native = emit_native_arm_new(max_num_labels);
3789 }
3790 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003791#endif
3792 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003793 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003794 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003795#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003796
Damien826005c2013-10-05 23:17:28 +01003797 default:
Damien826005c2013-10-05 23:17:28 +01003798 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003799 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003800 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003801 #endif
Damien826005c2013-10-05 23:17:28 +01003802 break;
3803 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003804#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003805
Damien George1e1779e2015-01-14 00:20:28 +00003806 // need a pass to compute stack size
3807 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3808
Damien George36db6bc2014-05-07 17:24:22 +01003809 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003810 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003811 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3812 }
3813
3814 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003815 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003816 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003817 }
Damien6cdd3af2013-10-05 18:08:26 +01003818 }
Damien429d7192013-10-04 19:53:11 +01003819 }
3820
Damien George41d02b62014-01-24 22:42:28 +00003821 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003822
3823#if MICROPY_EMIT_CPYTHON
3824 emit_cpython_free(emit_cpython);
3825#else
3826 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003827#if MICROPY_EMIT_NATIVE
3828 if (emit_native != NULL) {
3829#if MICROPY_EMIT_X64
3830 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003831#elif MICROPY_EMIT_X86
3832 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003833#elif MICROPY_EMIT_THUMB
3834 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003835#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003836 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003837#endif
3838 }
3839#endif
3840#if MICROPY_EMIT_INLINE_THUMB
3841 if (emit_inline_thumb != NULL) {
3842 emit_inline_thumb_free(emit_inline_thumb);
3843 }
3844#endif
Damien Georgea210c772015-03-26 15:49:53 +00003845#endif // MICROPY_EMIT_CPYTHON
Damien George41d02b62014-01-24 22:42:28 +00003846
Damien George52b5d762014-09-23 15:31:56 +00003847 // free the parse tree
Damien George62a3a282015-03-01 12:04:05 +00003848 mp_parse_node_free(module_scope->pn);
Damien George52b5d762014-09-23 15:31:56 +00003849
Damien George41d02b62014-01-24 22:42:28 +00003850 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003851 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003852 for (scope_t *s = module_scope; s;) {
3853 scope_t *next = s->next;
3854 scope_free(s);
3855 s = next;
3856 }
Damien5ac1b2e2013-10-18 19:58:12 +01003857
Damien George41d02b62014-01-24 22:42:28 +00003858 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003859 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003860 m_del_obj(compiler_t, comp);
3861
Damien Georgea91ac202014-10-05 19:01:34 +01003862 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003863 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003864 } else {
3865#if MICROPY_EMIT_CPYTHON
3866 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003867 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003868 return mp_const_true;
3869#else
3870 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003871 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003872#endif
3873 }
Damien429d7192013-10-04 19:53:11 +01003874}