blob: 141cd2db99a7d05033e018c708f62022cded77ab [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 Georgeaf272592014-04-04 11:21:58 +0000271 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100272 }
273 }
274 break;
275
276 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000277 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 +0100278 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
279 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000280 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100281 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000282 if (!mp_small_int_mul_overflow(arg0, arg1)) {
283 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100284 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000285 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
286 }
287 }
Damiend99b0522013-12-21 18:17:45 +0000288 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100289 // int / int
290 // pass
Damiend99b0522013-12-21 18:17:45 +0000291 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100292 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000293 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damien George0bb97132015-02-27 14:25:47 +0000294 } else {
295 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300296 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100297 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000298 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 +0300299 }
Damien429d7192013-10-04 19:53:11 +0100300 }
301 }
302 break;
303
304 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000305 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100306 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000307 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100308 // +int
Damiend99b0522013-12-21 18:17:45 +0000309 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
310 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100311 // -int
Damiend99b0522013-12-21 18:17:45 +0000312 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
Damien George0bb97132015-02-27 14:25:47 +0000313 } else {
314 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100315 // ~int
Damiend99b0522013-12-21 18:17:45 +0000316 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100317 }
318 }
319 break;
320
321 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100322 if (0) {
323#if MICROPY_EMIT_CPYTHON
324 } 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 +0100325 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100326 // can overflow; enabled only to compare with CPython
Damien George4dea9222015-04-09 15:29:54 +0000327 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
Damiend99b0522013-12-21 18:17:45 +0000328 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200329 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100330 if (power >= 0) {
331 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200332 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100333 for (; power > 0; power--) {
334 ans *= base;
335 }
Damiend99b0522013-12-21 18:17:45 +0000336 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100337 }
338 }
Damien George57e99eb2014-04-10 22:42:11 +0100339#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000340#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100341 } 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])) {
342 // id.id
343 // look it up in constant table, see if it can be replaced with an integer
Damien George4dea9222015-04-09 15:29:54 +0000344 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George57e99eb2014-04-10 22:42:11 +0100345 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
346 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
347 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
348 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
349 if (elem != NULL) {
350 mp_obj_t dest[2];
351 mp_load_method_maybe(elem->value, q_attr, dest);
352 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100353 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000354 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100355 }
356 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000357#endif
Damien429d7192013-10-04 19:53:11 +0100358 }
359 break;
360 }
361 }
362
363 return pn;
364}
365
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200366STATIC 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 +0100367STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000368STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100369
Damien George6f355fd2014-04-10 14:11:31 +0100370STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100371 return comp->next_label++;
372}
373
Damien George8dcc0c72014-03-27 10:55:21 +0000374STATIC void compile_increase_except_level(compiler_t *comp) {
375 comp->cur_except_level += 1;
376 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
377 comp->scope_cur->exc_stack_size = comp->cur_except_level;
378 }
379}
380
381STATIC void compile_decrease_except_level(compiler_t *comp) {
382 assert(comp->cur_except_level > 0);
383 comp->cur_except_level -= 1;
384}
385
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200386STATIC 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 +0100387 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100388 scope->parent = comp->scope_cur;
389 scope->next = NULL;
390 if (comp->scope_head == NULL) {
391 comp->scope_head = scope;
392 } else {
393 scope_t *s = comp->scope_head;
394 while (s->next != NULL) {
395 s = s->next;
396 }
397 s->next = scope;
398 }
399 return scope;
400}
401
Damien George91bc32d2015-04-09 15:31:53 +0000402typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
403
404STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, apply_list_fun_t f) {
Damien George963a5a32015-01-16 17:47:07 +0000405 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000406 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
407 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100408 for (int i = 0; i < num_nodes; i++) {
409 f(comp, pns->nodes[i]);
410 }
Damiend99b0522013-12-21 18:17:45 +0000411 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100412 f(comp, pn);
413 }
414}
415
Damien George969a6b32014-12-10 22:07:04 +0000416STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000417 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100418 for (int i = 0; i < num_nodes; i++) {
419 compile_node(comp, pns->nodes[i]);
420 }
421}
422
Damien George542bd6b2015-03-26 14:42:40 +0000423STATIC void compile_load_id(compiler_t *comp, qstr qst) {
424 if (comp->pass == MP_PASS_SCOPE) {
425 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
426 } else {
Damien George41125902015-03-26 16:44:14 +0000427 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000428 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000429 #else
430 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
431 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000432 }
433}
434
435STATIC void compile_store_id(compiler_t *comp, qstr qst) {
436 if (comp->pass == MP_PASS_SCOPE) {
437 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
438 } else {
Damien George41125902015-03-26 16:44:14 +0000439 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000440 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000441 #else
442 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
443 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000444 }
445}
446
447STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
448 if (comp->pass == MP_PASS_SCOPE) {
449 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
450 } else {
Damien George41125902015-03-26 16:44:14 +0000451 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000452 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000453 #else
454 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
455 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000456 }
457}
458
Damien3ef4abb2013-10-12 16:53:13 +0100459#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200460STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100461 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
462 return true;
463 }
Damien George4c81ba82015-01-13 16:21:23 +0000464 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
465 return true;
466 }
Damien George7d414a12015-02-08 01:57:40 +0000467 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
468 return true;
469 }
Damiend99b0522013-12-21 18:17:45 +0000470 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100471 return false;
472 }
Damiend99b0522013-12-21 18:17:45 +0000473 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100474 return false;
475 }
476 return true;
477}
478
Damien George5042bce2014-05-25 22:06:06 +0100479STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000480 bool has_single_quote = false;
481 bool has_double_quote = false;
482 for (int i = 0; i < len; i++) {
483 if (str[i] == '\'') {
484 has_single_quote = true;
485 } else if (str[i] == '"') {
486 has_double_quote = true;
487 }
488 }
489 if (bytes) {
490 vstr_printf(vstr, "b");
491 }
492 bool quote_single = false;
493 if (has_single_quote && !has_double_quote) {
494 vstr_printf(vstr, "\"");
495 } else {
496 quote_single = true;
497 vstr_printf(vstr, "'");
498 }
499 for (int i = 0; i < len; i++) {
500 if (str[i] == '\n') {
501 vstr_printf(vstr, "\\n");
502 } else if (str[i] == '\\') {
503 vstr_printf(vstr, "\\\\");
504 } else if (str[i] == '\'' && quote_single) {
505 vstr_printf(vstr, "\\'");
506 } else {
507 vstr_printf(vstr, "%c", str[i]);
508 }
509 }
510 if (has_single_quote && !has_double_quote) {
511 vstr_printf(vstr, "\"");
512 } else {
513 vstr_printf(vstr, "'");
514 }
515}
516
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200517STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000518 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 +0100519 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000520 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 +0100521 return;
522 }
523
Damien George7d414a12015-02-08 01:57:40 +0000524 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
525 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
526 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
527 return;
528 }
529
Damiend99b0522013-12-21 18:17:45 +0000530 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200531 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
532 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
533 return;
534 }
535
Damien George42f3de92014-10-03 17:44:14 +0000536 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000537 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
538 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100539 case MP_PARSE_NODE_STRING:
540 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100541 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100542 const byte *str = qstr_data(arg, &len);
543 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
544 break;
545 }
Damiend99b0522013-12-21 18:17:45 +0000546 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100547 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000548 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
549 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
550 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100551 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100552 }
553 break;
554 default: assert(0);
555 }
556}
557
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200558STATIC 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 +0100559 int n = 0;
560 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000561 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100562 }
563 int total = n;
564 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000565 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100566 total += 1;
Damien3a205172013-10-12 15:01:56 +0100567 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100568 is_const = false;
569 }
570 }
571 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100572 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100573 is_const = false;
574 break;
575 }
576 }
577 if (total > 0 && is_const) {
578 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000579 vstr_t *vstr = vstr_new();
580 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000581 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000582 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100583 need_comma = true;
584 }
585 for (int i = 0; i < n; i++) {
586 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000587 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100588 }
Damien02f89412013-12-12 15:13:36 +0000589 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100590 need_comma = true;
591 }
592 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000593 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100594 } else {
Damien02f89412013-12-12 15:13:36 +0000595 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100596 }
Damien George0d3cb672015-01-28 23:43:01 +0000597 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000598 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100599 } else {
Damiend99b0522013-12-21 18:17:45 +0000600 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100601 compile_node(comp, pn);
602 }
603 for (int i = 0; i < n; i++) {
604 compile_node(comp, pns_list->nodes[i]);
605 }
Damien Georgeb9791222014-01-23 00:34:21 +0000606 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100607 }
608}
Damien3a205172013-10-12 15:01:56 +0100609#endif
610
611// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100612STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100613#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100614 cpython_c_tuple(comp, pn, pns_list);
615#else
616 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000617 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100618 compile_node(comp, pn);
619 total += 1;
620 }
621 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000622 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100623 for (int i = 0; i < n; i++) {
624 compile_node(comp, pns_list->nodes[i]);
625 }
626 total += n;
627 }
Damien Georgeb9791222014-01-23 00:34:21 +0000628 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100629#endif
630}
Damien429d7192013-10-04 19:53:11 +0100631
Damien George969a6b32014-12-10 22:07:04 +0000632STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100633 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000634 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100635}
636
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200637STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000638 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
639 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100640}
641
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200642STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000643 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
644 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100645}
646
Damien3ef4abb2013-10-12 16:53:13 +0100647#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100648// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200649STATIC 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 +0100650 if (node_is_const_false(pn)) {
651 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000652 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100653 }
654 return;
655 } else if (node_is_const_true(pn)) {
656 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000657 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100658 }
659 return;
Damiend99b0522013-12-21 18:17:45 +0000660 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
661 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
662 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
663 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100664 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100665 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100666 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100667 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100668 }
Damien3a205172013-10-12 15:01:56 +0100669 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000670 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100671 } else {
672 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100673 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100674 }
675 }
676 return;
Damiend99b0522013-12-21 18:17:45 +0000677 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100678 if (jump_if == false) {
679 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100680 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100681 }
682 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100683 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100684 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100685 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100686 }
Damien3a205172013-10-12 15:01:56 +0100687 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000688 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100689 }
690 return;
Damiend99b0522013-12-21 18:17:45 +0000691 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100692 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100693 return;
694 }
695 }
696
697 // nothing special, fall back to default compiling for node and jump
698 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000699 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100700}
Damien3a205172013-10-12 15:01:56 +0100701#endif
Damien429d7192013-10-04 19:53:11 +0100702
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200703STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100704#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100705 cpython_c_if_cond(comp, pn, jump_if, label, false);
706#else
707 if (node_is_const_false(pn)) {
708 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000709 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100710 }
711 return;
712 } else if (node_is_const_true(pn)) {
713 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000714 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100715 }
716 return;
Damiend99b0522013-12-21 18:17:45 +0000717 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
718 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
719 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
720 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100721 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000722 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100723 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100724 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000725 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100726 }
Damien George0b2fd912015-02-28 14:37:54 +0000727 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000728 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100729 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000730 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100731 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000732 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100733 }
734 }
735 return;
Damiend99b0522013-12-21 18:17:45 +0000736 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100737 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000738 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100739 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000740 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100741 }
Damiend99b0522013-12-21 18:17:45 +0000742 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100743 c_if_cond(comp, pns->nodes[0], !jump_if, label);
744 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100745 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
746 // cond is something in parenthesis
747 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
748 // empty tuple, acts as false for the condition
749 if (jump_if == false) {
750 EMIT_ARG(jump, label);
751 }
752 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
753 // non-empty tuple, acts as true for the condition
754 if (jump_if == true) {
755 EMIT_ARG(jump, label);
756 }
757 } else {
758 // parenthesis around 1 item, is just that item
759 c_if_cond(comp, pns->nodes[0], jump_if, label);
760 }
761 return;
Damien3a205172013-10-12 15:01:56 +0100762 }
763 }
764
765 // nothing special, fall back to default compiling for node and jump
766 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000767 EMIT_ARG(pop_jump_if, jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100768#endif
Damien429d7192013-10-04 19:53:11 +0100769}
770
771typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100772STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100773
Damien George6be0b0a2014-08-15 14:30:52 +0100774STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100775 if (assign_kind != ASSIGN_AUG_STORE) {
776 compile_node(comp, pns->nodes[0]);
777 }
778
Damiend99b0522013-12-21 18:17:45 +0000779 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
780 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
781 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
782 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100783 if (assign_kind != ASSIGN_AUG_STORE) {
784 for (int i = 0; i < n - 1; i++) {
785 compile_node(comp, pns1->nodes[i]);
786 }
787 }
Damiend99b0522013-12-21 18:17:45 +0000788 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
789 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100790 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000791 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100792 if (assign_kind == ASSIGN_AUG_STORE) {
793 EMIT(rot_three);
794 EMIT(store_subscr);
795 } else {
796 compile_node(comp, pns1->nodes[0]);
797 if (assign_kind == ASSIGN_AUG_LOAD) {
798 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100799 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100800 } else {
801 EMIT(store_subscr);
802 }
803 }
Damiend99b0522013-12-21 18:17:45 +0000804 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
805 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100806 if (assign_kind == ASSIGN_AUG_LOAD) {
807 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000808 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100809 } else {
810 if (assign_kind == ASSIGN_AUG_STORE) {
811 EMIT(rot_two);
812 }
Damien Georgeb9791222014-01-23 00:34:21 +0000813 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100814 }
815 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100816 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100817 }
818 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100819 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100820 }
821
Damiend99b0522013-12-21 18:17:45 +0000822 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100823 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100824 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100825
826 return;
827
828cannot_assign:
829 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100830}
831
Damien George0288cf02014-04-11 11:53:00 +0000832// 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 +0100833STATIC 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 +0000834 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
835
836 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000837 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000838 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
839 EMIT_ARG(unpack_ex, 0, num_tail);
840 have_star_index = 0;
841 }
Damien George963a5a32015-01-16 17:47:07 +0000842 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000843 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000844 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000845 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
846 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100847 } else {
Damien George9d181f62014-04-27 16:55:27 +0100848 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100849 return;
850 }
851 }
852 }
Damien George963a5a32015-01-16 17:47:07 +0000853 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000854 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100855 }
Damien George0288cf02014-04-11 11:53:00 +0000856 if (num_head != 0) {
857 if (0 == have_star_index) {
858 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100859 } else {
Damien George0288cf02014-04-11 11:53:00 +0000860 c_assign(comp, node_head, ASSIGN_STORE);
861 }
862 }
Damien George963a5a32015-01-16 17:47:07 +0000863 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000864 if (num_head + i == have_star_index) {
865 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
866 } else {
867 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100868 }
869 }
870}
871
872// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100873STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100874 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000875 assert(!MP_PARSE_NODE_IS_NULL(pn));
876 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000877 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000878 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100879 switch (assign_kind) {
880 case ASSIGN_STORE:
881 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000882 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100883 break;
884 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000885 default:
Damien George542bd6b2015-03-26 14:42:40 +0000886 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100887 break;
888 }
889 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100890 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100891 return;
892 }
893 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000894 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000895 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
896 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100897 case PN_power:
898 // lhs is an index or attribute
899 c_assign_power(comp, pns, assign_kind);
900 break;
901
902 case PN_testlist_star_expr:
903 case PN_exprlist:
904 // lhs is a tuple
905 if (assign_kind != ASSIGN_STORE) {
906 goto bad_aug;
907 }
Damien George0288cf02014-04-11 11:53:00 +0000908 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100909 break;
910
911 case PN_atom_paren:
912 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000913 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100914 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100915 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000916 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000917 if (assign_kind != ASSIGN_STORE) {
918 goto bad_aug;
919 }
Damiend99b0522013-12-21 18:17:45 +0000920 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100921 goto testlist_comp;
922 } else {
923 // parenthesis around 1 item, is just that item
924 pn = pns->nodes[0];
925 goto tail_recursion;
926 }
927 break;
928
929 case PN_atom_bracket:
930 // lhs is something in brackets
931 if (assign_kind != ASSIGN_STORE) {
932 goto bad_aug;
933 }
Damiend99b0522013-12-21 18:17:45 +0000934 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100935 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000936 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000937 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
938 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100939 goto testlist_comp;
940 } else {
941 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000942 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100943 }
944 break;
945
946 default:
Damien George9d181f62014-04-27 16:55:27 +0100947 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100948 }
949 return;
950
951 testlist_comp:
952 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000953 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
954 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
955 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100956 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000957 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000958 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000959 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100960 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000961 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
962 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000963 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100964 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100965 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100966 } else {
967 // sequence with 2 items
968 goto sequence_with_2_items;
969 }
970 } else {
971 // sequence with 2 items
972 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000973 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100974 }
975 return;
976 }
977 return;
978
Damien George9d181f62014-04-27 16:55:27 +0100979 cannot_assign:
980 compile_syntax_error(comp, pn, "can't assign to expression");
981 return;
982
Damien429d7192013-10-04 19:53:11 +0100983 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100984 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100985}
986
987// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100988// if we are not in CPython compatibility mode then:
989// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
990// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
991// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100992STATIC 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 +0100993 assert(n_pos_defaults >= 0);
994 assert(n_kw_defaults >= 0);
995
Damien429d7192013-10-04 19:53:11 +0100996 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000997 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100998 int nfree = 0;
999 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +00001000 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
1001 id_info_t *id = &comp->scope_cur->id_info[i];
1002 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
1003 for (int j = 0; j < this_scope->id_info_len; j++) {
1004 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01001005 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +00001006#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +01001007 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001008#else
1009 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +00001010 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001011#endif
Damien318aec62013-12-10 18:28:17 +00001012 nfree += 1;
1013 }
1014 }
Damien429d7192013-10-04 19:53:11 +01001015 }
1016 }
1017 }
Damien429d7192013-10-04 19:53:11 +01001018
1019 // make the function/closure
1020 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001021 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001022 } else {
Damien George3558f622014-04-20 17:50:40 +01001023 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001024 }
1025}
1026
Damien George6be0b0a2014-08-15 14:30:52 +01001027STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001028 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001029 comp->have_star = true;
1030 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001031 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1032 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001033 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001034 } else {
1035 // named star
Damien429d7192013-10-04 19:53:11 +01001036 }
Damien George8b19db02014-04-11 23:25:34 +01001037 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001038
1039 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001040 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001041 // TODO do we need to do anything with this?
1042
1043 } else {
1044 mp_parse_node_t pn_id;
1045 mp_parse_node_t pn_colon;
1046 mp_parse_node_t pn_equal;
1047 if (MP_PARSE_NODE_IS_ID(pn)) {
1048 // this parameter is just an id
1049
1050 pn_id = pn;
1051 pn_colon = MP_PARSE_NODE_NULL;
1052 pn_equal = MP_PARSE_NODE_NULL;
1053
Damien George0bb97132015-02-27 14:25:47 +00001054 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001055 // this parameter has a colon and/or equal specifier
1056
Damien George0bb97132015-02-27 14:25:47 +00001057 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1058
Damien Georgef41fdd02014-03-03 23:19:11 +00001059 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1060 pn_id = pns->nodes[0];
1061 pn_colon = pns->nodes[1];
1062 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +00001063 }
1064
1065 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1066 // this parameter does not have a default value
1067
1068 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001069 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001070 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001071 return;
1072 }
1073
1074 } else {
1075 // this parameter has a default value
1076 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1077
Damien George8b19db02014-04-11 23:25:34 +01001078 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001079 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001080#if MICROPY_EMIT_CPYTHON
Damien George59fba2d2015-06-25 14:42:13 +00001081 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien Georgef0778a72014-06-07 22:01:00 +01001082 compile_node(comp, pn_equal);
1083#else
Damien George69b89d22014-04-11 13:38:30 +00001084 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1085 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001086 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1087 // we need to do this here before we start building the map for the default keywords
1088 if (comp->num_default_params > 0) {
1089 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001090 } else {
1091 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001092 }
Damien George69b89d22014-04-11 13:38:30 +00001093 // first default dict param, so make the map
1094 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001095 }
Damien Georgef0778a72014-06-07 22:01:00 +01001096
1097 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001098 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +00001099 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +00001100 EMIT(store_map);
1101#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001102 } else {
Damien George69b89d22014-04-11 13:38:30 +00001103 comp->num_default_params += 1;
1104 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001105 }
1106 }
1107
1108 // TODO pn_colon not implemented
1109 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001110 }
1111}
1112
1113// leaves function object on stack
1114// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001115STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001116 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001117 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001118 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001119 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001120 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001121 }
1122
1123 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001124 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001125 uint old_num_dict_params = comp->num_dict_params;
1126 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001127
1128 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001129 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001130 comp->num_dict_params = 0;
1131 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001132 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001133
Damien Georgea91ac202014-10-05 19:01:34 +01001134 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001135 return MP_QSTR_NULL;
1136 }
1137
Damien Georgee337f1e2014-03-31 15:18:37 +01001138#if !MICROPY_EMIT_CPYTHON
1139 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001140 // the default keywords args may have already made the tuple; if not, do it now
1141 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001142 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001143 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001144 }
1145#endif
1146
Damien429d7192013-10-04 19:53:11 +01001147 // get the scope for this function
1148 scope_t *fscope = (scope_t*)pns->nodes[4];
1149
1150 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001151 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001152
1153 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001154 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001155 comp->num_dict_params = old_num_dict_params;
1156 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001157
1158 // return its name (the 'f' in "def f(...):")
1159 return fscope->simple_name;
1160}
1161
1162// leaves class object on stack
1163// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001164STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001165 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001166 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001167 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001168 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001169 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001170 }
1171
1172 EMIT(load_build_class);
1173
1174 // scope for this class
1175 scope_t *cscope = (scope_t*)pns->nodes[3];
1176
1177 // compile the class
1178 close_over_variables_etc(comp, cscope, 0, 0);
1179
1180 // get its name
Damien George59fba2d2015-06-25 14:42:13 +00001181 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001182
1183 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001184 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1185 mp_parse_node_t parents = pns->nodes[1];
1186 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1187 parents = MP_PARSE_NODE_NULL;
1188 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001189 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001190 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001191
1192 // return its name (the 'C' in class C(...):")
1193 return cscope->simple_name;
1194}
1195
Damien6cdd3af2013-10-05 18:08:26 +01001196// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001197STATIC 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 +00001198 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001199 return false;
1200 }
1201
1202 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001203 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001204 return true;
1205 }
1206
Damiend99b0522013-12-21 18:17:45 +00001207 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001208 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001209 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001210#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001211 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001212 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001213 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001214 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001215#endif
Damien3ef4abb2013-10-12 16:53:13 +01001216#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001217 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001218 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001219#endif
Damien6cdd3af2013-10-05 18:08:26 +01001220 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001221 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001222 }
1223
1224 return true;
1225}
1226
Damien George969a6b32014-12-10 22:07:04 +00001227STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001228 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001229 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001230 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001231
Damien6cdd3af2013-10-05 18:08:26 +01001232 // inherit emit options for this function/class definition
1233 uint emit_options = comp->scope_cur->emit_options;
1234
1235 // compile each decorator
1236 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001237 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001238 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1239 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001240
1241 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001242 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001243 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001244
1245 // check for built-in decorators
1246 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1247 // this was a built-in
1248 num_built_in_decorators += 1;
1249
1250 } else {
1251 // not a built-in, compile normally
1252
1253 // compile the decorator function
1254 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001255 for (int j = 1; j < name_len; j++) {
1256 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1257 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001258 }
1259
1260 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001261 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001262 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001263 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001264 compile_node(comp, pns_decorator->nodes[1]);
1265 }
Damien429d7192013-10-04 19:53:11 +01001266 }
1267 }
1268
1269 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001270 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001271 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001272 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001273 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001274 } else {
Damien George0bb97132015-02-27 14:25:47 +00001275 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1276 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001277 }
1278
1279 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001280 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001281 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001282 }
1283
1284 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +00001285 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +01001286}
1287
Damien George969a6b32014-12-10 22:07:04 +00001288STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001289 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001290 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +00001291 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +01001292}
1293
Damien George6be0b0a2014-08-15 14:30:52 +01001294STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001295 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00001296 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001297 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1298 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001299
1300 compile_node(comp, pns->nodes[0]); // base of the power node
1301
Damiend99b0522013-12-21 18:17:45 +00001302 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1303 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1304 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1305 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001306 for (int i = 0; i < n - 1; i++) {
1307 compile_node(comp, pns1->nodes[i]);
1308 }
Damiend99b0522013-12-21 18:17:45 +00001309 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1310 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001311 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001312 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001313 compile_node(comp, pns1->nodes[0]);
1314 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001315 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1316 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001317 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001318 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001319 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001320 }
1321 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001322 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001323 }
1324
Damiend99b0522013-12-21 18:17:45 +00001325 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001326 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001327 }
Damiend99b0522013-12-21 18:17:45 +00001328 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1329 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1330 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1331 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001332 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1333
Damiend99b0522013-12-21 18:17:45 +00001334 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1335 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1336 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001337 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001338 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001339 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001340 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001341 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001342 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001343 c_del_stmt(comp, pns->nodes[0]);
1344 for (int i = 0; i < n; i++) {
1345 c_del_stmt(comp, pns1->nodes[i]);
1346 }
Damiend99b0522013-12-21 18:17:45 +00001347 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001348 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001349 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001350 } else {
1351 // sequence with 2 items
1352 goto sequence_with_2_items;
1353 }
1354 } else {
1355 // sequence with 2 items
1356 sequence_with_2_items:
1357 c_del_stmt(comp, pns->nodes[0]);
1358 c_del_stmt(comp, pns->nodes[1]);
1359 }
1360 } else {
1361 // tuple with 1 element
1362 c_del_stmt(comp, pn);
1363 }
1364 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001365 // TODO is there anything else to implement?
1366 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001367 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001368
1369 return;
1370
1371cannot_delete:
1372 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001373}
1374
Damien George969a6b32014-12-10 22:07:04 +00001375STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001376 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1377}
1378
Damien George969a6b32014-12-10 22:07:04 +00001379STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001380 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001381 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001382 }
Damien George090c9232014-10-17 14:08:49 +00001383 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001384 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001385}
1386
Damien George969a6b32014-12-10 22:07:04 +00001387STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001388 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001389 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001390 }
Damien George090c9232014-10-17 14:08:49 +00001391 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001392 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001393}
1394
Damien George969a6b32014-12-10 22:07:04 +00001395STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001396 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001397 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001398 return;
1399 }
Damiend99b0522013-12-21 18:17:45 +00001400 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001401 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001402 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001403 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001404 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001405 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1406 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 +01001407
Damien George6f355fd2014-04-10 14:11:31 +01001408 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001409 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1410 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1411 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001412 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001413 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1414 } else {
1415 compile_node(comp, pns->nodes[0]);
1416 }
1417 EMIT(return_value);
1418}
1419
Damien George969a6b32014-12-10 22:07:04 +00001420STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001421 compile_node(comp, pns->nodes[0]);
1422 EMIT(pop_top);
1423}
1424
Damien George969a6b32014-12-10 22:07:04 +00001425STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001426 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001427 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001428 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001429 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001430 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001431 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001432 compile_node(comp, pns->nodes[0]);
1433 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001434 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001435 } else {
1436 // raise x
1437 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001438 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001439 }
1440}
1441
Damien George635543c2014-04-10 12:56:52 +01001442// q_base holds the base of the name
1443// eg a -> q_base=a
1444// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001445STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001446 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001447 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1448 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001449 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001450 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001451 pn = pns->nodes[0];
1452 is_as = true;
1453 }
Damien George635543c2014-04-10 12:56:52 +01001454 if (MP_PARSE_NODE_IS_NULL(pn)) {
1455 // empty name (eg, from . import x)
1456 *q_base = MP_QSTR_;
1457 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1458 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001459 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001460 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001461 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001462 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001463 }
Damien George635543c2014-04-10 12:56:52 +01001464 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001465 } else {
1466 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001467 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001468 {
Damien429d7192013-10-04 19:53:11 +01001469 // a name of the form a.b.c
1470 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001471 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001472 }
Damiend99b0522013-12-21 18:17:45 +00001473 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001474 int len = n - 1;
1475 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001476 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001477 }
Damien George55baff42014-01-21 21:40:13 +00001478 byte *q_ptr;
1479 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001480 for (int i = 0; i < n; i++) {
1481 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001482 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001483 }
Damien George39dc1452014-10-03 19:52:22 +01001484 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001485 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001486 memcpy(str_dest, str_src, str_src_len);
1487 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001488 }
Damien George635543c2014-04-10 12:56:52 +01001489 qstr q_full = qstr_build_end(q_ptr);
1490 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001491 if (is_as) {
1492 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001493 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001494 }
1495 }
Damien429d7192013-10-04 19:53:11 +01001496 }
Damien429d7192013-10-04 19:53:11 +01001497 }
1498}
1499
Damien George6be0b0a2014-08-15 14:30:52 +01001500STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001501 EMIT_ARG(load_const_small_int, 0); // level 0 import
1502 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1503 qstr q_base;
1504 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001505 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001506}
1507
Damien George969a6b32014-12-10 22:07:04 +00001508STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001509 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1510}
1511
Damien George969a6b32014-12-10 22:07:04 +00001512STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001513 mp_parse_node_t pn_import_source = pns->nodes[0];
1514
1515 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1516 uint import_level = 0;
1517 do {
1518 mp_parse_node_t pn_rel;
1519 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)) {
1520 // This covers relative imports with dots only like "from .. import"
1521 pn_rel = pn_import_source;
1522 pn_import_source = MP_PARSE_NODE_NULL;
1523 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1524 // This covers relative imports starting with dot(s) like "from .foo import"
1525 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1526 pn_rel = pns_2b->nodes[0];
1527 pn_import_source = pns_2b->nodes[1];
1528 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1529 } else {
1530 // Not a relative import
1531 break;
1532 }
1533
1534 // get the list of . and/or ...'s
1535 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001536 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001537
1538 // count the total number of .'s
1539 for (int i = 0; i < n; i++) {
1540 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1541 import_level++;
1542 } else {
1543 // should be an MP_TOKEN_ELLIPSIS
1544 import_level += 3;
1545 }
1546 }
1547 } while (0);
1548
Damiend99b0522013-12-21 18:17:45 +00001549 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001550 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001551
1552 // build the "fromlist" tuple
1553#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001554 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001555#else
Damien George59fba2d2015-06-25 14:42:13 +00001556 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001557 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001558#endif
1559
1560 // do the import
Damien George635543c2014-04-10 12:56:52 +01001561 qstr dummy_q;
1562 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001563 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001564
Damien429d7192013-10-04 19:53:11 +01001565 } else {
Damien George635543c2014-04-10 12:56:52 +01001566 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001567
1568 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001569 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001570 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001571#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001572 {
1573 vstr_t *vstr = vstr_new();
1574 vstr_printf(vstr, "(");
1575 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001576 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1577 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1578 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001579 if (i > 0) {
1580 vstr_printf(vstr, ", ");
1581 }
1582 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001583 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001584 const byte *str = qstr_data(id2, &len);
1585 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001586 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001587 }
Damien02f89412013-12-12 15:13:36 +00001588 if (n == 1) {
1589 vstr_printf(vstr, ",");
1590 }
1591 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001592 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001593 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001594 }
Damiendb4c3612013-12-10 17:27:24 +00001595#else
1596 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001597 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1598 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1599 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001600 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001601 }
Damien Georgeb9791222014-01-23 00:34:21 +00001602 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001603#endif
1604
1605 // do the import
Damien George635543c2014-04-10 12:56:52 +01001606 qstr dummy_q;
1607 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001608 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001609 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1610 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1611 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001612 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001613 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001614 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001615 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001616 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001617 }
1618 }
1619 EMIT(pop_top);
1620 }
1621}
1622
Damien George584ba672014-12-21 17:26:45 +00001623STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1624 bool added;
1625 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1626 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001627 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001628 compile_syntax_error(comp, pn, "identifier already used");
1629 return;
1630 }
1631 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1632
1633 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1634 id_info = scope_find_global(comp->scope_cur, qst);
1635 if (id_info != NULL) {
1636 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1637 }
1638}
1639
Damien George969a6b32014-12-10 22:07:04 +00001640STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001641 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001642 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001643 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001644 for (int i = 0; i < n; i++) {
1645 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001646 }
1647 }
1648}
1649
Damien George584ba672014-12-21 17:26:45 +00001650STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1651 bool added;
1652 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1653 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001654 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001655 compile_syntax_error(comp, pn, "identifier already used");
1656 return;
1657 }
1658 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1659 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)) {
1660 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1661 return;
1662 }
1663 id_info->kind = ID_INFO_KIND_FREE;
1664 scope_close_over_in_parents(comp->scope_cur, qst);
1665}
1666
Damien George969a6b32014-12-10 22:07:04 +00001667STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001668 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001669 if (comp->scope_cur->kind == SCOPE_MODULE) {
1670 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1671 return;
1672 }
1673 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001674 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001675 for (int i = 0; i < n; i++) {
1676 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001677 }
1678 }
1679}
1680
Damien George969a6b32014-12-10 22:07:04 +00001681STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001682 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001683 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001684 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001685 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001686 // assertion message
1687 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001688 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001689 }
Damien Georgeb9791222014-01-23 00:34:21 +00001690 EMIT_ARG(raise_varargs, 1);
1691 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001692}
1693
Damien George969a6b32014-12-10 22:07:04 +00001694STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001695 // TODO proper and/or short circuiting
1696
Damien George6f355fd2014-04-10 14:11:31 +01001697 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001698
Damien George391db862014-10-17 17:57:33 +00001699 // optimisation: don't emit anything when "if False" (not in CPython)
1700 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1701 uint l_fail = comp_next_label(comp);
1702 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001703
Damien George391db862014-10-17 17:57:33 +00001704 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001705
Damien George391db862014-10-17 17:57:33 +00001706 // optimisation: skip everything else when "if True" (not in CPython)
1707 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1708 goto done;
1709 }
1710
1711 if (
1712 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1713 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1714 // optimisation: don't jump if last instruction was return
1715 && !EMIT(last_emit_was_return_value)
1716 ) {
1717 // jump over elif/else blocks
1718 EMIT_ARG(jump, l_end);
1719 }
1720
1721 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001722 }
1723
Damien George235f9b32014-10-17 17:30:16 +00001724 // compile elif blocks (if any)
1725 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001726 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 +00001727 for (int i = 0; i < n_elif; i++) {
1728 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1729 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001730
Damien George391db862014-10-17 17:57:33 +00001731 // optimisation: don't emit anything when "if False" (not in CPython)
1732 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1733 uint l_fail = comp_next_label(comp);
1734 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001735
Damien George391db862014-10-17 17:57:33 +00001736 compile_node(comp, pns_elif->nodes[1]); // elif block
1737
1738 // optimisation: skip everything else when "elif True" (not in CPython)
1739 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1740 goto done;
1741 }
1742
1743 // optimisation: don't jump if last instruction was return
1744 if (!EMIT(last_emit_was_return_value)) {
1745 EMIT_ARG(jump, l_end);
1746 }
1747 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001748 }
1749 }
1750
1751 // compile else block
1752 compile_node(comp, pns->nodes[3]); // can be null
1753
Damien George391db862014-10-17 17:57:33 +00001754done:
Damien Georgeb9791222014-01-23 00:34:21 +00001755 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001756}
1757
Damien Georgecbddb272014-02-01 20:08:18 +00001758#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001759 uint16_t old_break_label = comp->break_label; \
1760 uint16_t old_continue_label = comp->continue_label; \
1761 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001762 uint break_label = comp_next_label(comp); \
1763 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001764 comp->break_label = break_label; \
1765 comp->continue_label = continue_label; \
1766 comp->break_continue_except_level = comp->cur_except_level;
1767
1768#define END_BREAK_CONTINUE_BLOCK \
1769 comp->break_label = old_break_label; \
1770 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001771 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001772
Damien George969a6b32014-12-10 22:07:04 +00001773STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001774 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001775
Damience89a212013-10-15 22:25:17 +01001776 // compared to CPython, we have an optimised version of while loops
1777#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001778 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001779 EMIT_ARG(setup_loop, break_label);
1780 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001781 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1782 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001783 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001784 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001785 }
Damien Georgeb9791222014-01-23 00:34:21 +00001786 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001787 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1788 // this is a small hack to agree with CPython
1789 if (!node_is_const_true(pns->nodes[0])) {
1790 EMIT(pop_block);
1791 }
Damience89a212013-10-15 22:25:17 +01001792#else
Damien George391db862014-10-17 17:57:33 +00001793 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1794 uint top_label = comp_next_label(comp);
1795 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1796 EMIT_ARG(jump, continue_label);
1797 }
1798 EMIT_ARG(label_assign, top_label);
1799 compile_node(comp, pns->nodes[1]); // body
1800 EMIT_ARG(label_assign, continue_label);
1801 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1802 }
Damience89a212013-10-15 22:25:17 +01001803#endif
1804
1805 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001806 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001807
1808 compile_node(comp, pns->nodes[2]); // else
1809
Damien Georgeb9791222014-01-23 00:34:21 +00001810 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001811}
1812
Damien George2ac4af62014-08-15 16:45:41 +01001813#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001814// This function compiles an optimised for-loop of the form:
1815// for <var> in range(<start>, <end>, <step>):
1816// <body>
1817// else:
1818// <else>
1819// <var> must be an identifier and <step> must be a small-int.
1820//
1821// Semantics of for-loop require:
1822// - final failing value should not be stored in the loop variable
1823// - if the loop never runs, the loop variable should never be assigned
1824// - assignments to <var>, <end> or <step> in the body do not alter the loop
1825// (<step> is a constant for us, so no need to worry about it changing)
1826//
1827// If <end> is a small-int, then the stack during the for-loop contains just
1828// the current value of <var>. Otherwise, the stack contains <end> then the
1829// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001830STATIC 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 +00001831 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001832
Damien George6f355fd2014-04-10 14:11:31 +01001833 uint top_label = comp_next_label(comp);
1834 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001835
Damien Georgee181c0d2014-12-12 17:19:56 +00001836 // put the end value on the stack if it's not a small-int constant
1837 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1838 if (end_on_stack) {
1839 compile_node(comp, pn_end);
1840 }
1841
1842 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001843 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001844
Damien Georgeb9791222014-01-23 00:34:21 +00001845 EMIT_ARG(jump, entry_label);
1846 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001847
Damien Georgec33ce602014-12-11 17:35:23 +00001848 // duplicate next value and store it to var
1849 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001850 c_assign(comp, pn_var, ASSIGN_STORE);
1851
Damienf3822fc2013-11-09 20:12:03 +00001852 // compile body
1853 compile_node(comp, pn_body);
1854
Damien Georgeb9791222014-01-23 00:34:21 +00001855 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001856
Damien Georgee181c0d2014-12-12 17:19:56 +00001857 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001858 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001859 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001860
Damien Georgeb9791222014-01-23 00:34:21 +00001861 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001862
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001863 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001864 if (end_on_stack) {
1865 EMIT(dup_top_two);
1866 EMIT(rot_two);
1867 } else {
1868 EMIT(dup_top);
1869 compile_node(comp, pn_end);
1870 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001871 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1872 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001873 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001874 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001875 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001876 }
Damien George63f38322015-02-28 15:04:06 +00001877 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001878
1879 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001880 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001881
1882 compile_node(comp, pn_else);
1883
Damien Georgeb9791222014-01-23 00:34:21 +00001884 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001885
1886 // discard final value of var that failed the loop condition
1887 EMIT(pop_top);
1888
1889 // discard <end> value if it's on the stack
1890 if (end_on_stack) {
1891 EMIT(pop_top);
1892 }
Damienf72fd0e2013-11-06 20:20:49 +00001893}
Damien George2ac4af62014-08-15 16:45:41 +01001894#endif
Damienf72fd0e2013-11-06 20:20:49 +00001895
Damien George969a6b32014-12-10 22:07:04 +00001896STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001897#if !MICROPY_EMIT_CPYTHON
1898 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1899 // this is actually slower, but uses no heap memory
1900 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001901 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 +00001902 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001903 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1904 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1905 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1906 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001907 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1908 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001909 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001910 mp_parse_node_t pn_range_start;
1911 mp_parse_node_t pn_range_end;
1912 mp_parse_node_t pn_range_step;
1913 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001914 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001915 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001916 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001917 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001918 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001919 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001920 } else if (n_args == 2) {
1921 pn_range_start = args[0];
1922 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001923 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001924 } else {
1925 pn_range_start = args[0];
1926 pn_range_end = args[1];
1927 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001928 // We need to know sign of step. This is possible only if it's constant
1929 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1930 optimize = false;
1931 }
Damienf72fd0e2013-11-06 20:20:49 +00001932 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001933 }
1934 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001935 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1936 return;
1937 }
1938 }
1939 }
1940#endif
1941
Damien Georgecbddb272014-02-01 20:08:18 +00001942 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001943 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001944
Damien George6f355fd2014-04-10 14:11:31 +01001945 uint pop_label = comp_next_label(comp);
1946 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001947
Damience89a212013-10-15 22:25:17 +01001948 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1949#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001950 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001951#endif
1952
Damien429d7192013-10-04 19:53:11 +01001953 compile_node(comp, pns->nodes[1]); // iterator
1954 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001955 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001956 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001957 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1958 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001959 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001960 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001961 }
Damien Georgeb9791222014-01-23 00:34:21 +00001962 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001963 EMIT(for_iter_end);
1964
1965 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001966 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001967
Damience89a212013-10-15 22:25:17 +01001968#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001969 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001970#endif
Damien429d7192013-10-04 19:53:11 +01001971
1972 compile_node(comp, pns->nodes[3]); // else (not tested)
1973
Damien Georgeb9791222014-01-23 00:34:21 +00001974 EMIT_ARG(label_assign, break_label);
1975 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001976}
1977
Damien George6be0b0a2014-08-15 14:30:52 +01001978STATIC 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 +01001979 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001980 uint l1 = comp_next_label(comp);
1981 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001982
Damien Georgeb9791222014-01-23 00:34:21 +00001983 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001984 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001985
Damien429d7192013-10-04 19:53:11 +01001986 compile_node(comp, pn_body); // body
1987 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001988 EMIT_ARG(jump, success_label); // jump over exception handler
1989
1990 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001991 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001992
Damien George6f355fd2014-04-10 14:11:31 +01001993 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001994
1995 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001996 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1997 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001998
1999 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01002000 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002001
Damiend99b0522013-12-21 18:17:45 +00002002 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002003 // this is a catch all exception handler
2004 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002005 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00002006 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002007 return;
2008 }
2009 } else {
2010 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00002011 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
2012 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
2013 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
2014 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01002015 // handler binds the exception to a local
2016 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002017 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002018 }
2019 }
2020 EMIT(dup_top);
2021 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002022 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00002023 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002024 }
2025
2026 EMIT(pop_top);
2027
2028 if (qstr_exception_local == 0) {
2029 EMIT(pop_top);
2030 } else {
Damien George542bd6b2015-03-26 14:42:40 +00002031 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002032 }
2033
2034 EMIT(pop_top);
2035
Damien George6f355fd2014-04-10 14:11:31 +01002036 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002037 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002038 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002039 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002040 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002041 }
2042 compile_node(comp, pns_except->nodes[1]);
2043 if (qstr_exception_local != 0) {
2044 EMIT(pop_block);
2045 }
2046 EMIT(pop_except);
2047 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002048 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2049 EMIT_ARG(label_assign, l3);
2050 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00002051 compile_store_id(comp, qstr_exception_local);
2052 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002053
Damien George8dcc0c72014-03-27 10:55:21 +00002054 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002055 EMIT(end_finally);
2056 }
Damien Georgeb9791222014-01-23 00:34:21 +00002057 EMIT_ARG(jump, l2);
2058 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002059 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002060 }
2061
Damien George8dcc0c72014-03-27 10:55:21 +00002062 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002063 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002064 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002065
Damien Georgeb9791222014-01-23 00:34:21 +00002066 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002067 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002068 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002069}
2070
Damien George6be0b0a2014-08-15 14:30:52 +01002071STATIC 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 +01002072 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002073
Damien Georgeb9791222014-01-23 00:34:21 +00002074 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002075 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002076
Damien429d7192013-10-04 19:53:11 +01002077 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002078 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002079 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002080 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002081 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002082 } else {
2083 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2084 }
2085 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002086 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2087 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002088 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002089
Damien George8dcc0c72014-03-27 10:55:21 +00002090 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002091 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002092}
2093
Damien George969a6b32014-12-10 22:07:04 +00002094STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00002095 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2096 {
Damiend99b0522013-12-21 18:17:45 +00002097 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2098 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002099 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002100 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2101 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002102 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002103 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002104 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002105 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002106 // no finally
2107 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2108 } else {
2109 // have finally
Damiend99b0522013-12-21 18:17:45 +00002110 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 +01002111 }
2112 } else {
2113 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002114 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002115 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002116 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002117 }
Damien429d7192013-10-04 19:53:11 +01002118 }
2119}
2120
Damien George6be0b0a2014-08-15 14:30:52 +01002121STATIC 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 +01002122 if (n == 0) {
2123 // no more pre-bits, compile the body of the with
2124 compile_node(comp, body);
2125 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002126 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002127 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002128 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002129 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002130 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002131 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002132 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2133 } else {
2134 // this pre-bit is just an expression
2135 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002136 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002137 EMIT(pop_top);
2138 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002139 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002140 // compile additional pre-bits and the body
2141 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2142 // finish this with block
2143 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002144 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2145 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002146 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002147 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002148 EMIT(end_finally);
2149 }
2150}
2151
Damien George969a6b32014-12-10 22:07:04 +00002152STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002153 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002154 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002155 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002156 assert(n > 0);
2157
2158 // compile in a nested fashion
2159 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2160}
2161
Damien George969a6b32014-12-10 22:07:04 +00002162STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002163 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002164 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2165 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00002166 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002167 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002168 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002169 EMIT(pop_top);
2170
Damien429d7192013-10-04 19:53:11 +01002171 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002172 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002173 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002174 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002175 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2176 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002177 // do nothing with a lonely constant
2178 } else {
2179 compile_node(comp, pns->nodes[0]); // just an expression
2180 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2181 }
Damien429d7192013-10-04 19:53:11 +01002182 }
2183 } else {
Damiend99b0522013-12-21 18:17:45 +00002184 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2185 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002186 if (kind == PN_expr_stmt_augassign) {
2187 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2188 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002189 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002190 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002191 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002192 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2193 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2194 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2195 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2196 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2197 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2198 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2199 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2200 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2201 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2202 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002203 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002204 }
Damien George7e5fb242014-02-01 22:18:47 +00002205 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002206 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2207 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002208 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2209 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002210 // following CPython, we store left-most first
2211 if (rhs > 0) {
2212 EMIT(dup_top);
2213 }
2214 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2215 for (int i = 0; i < rhs; i++) {
2216 if (i + 1 < rhs) {
2217 EMIT(dup_top);
2218 }
Damiend99b0522013-12-21 18:17:45 +00002219 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002220 }
Damien George0bb97132015-02-27 14:25:47 +00002221 } else {
2222 assert(kind == PN_expr_stmt_assign); // should be
Damien George42e0c592015-03-14 13:11:35 +00002223 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
2224 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002225 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2226 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2227 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002228 // optimisation for a, b = c, d; to match CPython's optimisation
Damien George4dea9222015-04-09 15:29:54 +00002229 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2230 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002231 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2232 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2233 // can't optimise when it's a star expression on the lhs
2234 goto no_optimisation;
2235 }
Damien429d7192013-10-04 19:53:11 +01002236 compile_node(comp, pns10->nodes[0]); // rhs
2237 compile_node(comp, pns10->nodes[1]); // rhs
2238 EMIT(rot_two);
2239 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2240 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00002241 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2242 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002243 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2244 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2245 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002246 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damien George4dea9222015-04-09 15:29:54 +00002247 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2248 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002249 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2250 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2251 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2252 // can't optimise when it's a star expression on the lhs
2253 goto no_optimisation;
2254 }
Damien429d7192013-10-04 19:53:11 +01002255 compile_node(comp, pns10->nodes[0]); // rhs
2256 compile_node(comp, pns10->nodes[1]); // rhs
2257 compile_node(comp, pns10->nodes[2]); // rhs
2258 EMIT(rot_three);
2259 EMIT(rot_two);
2260 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2261 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2262 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2263 } else {
Damien George495d7812014-04-08 17:51:47 +01002264 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002265 compile_node(comp, pns1->nodes[0]); // rhs
2266 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2267 }
Damien429d7192013-10-04 19:53:11 +01002268 }
2269 }
2270}
2271
Damien George6be0b0a2014-08-15 14:30:52 +01002272STATIC 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 +00002273 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002274 compile_node(comp, pns->nodes[0]);
2275 for (int i = 1; i < num_nodes; i += 1) {
2276 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002277 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002278 }
2279}
2280
Damien George969a6b32014-12-10 22:07:04 +00002281STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002282 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2283 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002284
Damien George6f355fd2014-04-10 14:11:31 +01002285 uint l_fail = comp_next_label(comp);
2286 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002287 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2288 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002289 EMIT_ARG(jump, l_end);
2290 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002291 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002292 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002293 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002294}
2295
Damien George969a6b32014-12-10 22:07:04 +00002296STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002297 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002298 //mp_parse_node_t pn_params = pns->nodes[0];
2299 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002300
Damien George36db6bc2014-05-07 17:24:22 +01002301 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002302 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002303 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 +01002304 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002305 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002306 }
2307
2308 // get the scope for this lambda
2309 scope_t *this_scope = (scope_t*)pns->nodes[2];
2310
2311 // make the lambda
2312 close_over_variables_etc(comp, this_scope, 0, 0);
2313}
2314
Damien George7711afb2015-02-28 15:10:18 +00002315STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002316 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002317 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002318 for (int i = 0; i < n; i += 1) {
2319 compile_node(comp, pns->nodes[i]);
2320 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002321 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002322 }
2323 }
Damien Georgeb9791222014-01-23 00:34:21 +00002324 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002325}
2326
Damien George7711afb2015-02-28 15:10:18 +00002327STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2328 compile_or_and_test(comp, pns, true);
2329}
2330
Damien George969a6b32014-12-10 22:07:04 +00002331STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002332 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002333}
2334
Damien George969a6b32014-12-10 22:07:04 +00002335STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002336 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002337 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002338}
2339
Damien George969a6b32014-12-10 22:07:04 +00002340STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002341 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002342 compile_node(comp, pns->nodes[0]);
2343 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002344 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002345 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002346 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002347 }
2348 for (int i = 1; i + 1 < num_nodes; i += 2) {
2349 compile_node(comp, pns->nodes[i + 1]);
2350 if (i + 2 < num_nodes) {
2351 EMIT(dup_top);
2352 EMIT(rot_three);
2353 }
Damien George7e5fb242014-02-01 22:18:47 +00002354 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002355 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002356 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002357 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2358 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2359 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2360 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2361 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2362 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002363 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002364 }
2365 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002366 } else {
2367 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002368 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2369 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002370 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002371 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002372 } else {
2373 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002374 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002375 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002376 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002377 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002378 }
Damien429d7192013-10-04 19:53:11 +01002379 }
Damien429d7192013-10-04 19:53:11 +01002380 }
2381 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002382 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002383 }
2384 }
2385 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002386 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002387 EMIT_ARG(jump, l_end);
2388 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002389 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002390 EMIT(rot_two);
2391 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002392 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002393 }
2394}
2395
Damien George969a6b32014-12-10 22:07:04 +00002396STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002397 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002398}
2399
Damien George969a6b32014-12-10 22:07:04 +00002400STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002401 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002402}
2403
Damien George969a6b32014-12-10 22:07:04 +00002404STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002405 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002406}
2407
Damien George969a6b32014-12-10 22:07:04 +00002408STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002409 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002410}
2411
Damien George969a6b32014-12-10 22:07:04 +00002412STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002413 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002414 compile_node(comp, pns->nodes[0]);
2415 for (int i = 1; i + 1 < num_nodes; i += 2) {
2416 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002417 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002418 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002419 } else {
Damien George0bb97132015-02-27 14:25:47 +00002420 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2421 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002422 }
2423 }
2424}
2425
Damien George969a6b32014-12-10 22:07:04 +00002426STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002427 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002428 compile_node(comp, pns->nodes[0]);
2429 for (int i = 1; i + 1 < num_nodes; i += 2) {
2430 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002431 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002432 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002433 } else {
Damien George0bb97132015-02-27 14:25:47 +00002434 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2435 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002436 }
2437 }
2438}
2439
Damien George969a6b32014-12-10 22:07:04 +00002440STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002441 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002442 compile_node(comp, pns->nodes[0]);
2443 for (int i = 1; i + 1 < num_nodes; i += 2) {
2444 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002445 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002446 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002447 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002448 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002449 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002450 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002451 } else {
Damien George0bb97132015-02-27 14:25:47 +00002452 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2453 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002454 }
2455 }
2456}
2457
Damien George969a6b32014-12-10 22:07:04 +00002458STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002459 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002460 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002461 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002462 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002463 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002464 } else {
Damien George0bb97132015-02-27 14:25:47 +00002465 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2466 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002467 }
2468}
2469
Damien George969a6b32014-12-10 22:07:04 +00002470STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002471 // this is to handle special super() call
2472 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2473
2474 compile_generic_all_nodes(comp, pns);
2475}
2476
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002477STATIC 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 +01002478 // function to call is on top of stack
2479
Damien George35e2a4e2014-02-05 00:51:47 +00002480#if !MICROPY_EMIT_CPYTHON
2481 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002482 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 +00002483 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002484 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002485 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002486 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002487 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002488 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002489 EMIT_ARG(call_function, 2, 0, 0);
2490 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002491 }
2492 }
Damien George01039b52014-12-21 17:44:27 +00002493 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002494 return;
2495 }
2496#endif
2497
Damien George2c0842b2014-04-27 16:46:51 +01002498 // get the list of arguments
2499 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002500 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002501
Damien George2c0842b2014-04-27 16:46:51 +01002502 // compile the arguments
2503 // Rather than calling compile_node on the list, we go through the list of args
2504 // explicitly here so that we can count the number of arguments and give sensible
2505 // error messages.
2506 int n_positional = n_positional_extra;
2507 uint n_keyword = 0;
2508 uint star_flags = 0;
2509 for (int i = 0; i < n_args; i++) {
2510 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2511 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2512 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2513 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2514 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2515 return;
2516 }
2517 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2518 compile_node(comp, pns_arg->nodes[0]);
2519 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2520 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2521 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2522 return;
2523 }
2524 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2525 compile_node(comp, pns_arg->nodes[0]);
2526 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2527 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2528 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2529 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2530 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2531 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2532 return;
2533 }
Damien George59fba2d2015-06-25 14:42:13 +00002534 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien George2c0842b2014-04-27 16:46:51 +01002535 compile_node(comp, pns2->nodes[0]);
2536 n_keyword += 1;
2537 } else {
2538 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2539 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2540 n_positional++;
2541 }
2542 } else {
2543 goto normal_argument;
2544 }
2545 } else {
2546 normal_argument:
2547 if (n_keyword > 0) {
2548 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2549 return;
2550 }
2551 compile_node(comp, args[i]);
2552 n_positional++;
2553 }
Damien429d7192013-10-04 19:53:11 +01002554 }
2555
Damien George2c0842b2014-04-27 16:46:51 +01002556 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002557 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002558 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002559 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002560 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002561 }
Damien429d7192013-10-04 19:53:11 +01002562}
2563
Damien George969a6b32014-12-10 22:07:04 +00002564STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002565 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002566 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002567 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 +01002568 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002569 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2570 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002571 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002572 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002573 i += 1;
2574 } else {
2575 compile_node(comp, pns->nodes[i]);
2576 }
Damien George35e2a4e2014-02-05 00:51:47 +00002577 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002578 }
2579}
2580
Damien George969a6b32014-12-10 22:07:04 +00002581STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002582 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002583 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002584}
2585
Damien George969a6b32014-12-10 22:07:04 +00002586STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002587 // a list of strings
Damien63321742013-12-10 17:41:49 +00002588
2589 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002590 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002591 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002592 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002593 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002594 int pn_kind;
2595 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2596 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2597 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2598 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2599 } else {
2600 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2601 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002602 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2603 pn_kind = MP_PARSE_NODE_STRING;
2604 } else {
2605 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2606 pn_kind = MP_PARSE_NODE_BYTES;
2607 }
Damien George40f3c022014-07-03 13:25:24 +01002608 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002609 }
Damien63321742013-12-10 17:41:49 +00002610 if (i == 0) {
2611 string_kind = pn_kind;
2612 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002613 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002614 return;
2615 }
Damien429d7192013-10-04 19:53:11 +01002616 }
Damien63321742013-12-10 17:41:49 +00002617
Damien George65ef6b72015-01-14 21:17:27 +00002618 // if we are not in the last pass, just load a dummy object
2619 if (comp->pass != MP_PASS_EMIT) {
2620 EMIT_ARG(load_const_obj, mp_const_none);
2621 return;
2622 }
2623
Damien63321742013-12-10 17:41:49 +00002624 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002625 vstr_t vstr;
2626 vstr_init_len(&vstr, n_bytes);
2627 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002628 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002629 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002630 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002631 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2632 memcpy(s_dest, s, s_len);
2633 s_dest += s_len;
2634 } else {
2635 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002636 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2637 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002638 }
Damien63321742013-12-10 17:41:49 +00002639 }
Damien George65ef6b72015-01-14 21:17:27 +00002640
2641 // load the object
Damien George05005f62015-01-21 22:48:37 +00002642 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 +01002643}
2644
2645// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002646STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002647 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2648 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2649 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002650
Damien George36db6bc2014-05-07 17:24:22 +01002651 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002652 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002653 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 +01002654 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002655 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002656 }
2657
2658 // get the scope for this comprehension
2659 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2660
2661 // compile the comprehension
2662 close_over_variables_etc(comp, this_scope, 0, 0);
2663
2664 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2665 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002666 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002667}
2668
Damien George969a6b32014-12-10 22:07:04 +00002669STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002670 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002671 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002672 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2673 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2674 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2675 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2676 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2677 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2678 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002679 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002680 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002681 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002682 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002683 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002684 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002685 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002686 // generator expression
2687 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2688 } else {
2689 // tuple with 2 items
2690 goto tuple_with_2_items;
2691 }
2692 } else {
2693 // tuple with 2 items
2694 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002695 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002696 }
2697 } else {
2698 // parenthesis around a single item, is just that item
2699 compile_node(comp, pns->nodes[0]);
2700 }
2701}
2702
Damien George969a6b32014-12-10 22:07:04 +00002703STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002704 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002705 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002706 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002707 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2708 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2709 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2710 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2711 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002712 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002713 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002714 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002715 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002716 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002717 // list of many items
2718 compile_node(comp, pns2->nodes[0]);
2719 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002720 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002721 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002722 // list comprehension
2723 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2724 } else {
2725 // list with 2 items
2726 goto list_with_2_items;
2727 }
2728 } else {
2729 // list with 2 items
2730 list_with_2_items:
2731 compile_node(comp, pns2->nodes[0]);
2732 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002733 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002734 }
2735 } else {
2736 // list with 1 item
2737 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002738 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002739 }
2740}
2741
Damien George969a6b32014-12-10 22:07:04 +00002742STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002743 mp_parse_node_t pn = pns->nodes[0];
2744 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002745 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002746 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002747 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2748 pns = (mp_parse_node_struct_t*)pn;
2749 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002750 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002751 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002752 compile_node(comp, pn);
2753 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002754 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2755 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2756 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2757 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002758 // dict/set with multiple elements
2759
2760 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002761 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002762 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002763
2764 // first element sets whether it's a dict or set
2765 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002766 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002767 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002768 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002769 compile_node(comp, pns->nodes[0]);
2770 EMIT(store_map);
2771 is_dict = true;
2772 } else {
2773 // a set
2774 compile_node(comp, pns->nodes[0]); // 1st value of set
2775 is_dict = false;
2776 }
2777
2778 // process rest of elements
2779 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002780 mp_parse_node_t pn_i = nodes[i];
2781 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2782 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002783 if (is_dict) {
2784 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002785 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002786 return;
2787 }
2788 EMIT(store_map);
2789 } else {
2790 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002791 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002792 return;
2793 }
2794 }
2795 }
2796
Damien Georgee37dcaa2014-12-27 17:07:16 +00002797 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002798 // if it's a set, build it
2799 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002800 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002801 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002802 #endif
Damien George0bb97132015-02-27 14:25:47 +00002803 } else {
2804 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002805 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002806 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002807 // a dictionary comprehension
2808 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2809 } else {
2810 // a set comprehension
2811 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2812 }
Damien429d7192013-10-04 19:53:11 +01002813 }
2814 } else {
2815 // set with one element
2816 goto set_with_one_element;
2817 }
2818 } else {
2819 // set with one element
2820 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002821 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002822 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002823 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002824 #else
2825 assert(0);
2826 #endif
Damien429d7192013-10-04 19:53:11 +01002827 }
2828}
2829
Damien George969a6b32014-12-10 22:07:04 +00002830STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002831 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002832}
2833
Damien George969a6b32014-12-10 22:07:04 +00002834STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002835 // object who's index we want is on top of stack
2836 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002837 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002838}
2839
Damien George969a6b32014-12-10 22:07:04 +00002840STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002841 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002842 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002843}
2844
Damien George83204f32014-12-27 17:20:41 +00002845#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002846STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002847 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2848 mp_parse_node_t pn = pns->nodes[0];
2849 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002850 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002851 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2852 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002853 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2854 pns = (mp_parse_node_struct_t*)pn;
2855 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002857 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002858 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002859 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002861 } else {
2862 // [?::x]
2863 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002864 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002865 }
Damiend99b0522013-12-21 18:17:45 +00002866 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002867 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002868 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2869 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2870 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2871 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002872 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002873 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002874 } else {
2875 // [?:x:x]
2876 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002877 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002878 }
2879 } else {
2880 // [?:x]
2881 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002882 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002883 }
2884 } else {
2885 // [?:x]
2886 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002887 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002888 }
2889}
2890
Damien George969a6b32014-12-10 22:07:04 +00002891STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002892 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002893 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2894 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002895}
2896
Damien George969a6b32014-12-10 22:07:04 +00002897STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002898 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002899 compile_subscript_3_helper(comp, pns);
2900}
Damien George83204f32014-12-27 17:20:41 +00002901#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002902
Damien George969a6b32014-12-10 22:07:04 +00002903STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002904 // if this is called then we are compiling a dict key:value pair
2905 compile_node(comp, pns->nodes[1]); // value
2906 compile_node(comp, pns->nodes[0]); // key
2907}
2908
Damien George969a6b32014-12-10 22:07:04 +00002909STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002910 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002911 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002912 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002913}
2914
Damien George969a6b32014-12-10 22:07:04 +00002915STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002916 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002917 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002918 return;
2919 }
Damiend99b0522013-12-21 18:17:45 +00002920 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002921 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002922 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002923 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2924 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002925 compile_node(comp, pns->nodes[0]);
2926 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002927 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002928 EMIT(yield_from);
2929 } else {
2930 compile_node(comp, pns->nodes[0]);
2931 EMIT(yield_value);
2932 }
2933}
2934
Damien George65ef6b72015-01-14 21:17:27 +00002935STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2936 // only create and load the actual str object on the last pass
2937 if (comp->pass != MP_PASS_EMIT) {
2938 EMIT_ARG(load_const_obj, mp_const_none);
2939 } else {
2940 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2941 }
2942}
2943
2944STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2945 // only create and load the actual bytes object on the last pass
2946 if (comp->pass != MP_PASS_EMIT) {
2947 EMIT_ARG(load_const_obj, mp_const_none);
2948 } else {
2949 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2950 }
2951}
2952
Damien George7d414a12015-02-08 01:57:40 +00002953STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2954 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2955}
2956
Damiend99b0522013-12-21 18:17:45 +00002957typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002958STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002959#define nc NULL
2960#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002961#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002962#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002963#undef nc
2964#undef c
2965#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002966 NULL,
2967 compile_string,
2968 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002969 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002970};
2971
Damien George6be0b0a2014-08-15 14:30:52 +01002972STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002973 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002974 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002975 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002976 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002977 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002978 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002979 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002980 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002981 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002982 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2983 case MP_PARSE_NODE_BYTES:
2984 // only create and load the actual bytes object on the last pass
2985 if (comp->pass != MP_PASS_EMIT) {
2986 EMIT_ARG(load_const_obj, mp_const_none);
2987 } else {
2988 mp_uint_t len;
2989 const byte *data = qstr_data(arg, &len);
2990 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2991 }
2992 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002993 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002994 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002995 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002996 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002997 // do nothing
2998 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002999 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01003000 }
3001 break;
Damien429d7192013-10-04 19:53:11 +01003002 }
3003 } else {
Damiend99b0522013-12-21 18:17:45 +00003004 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00003005 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00003006 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
3007 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01003008#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00003009 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
3010 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01003011#endif
Damien George65ef6b72015-01-14 21:17:27 +00003012 compile_syntax_error(comp, pn, "internal compiler error");
3013 } else {
3014 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01003015 }
3016 }
3017}
3018
Damien George2ac4af62014-08-15 16:45:41 +01003019STATIC 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 +01003020 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01003021 qstr param_name = MP_QSTR_NULL;
3022 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003023 if (MP_PARSE_NODE_IS_ID(pn)) {
3024 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01003025 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003026 // comes after a star, so counts as a keyword-only parameter
3027 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01003028 } else {
Damien George2827d622014-04-27 15:50:52 +01003029 // comes before a star, so counts as a positional parameter
3030 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003031 }
Damienb14de212013-10-06 00:28:28 +01003032 } else {
Damiend99b0522013-12-21 18:17:45 +00003033 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3034 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3035 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3036 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003037 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003038 // comes after a star, so counts as a keyword-only parameter
3039 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003040 } else {
Damien George2827d622014-04-27 15:50:52 +01003041 // comes before a star, so counts as a positional parameter
3042 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003043 }
Damiend99b0522013-12-21 18:17:45 +00003044 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003045 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003046 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003047 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003048 // bare star
3049 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003050 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003051 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003052 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003053 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003054 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00003055 } else {
3056 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01003057 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003058 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003059 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3060 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003061 }
Damien George0bb97132015-02-27 14:25:47 +00003062 } else {
3063 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00003064 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003065 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003066 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003067 }
Damien429d7192013-10-04 19:53:11 +01003068 }
3069
Damien George2827d622014-04-27 15:50:52 +01003070 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003071 bool added;
3072 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3073 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003074 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003075 return;
3076 }
Damien429d7192013-10-04 19:53:11 +01003077 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003078 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003079 }
3080}
3081
Damien George2827d622014-04-27 15:50:52 +01003082STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003083 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003084}
3085
Damien George2827d622014-04-27 15:50:52 +01003086STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003087 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3088}
3089
3090STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3091 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3092 // no annotation
3093 return;
3094 }
3095
3096 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3097 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3098 // named parameter with possible annotation
3099 // fallthrough
3100 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3101 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3102 // named star with possible annotation
3103 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3104 // fallthrough
3105 } else {
3106 // no annotation
3107 return;
3108 }
3109 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3110 // double star with possible annotation
3111 // fallthrough
3112 } else {
3113 // no annotation
3114 return;
3115 }
3116
3117 mp_parse_node_t pn_annotation = pns->nodes[1];
3118
3119 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3120 #if MICROPY_EMIT_NATIVE
3121 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3122 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3123 assert(id_info != NULL);
3124
3125 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3126 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3127 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3128 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3129 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003130 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003131 }
3132 }
3133 #endif // MICROPY_EMIT_NATIVE
3134 }
Damien429d7192013-10-04 19:53:11 +01003135}
3136
Damien George6be0b0a2014-08-15 14:30:52 +01003137STATIC 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 +01003138 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003139 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003140 // no more nested if/for; compile inner expression
3141 compile_node(comp, pn_inner_expr);
3142 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003143 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003144 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003145 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003146 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003147 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003148 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003149 #endif
Damien429d7192013-10-04 19:53:11 +01003150 } else {
3151 EMIT(yield_value);
3152 EMIT(pop_top);
3153 }
Damiend99b0522013-12-21 18:17:45 +00003154 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003155 // if condition
Damiend99b0522013-12-21 18:17:45 +00003156 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003157 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3158 pn_iter = pns_comp_if->nodes[1];
3159 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00003160 } else {
3161 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01003162 // for loop
Damiend99b0522013-12-21 18:17:45 +00003163 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003164 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003165 uint l_end2 = comp_next_label(comp);
3166 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003167 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003168 EMIT_ARG(label_assign, l_top2);
3169 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003170 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3171 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 +00003172 EMIT_ARG(jump, l_top2);
3173 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003174 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01003175 }
3176}
3177
Damien George1463c1f2014-04-25 23:52:57 +01003178STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3179#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003180 // see http://www.python.org/dev/peps/pep-0257/
3181
3182 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003183 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003184 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003185 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003186 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003187 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3188 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003189 for (int i = 0; i < num_nodes; i++) {
3190 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003191 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 +00003192 // not a newline, so this is the first statement; finish search
3193 break;
3194 }
3195 }
3196 // 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 +00003197 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003198 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003199 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003200 } else {
3201 return;
3202 }
3203
3204 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003205 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00003206 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003207 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3208 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3209 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3210 // compile the doc string
3211 compile_node(comp, pns->nodes[0]);
3212 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00003213 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003214 }
3215 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003216#else
3217 (void)comp;
3218 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003219#endif
Damien429d7192013-10-04 19:53:11 +01003220}
3221
Damien George1463c1f2014-04-25 23:52:57 +01003222STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003223 comp->pass = pass;
3224 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003225 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003226 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003227
Damien George36db6bc2014-05-07 17:24:22 +01003228 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003229 // reset maximum stack sizes in scope
3230 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003231 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003232 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003233 }
3234
Damien5ac1b2e2013-10-18 19:58:12 +01003235#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003236 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003237 scope_print_info(scope);
3238 }
Damien5ac1b2e2013-10-18 19:58:12 +01003239#endif
Damien429d7192013-10-04 19:53:11 +01003240
3241 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003242 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3243 assert(scope->kind == SCOPE_MODULE);
3244 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3245 compile_node(comp, pns->nodes[0]); // compile the expression
3246 EMIT(return_value);
3247 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003248 if (!comp->is_repl) {
3249 check_for_doc_string(comp, scope->pn);
3250 }
Damien429d7192013-10-04 19:53:11 +01003251 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003252 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003253 EMIT(return_value);
3254 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003255 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3256 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3257 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003258
3259 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003260 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003261 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003262 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003263 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003264 } else {
3265 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003266
Damien George2ac4af62014-08-15 16:45:41 +01003267 // argument annotations
3268 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3269
3270 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003271 mp_parse_node_t pn_annotation = pns->nodes[2];
3272 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3273 #if MICROPY_EMIT_NATIVE
3274 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3275 // nodes[2] can be null or a test-expr
3276 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3277 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3278 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3279 } else {
3280 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3281 }
Damien George2ac4af62014-08-15 16:45:41 +01003282 }
Damien Georgea5190a72014-08-15 22:39:08 +01003283 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003284 }
Damien George2ac4af62014-08-15 16:45:41 +01003285 }
Damien429d7192013-10-04 19:53:11 +01003286
3287 compile_node(comp, pns->nodes[3]); // 3 is function body
3288 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003289 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003290 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003291 EMIT(return_value);
3292 }
3293 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003294 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3295 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3296 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003297
3298 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003299 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003300 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003301 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003302 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3303 }
3304
3305 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003306
3307 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3308 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3309 EMIT(pop_top);
3310 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3311 }
Damien429d7192013-10-04 19:53:11 +01003312 EMIT(return_value);
3313 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3314 // a bit of a hack at the moment
3315
Damiend99b0522013-12-21 18:17:45 +00003316 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3317 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3318 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3319 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3320 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003321
Damien George708c0732014-04-27 19:23:46 +01003322 // We need a unique name for the comprehension argument (the iterator).
3323 // CPython uses .0, but we should be able to use anything that won't
3324 // clash with a user defined variable. Best to use an existing qstr,
3325 // so we use the blank qstr.
3326#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003327 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003328#else
3329 qstr qstr_arg = MP_QSTR_;
3330#endif
Damien George36db6bc2014-05-07 17:24:22 +01003331 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003332 bool added;
3333 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3334 assert(added);
3335 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003336 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003337 }
3338
3339 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003340 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003341 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003342 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003343 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003344 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003345 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003346 #endif
Damien429d7192013-10-04 19:53:11 +01003347 }
3348
Damien George6f355fd2014-04-10 14:11:31 +01003349 uint l_end = comp_next_label(comp);
3350 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00003351 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00003352 EMIT_ARG(label_assign, l_top);
3353 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003354 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3355 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003356 EMIT_ARG(jump, l_top);
3357 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003358 EMIT(for_iter_end);
3359
3360 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003361 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003362 }
3363 EMIT(return_value);
3364 } else {
3365 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003366 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3367 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3368 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003369
Damien George36db6bc2014-05-07 17:24:22 +01003370 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003371 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003372 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003373 assert(added);
3374 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003375 }
3376
Damien George542bd6b2015-03-26 14:42:40 +00003377 compile_load_id(comp, MP_QSTR___name__);
3378 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003379 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003380 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003381
3382 check_for_doc_string(comp, pns->nodes[2]);
3383 compile_node(comp, pns->nodes[2]); // 2 is class body
3384
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003385 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003386 assert(id != NULL);
3387 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003388 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003389 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003390#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003391 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003392#else
Damien George542bd6b2015-03-26 14:42:40 +00003393 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003394#endif
Damien429d7192013-10-04 19:53:11 +01003395 }
3396 EMIT(return_value);
3397 }
3398
Damien415eb6f2013-10-05 12:19:06 +01003399 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003400
3401 // make sure we match all the exception levels
3402 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003403}
3404
Damien George094d4502014-04-02 17:31:27 +01003405#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003406// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003407STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003408 comp->pass = pass;
3409 comp->scope_cur = scope;
3410 comp->next_label = 1;
3411
3412 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003413 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003414 return;
3415 }
3416
Damien George36db6bc2014-05-07 17:24:22 +01003417 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003418 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003419 }
3420
Damien826005c2013-10-05 23:17:28 +01003421 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003422 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3423 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3424 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003425
Damiend99b0522013-12-21 18:17:45 +00003426 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003427
Damiena2f2f7d2013-10-06 00:14:13 +01003428 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003429 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003430 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003431 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003432 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003433 if (comp->compile_error != MP_OBJ_NULL) {
3434 goto inline_asm_error;
3435 }
Damiena2f2f7d2013-10-06 00:14:13 +01003436 }
3437
Damiend99b0522013-12-21 18:17:45 +00003438 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003439
Damiend99b0522013-12-21 18:17:45 +00003440 mp_parse_node_t pn_body = pns->nodes[3]; // body
3441 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003442 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003443
Damien826005c2013-10-05 23:17:28 +01003444 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003445 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3446 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003447 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3448 // no instructions
3449 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003450 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003451 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003452 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003453 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003454 return;
3455 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003456
3457 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003458 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003459 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3460 goto not_an_instruction;
3461 }
Damiend99b0522013-12-21 18:17:45 +00003462 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003463 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3464 goto not_an_instruction;
3465 }
3466 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3467 goto not_an_instruction;
3468 }
3469 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3470 goto not_an_instruction;
3471 }
Damiend99b0522013-12-21 18:17:45 +00003472 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003473
3474 // parse node looks like an instruction
3475 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003476 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3477 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3478 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003479 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003480
3481 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003482 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003483 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003484 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003485 return;
3486 }
Damien George6f355fd2014-04-10 14:11:31 +01003487 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003488 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003489 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3490 compile_syntax_error(comp, nodes[i], "label redefined");
3491 return;
3492 }
Damien826005c2013-10-05 23:17:28 +01003493 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003494 } else if (op == MP_QSTR_align) {
3495 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003496 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003497 return;
3498 }
Damien George36db6bc2014-05-07 17:24:22 +01003499 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003500 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3501 }
3502 } else if (op == MP_QSTR_data) {
3503 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003504 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003505 return;
3506 }
Damien George36db6bc2014-05-07 17:24:22 +01003507 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003508 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003509 for (uint j = 1; j < n_args; j++) {
3510 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003511 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003512 return;
3513 }
Damien George50912e72015-01-20 11:55:10 +00003514 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003515 }
3516 }
Damien826005c2013-10-05 23:17:28 +01003517 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003518 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003519 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003520 }
3521 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003522
3523 if (comp->compile_error != MP_OBJ_NULL) {
3524 pns = pns2; // this is the parse node that had the error
3525 goto inline_asm_error;
3526 }
Damien826005c2013-10-05 23:17:28 +01003527 }
3528
Damien George36db6bc2014-05-07 17:24:22 +01003529 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003530 EMIT_INLINE_ASM(end_pass);
3531 }
3532
3533 if (comp->compile_error != MP_OBJ_NULL) {
3534 // inline assembler had an error; add traceback to its exception
3535 inline_asm_error:
3536 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 +01003537 }
Damien429d7192013-10-04 19:53:11 +01003538}
Damien George094d4502014-04-02 17:31:27 +01003539#endif
Damien429d7192013-10-04 19:53:11 +01003540
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003541STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003542#if !MICROPY_EMIT_CPYTHON
3543 // in Micro Python we put the *x parameter after all other parameters (except **y)
3544 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3545 id_info_t *id_param = NULL;
3546 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3547 id_info_t *id = &scope->id_info[i];
3548 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3549 if (id_param != NULL) {
3550 // swap star param with last param
3551 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3552 }
3553 break;
3554 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3555 id_param = id;
3556 }
3557 }
3558 }
3559#endif
3560
Damien429d7192013-10-04 19:53:11 +01003561 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003562 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003563 scope->num_locals = 0;
3564 for (int i = 0; i < scope->id_info_len; i++) {
3565 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003566 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003567 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3568 continue;
3569 }
3570 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3571 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3572 }
Damien George2827d622014-04-27 15:50:52 +01003573 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003574 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003575 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003576 }
3577 }
3578
3579 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003580#if MICROPY_EMIT_CPYTHON
3581 int num_cell = 0;
3582#endif
Damien9ecbcff2013-12-11 00:41:43 +00003583 for (int i = 0; i < scope->id_info_len; i++) {
3584 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003585#if MICROPY_EMIT_CPYTHON
3586 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003587 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003588 id->local_num = num_cell;
3589 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003590 }
Damien George6baf76e2013-12-30 22:32:17 +00003591#else
3592 // in Micro Python the cells come right after the fast locals
3593 // parameters are not counted here, since they remain at the start
3594 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003595 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003596 id->local_num = scope->num_locals;
3597 scope->num_locals += 1;
3598 }
3599#endif
Damien9ecbcff2013-12-11 00:41:43 +00003600 }
Damien9ecbcff2013-12-11 00:41:43 +00003601
3602 // compute the index of free vars (freevars[idx] in CPython)
3603 // make sure they are in the order of the parent scope
3604 if (scope->parent != NULL) {
3605 int num_free = 0;
3606 for (int i = 0; i < scope->parent->id_info_len; i++) {
3607 id_info_t *id = &scope->parent->id_info[i];
3608 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3609 for (int j = 0; j < scope->id_info_len; j++) {
3610 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003611 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003612 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003613#if MICROPY_EMIT_CPYTHON
3614 // in CPython the frees are numbered after the cells
3615 id2->local_num = num_cell + num_free;
3616#else
3617 // in Micro Python the frees come first, before the params
3618 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003619#endif
3620 num_free += 1;
3621 }
3622 }
3623 }
Damien429d7192013-10-04 19:53:11 +01003624 }
Damien George6baf76e2013-12-30 22:32:17 +00003625#if !MICROPY_EMIT_CPYTHON
3626 // in Micro Python shift all other locals after the free locals
3627 if (num_free > 0) {
3628 for (int i = 0; i < scope->id_info_len; i++) {
3629 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003630 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003631 id->local_num += num_free;
3632 }
3633 }
Damien George2827d622014-04-27 15:50:52 +01003634 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 +00003635 scope->num_locals += num_free;
3636 }
3637#endif
Damien429d7192013-10-04 19:53:11 +01003638 }
3639
Damien George8725f8f2014-02-15 19:33:11 +00003640 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003641
3642#if MICROPY_EMIT_CPYTHON
3643 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003644 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) {
3645 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003646 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003647 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003648 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 +00003649 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003650 }
3651 }
Damien George882b3632014-04-02 15:56:31 +01003652#endif
3653
Damien429d7192013-10-04 19:53:11 +01003654 int num_free = 0;
3655 for (int i = 0; i < scope->id_info_len; i++) {
3656 id_info_t *id = &scope->id_info[i];
3657 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3658 num_free += 1;
3659 }
3660 }
3661 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003662 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003663 }
3664}
3665
Damien George65cad122014-04-06 11:48:15 +01003666mp_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 +01003667 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003668 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003669 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003670 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003671
Damien George62a3a282015-03-01 12:04:05 +00003672 // create the module scope
3673 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3674
3675 // optimise constants (scope must be set for error messages to work)
3676 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003677 mp_map_t consts;
3678 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003679 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003680 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003681
Damien Georgea210c772015-03-26 15:49:53 +00003682 // create standard emitter; it's used at least for MP_PASS_SCOPE
3683 #if MICROPY_EMIT_CPYTHON
3684 emit_t *emit_cpython = emit_cpython_new();
3685 #else
3686 emit_t *emit_bc = emit_bc_new();
3687 #endif
3688
Damien826005c2013-10-05 23:17:28 +01003689 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003690 #if MICROPY_EMIT_CPYTHON
3691 comp->emit = emit_cpython;
3692 comp->emit_method_table = &emit_cpython_method_table;
3693 #else
3694 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003695 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003696 comp->emit_method_table = &emit_bc_method_table;
3697 #endif
Damien George41125902015-03-26 16:44:14 +00003698 #endif
Damien Georgea77ffe62015-03-14 12:59:31 +00003699 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003700 comp->emit_inline_asm = NULL;
3701 comp->emit_inline_asm_method_table = NULL;
Damien Georgea77ffe62015-03-14 12:59:31 +00003702 #endif
Damien826005c2013-10-05 23:17:28 +01003703 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003704 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003705 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003706#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003707 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003708 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003709#endif
Damien826005c2013-10-05 23:17:28 +01003710 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003711 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003712 }
3713
3714 // update maximim number of labels needed
3715 if (comp->next_label > max_num_labels) {
3716 max_num_labels = comp->next_label;
3717 }
Damien429d7192013-10-04 19:53:11 +01003718 }
3719
Damien826005c2013-10-05 23:17:28 +01003720 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003721 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 +00003722 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003723 }
3724
Damien Georgea210c772015-03-26 15:49:53 +00003725 // set max number of labels now that it's calculated
3726 #if MICROPY_EMIT_CPYTHON
3727 emit_cpython_set_max_num_labels(emit_cpython, max_num_labels);
3728 #else
3729 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
3730 #endif
3731
Damien826005c2013-10-05 23:17:28 +01003732 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003733#if !MICROPY_EMIT_CPYTHON
Damien Georgee67ed5d2014-01-04 13:55:24 +00003734#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003735 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003736#endif
Damien3ef4abb2013-10-12 16:53:13 +01003737#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003738 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003739#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003740#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003741 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003742 if (false) {
3743 // dummy
3744
Damien3ef4abb2013-10-12 16:53:13 +01003745#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003746 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003747 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003748 if (emit_inline_thumb == NULL) {
3749 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3750 }
3751 comp->emit = NULL;
3752 comp->emit_method_table = NULL;
3753 comp->emit_inline_asm = emit_inline_thumb;
3754 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003755 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003756 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003757 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003758 }
Damienc025ebb2013-10-12 14:30:21 +01003759#endif
3760
Damien826005c2013-10-05 23:17:28 +01003761 } else {
Damienc025ebb2013-10-12 14:30:21 +01003762
3763 // choose the emit type
3764
Damien3ef4abb2013-10-12 16:53:13 +01003765#if MICROPY_EMIT_CPYTHON
Damien Georgea210c772015-03-26 15:49:53 +00003766 comp->emit = emit_cpython;
Damienc025ebb2013-10-12 14:30:21 +01003767 comp->emit_method_table = &emit_cpython_method_table;
3768#else
Damien826005c2013-10-05 23:17:28 +01003769 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003770
3771#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003772 case MP_EMIT_OPT_NATIVE_PYTHON:
3773 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003774#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003775 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003776 emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003777 }
Damien13ed3a62013-10-08 09:05:10 +01003778 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003779#elif MICROPY_EMIT_X86
3780 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003781 emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels);
Damien Georgec90f59e2014-09-06 23:06:36 +01003782 }
3783 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003784#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003785 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003786 emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels);
Damienc025ebb2013-10-12 14:30:21 +01003787 }
3788 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003789#elif MICROPY_EMIT_ARM
3790 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003791 emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003792 }
3793 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003794#endif
3795 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003796 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003797 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003798#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003799
Damien826005c2013-10-05 23:17:28 +01003800 default:
Damien826005c2013-10-05 23:17:28 +01003801 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003802 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003803 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003804 #endif
Damien826005c2013-10-05 23:17:28 +01003805 break;
3806 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003807#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003808
Damien George1e1779e2015-01-14 00:20:28 +00003809 // need a pass to compute stack size
3810 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3811
Damien George36db6bc2014-05-07 17:24:22 +01003812 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003813 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003814 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3815 }
3816
3817 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003818 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003819 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003820 }
Damien6cdd3af2013-10-05 18:08:26 +01003821 }
Damien429d7192013-10-04 19:53:11 +01003822 }
3823
Damien George41d02b62014-01-24 22:42:28 +00003824 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003825
3826#if MICROPY_EMIT_CPYTHON
3827 emit_cpython_free(emit_cpython);
3828#else
3829 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003830#if MICROPY_EMIT_NATIVE
3831 if (emit_native != NULL) {
3832#if MICROPY_EMIT_X64
3833 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003834#elif MICROPY_EMIT_X86
3835 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003836#elif MICROPY_EMIT_THUMB
3837 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003838#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003839 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003840#endif
3841 }
3842#endif
3843#if MICROPY_EMIT_INLINE_THUMB
3844 if (emit_inline_thumb != NULL) {
3845 emit_inline_thumb_free(emit_inline_thumb);
3846 }
3847#endif
Damien Georgea210c772015-03-26 15:49:53 +00003848#endif // MICROPY_EMIT_CPYTHON
Damien George41d02b62014-01-24 22:42:28 +00003849
Damien George52b5d762014-09-23 15:31:56 +00003850 // free the parse tree
Damien George62a3a282015-03-01 12:04:05 +00003851 mp_parse_node_free(module_scope->pn);
Damien George52b5d762014-09-23 15:31:56 +00003852
Damien George41d02b62014-01-24 22:42:28 +00003853 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003854 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003855 for (scope_t *s = module_scope; s;) {
3856 scope_t *next = s->next;
3857 scope_free(s);
3858 s = next;
3859 }
Damien5ac1b2e2013-10-18 19:58:12 +01003860
Damien George41d02b62014-01-24 22:42:28 +00003861 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003862 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003863 m_del_obj(compiler_t, comp);
3864
Damien Georgea91ac202014-10-05 19:01:34 +01003865 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003866 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003867 } else {
3868#if MICROPY_EMIT_CPYTHON
3869 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003870 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003871 return mp_const_true;
3872#else
3873 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003874 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003875#endif
3876 }
Damien429d7192013-10-04 19:53:11 +01003877}