blob: a124203e4bc9787adf6b4eea06cb1d95942ffea3 [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
Damien Georgecfc4c332015-07-29 22:16:01 +000084 mp_obj_t compile_error; // set to an exception object if there's an error
85 mp_uint_t compile_error_line; // set to best guess of line of 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 Georgecfc4c332015-07-29 22:16:01 +0000111STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
112 // if the line of the error is unknown then try to update it from the pn
113 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
114 comp->compile_error_line = (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100115 }
Damien George84d59c22015-07-27 22:20:00 +0100116}
117
118STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000119 // only register the error if there has been no other error
120 if (comp->compile_error == MP_OBJ_NULL) {
121 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
122 compile_error_set_line(comp, pn);
123 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000124}
125
Damien Georgeddd1e182015-01-10 14:07:24 +0000126#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100127STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300128 #if MICROPY_PY_UCTYPES
129 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
130 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100131 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100132 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100133};
Damien Georgeddd1e182015-01-10 14:07:24 +0000134STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
135#endif
Damien George57e99eb2014-04-10 22:42:11 +0100136
Damien Georgeffae48d2014-05-08 15:58:39 +0000137// this function is essentially a simple preprocessor
138STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
139 if (0) {
140 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100141#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000142 } else if (MP_PARSE_NODE_IS_ID(pn)) {
143 // lookup identifier in table of dynamic constants
144 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
145 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
146 if (elem != NULL) {
147 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
148 }
149#endif
150 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000151 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100152
Damien Georgeffae48d2014-05-08 15:58:39 +0000153 // fold some parse nodes before folding their arguments
154 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100155#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000156 case PN_expr_stmt:
157 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
158 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
159 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
160 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
161 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
162 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
163 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
164 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
165 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
166 ) {
167 // code to assign dynamic constants: id = const(value)
168
169 // get the id
170 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
171
172 // get the value
173 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
174 pn_value = fold_constants(comp, pn_value, consts);
175 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
176 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
177 break;
178 }
Damien George40f3c022014-07-03 13:25:24 +0100179 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000180
181 // store the value in the table of dynamic constants
182 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
183 if (elem->value != MP_OBJ_NULL) {
184 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
185 break;
186 }
187 elem->value = MP_OBJ_NEW_SMALL_INT(value);
188
189 // replace const(value) with value
190 pns1->nodes[0] = pn_value;
191
192 // finished folding this assignment
193 return pn;
194 }
195 }
196 }
197 break;
198#endif
Damien George5042bce2014-05-25 22:06:06 +0100199 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000200 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000201 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100202 return pn;
Damien429d7192013-10-04 19:53:11 +0100203 }
204
Damien Georgeffae48d2014-05-08 15:58:39 +0000205 // fold arguments
206 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
207 for (int i = 0; i < n; i++) {
208 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
209 }
210
211 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000212 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100213 case PN_atom_paren:
214 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
215 // (int)
216 pn = pns->nodes[0];
217 }
218 break;
219
220 case PN_expr:
221 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
222 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100223 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
224 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100225 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
226 }
227 break;
228
229 case PN_and_expr:
230 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
231 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100232 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
233 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100234 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
235 }
236 break;
237
Damien429d7192013-10-04 19:53:11 +0100238 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000239 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100240 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
241 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000242 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100243 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000244 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 +0100245 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
246 }
Damien George0bb97132015-02-27 14:25:47 +0000247 } else {
248 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100249 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000250 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200251 // Shifting to big amounts is underfined behavior
252 // in C and is CPU-dependent; propagate sign bit.
253 arg1 = BITS_PER_WORD - 1;
254 }
Damiend99b0522013-12-21 18:17:45 +0000255 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100256 }
257 }
258 break;
259
260 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100261 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000262 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100263 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
264 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000265 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100266 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000267 arg0 += arg1;
Damien George0bb97132015-02-27 14:25:47 +0000268 } else {
269 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100270 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000271 arg0 -= arg1;
Damien0efb3a12013-10-12 16:16:56 +0100272 }
Damien Georged1e355e2014-05-28 14:51:12 +0100273 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000274 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100275 }
276 }
277 break;
278
279 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000280 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 +0100281 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
282 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000283 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100284 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000285 if (!mp_small_int_mul_overflow(arg0, arg1)) {
286 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100287 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000288 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
289 }
290 }
Damiend99b0522013-12-21 18:17:45 +0000291 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100292 // int / int
293 // pass
Damiend99b0522013-12-21 18:17:45 +0000294 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100295 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000296 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damien George0bb97132015-02-27 14:25:47 +0000297 } else {
298 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300299 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100300 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000301 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 +0300302 }
Damien429d7192013-10-04 19:53:11 +0100303 }
304 }
305 break;
306
307 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000308 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100309 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000310 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
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);
313 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100314 // -int
Damiend99b0522013-12-21 18:17:45 +0000315 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
Damien George0bb97132015-02-27 14:25:47 +0000316 } else {
317 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100318 // ~int
Damiend99b0522013-12-21 18:17:45 +0000319 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100320 }
321 }
322 break;
323
324 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100325 if (0) {
326#if MICROPY_EMIT_CPYTHON
327 } 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 +0100328 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100329 // can overflow; enabled only to compare with CPython
Damien George4dea9222015-04-09 15:29:54 +0000330 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
Damiend99b0522013-12-21 18:17:45 +0000331 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200332 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100333 if (power >= 0) {
334 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200335 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100336 for (; power > 0; power--) {
337 ans *= base;
338 }
Damiend99b0522013-12-21 18:17:45 +0000339 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100340 }
341 }
Damien George57e99eb2014-04-10 22:42:11 +0100342#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000343#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100344 } 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])) {
345 // id.id
346 // look it up in constant table, see if it can be replaced with an integer
Damien George4dea9222015-04-09 15:29:54 +0000347 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George57e99eb2014-04-10 22:42:11 +0100348 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
349 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
350 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
351 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
352 if (elem != NULL) {
353 mp_obj_t dest[2];
354 mp_load_method_maybe(elem->value, q_attr, dest);
355 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100356 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000357 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100358 }
359 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000360#endif
Damien429d7192013-10-04 19:53:11 +0100361 }
362 break;
363 }
364 }
365
366 return pn;
367}
368
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200369STATIC 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 +0100370STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000371STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100372
Damien George6f355fd2014-04-10 14:11:31 +0100373STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100374 return comp->next_label++;
375}
376
Damien George8dcc0c72014-03-27 10:55:21 +0000377STATIC void compile_increase_except_level(compiler_t *comp) {
378 comp->cur_except_level += 1;
379 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
380 comp->scope_cur->exc_stack_size = comp->cur_except_level;
381 }
382}
383
384STATIC void compile_decrease_except_level(compiler_t *comp) {
385 assert(comp->cur_except_level > 0);
386 comp->cur_except_level -= 1;
387}
388
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200389STATIC 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 +0100390 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100391 scope->parent = comp->scope_cur;
392 scope->next = NULL;
393 if (comp->scope_head == NULL) {
394 comp->scope_head = scope;
395 } else {
396 scope_t *s = comp->scope_head;
397 while (s->next != NULL) {
398 s = s->next;
399 }
400 s->next = scope;
401 }
402 return scope;
403}
404
Damien George91bc32d2015-04-09 15:31:53 +0000405typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
406
407STATIC 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 +0000408 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000409 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
410 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100411 for (int i = 0; i < num_nodes; i++) {
412 f(comp, pns->nodes[i]);
413 }
Damiend99b0522013-12-21 18:17:45 +0000414 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100415 f(comp, pn);
416 }
417}
418
Damien George969a6b32014-12-10 22:07:04 +0000419STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000420 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100421 for (int i = 0; i < num_nodes; i++) {
422 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000423 if (comp->compile_error != MP_OBJ_NULL) {
424 // add line info for the error in case it didn't have a line number
425 compile_error_set_line(comp, pns->nodes[i]);
426 return;
427 }
Damien429d7192013-10-04 19:53:11 +0100428 }
429}
430
Damien George542bd6b2015-03-26 14:42:40 +0000431STATIC void compile_load_id(compiler_t *comp, qstr qst) {
432 if (comp->pass == MP_PASS_SCOPE) {
433 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
434 } else {
Damien George41125902015-03-26 16:44:14 +0000435 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000436 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000437 #else
438 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
439 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000440 }
441}
442
443STATIC void compile_store_id(compiler_t *comp, qstr qst) {
444 if (comp->pass == MP_PASS_SCOPE) {
445 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
446 } else {
Damien George41125902015-03-26 16:44:14 +0000447 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000448 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000449 #else
450 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
451 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000452 }
453}
454
455STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
456 if (comp->pass == MP_PASS_SCOPE) {
457 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
458 } else {
Damien George41125902015-03-26 16:44:14 +0000459 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000460 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000461 #else
462 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
463 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000464 }
465}
466
Damien3ef4abb2013-10-12 16:53:13 +0100467#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200468STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100469 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
470 return true;
471 }
Damien George4c81ba82015-01-13 16:21:23 +0000472 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
473 return true;
474 }
Damien George7d414a12015-02-08 01:57:40 +0000475 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
476 return true;
477 }
Damiend99b0522013-12-21 18:17:45 +0000478 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100479 return false;
480 }
Damiend99b0522013-12-21 18:17:45 +0000481 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100482 return false;
483 }
484 return true;
485}
486
Damien George5042bce2014-05-25 22:06:06 +0100487STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000488 bool has_single_quote = false;
489 bool has_double_quote = false;
490 for (int i = 0; i < len; i++) {
491 if (str[i] == '\'') {
492 has_single_quote = true;
493 } else if (str[i] == '"') {
494 has_double_quote = true;
495 }
496 }
497 if (bytes) {
498 vstr_printf(vstr, "b");
499 }
500 bool quote_single = false;
501 if (has_single_quote && !has_double_quote) {
502 vstr_printf(vstr, "\"");
503 } else {
504 quote_single = true;
505 vstr_printf(vstr, "'");
506 }
507 for (int i = 0; i < len; i++) {
508 if (str[i] == '\n') {
509 vstr_printf(vstr, "\\n");
510 } else if (str[i] == '\\') {
511 vstr_printf(vstr, "\\\\");
512 } else if (str[i] == '\'' && quote_single) {
513 vstr_printf(vstr, "\\'");
514 } else {
515 vstr_printf(vstr, "%c", str[i]);
516 }
517 }
518 if (has_single_quote && !has_double_quote) {
519 vstr_printf(vstr, "\"");
520 } else {
521 vstr_printf(vstr, "'");
522 }
523}
524
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200525STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000526 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 +0100527 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000528 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 +0100529 return;
530 }
531
Damien George7d414a12015-02-08 01:57:40 +0000532 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
533 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
534 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
535 return;
536 }
537
Damiend99b0522013-12-21 18:17:45 +0000538 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200539 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
540 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
541 return;
542 }
543
Damien George42f3de92014-10-03 17:44:14 +0000544 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000545 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
546 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100547 case MP_PARSE_NODE_STRING:
548 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100549 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100550 const byte *str = qstr_data(arg, &len);
551 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
552 break;
553 }
Damiend99b0522013-12-21 18:17:45 +0000554 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100555 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000556 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
557 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
558 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100559 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100560 }
561 break;
562 default: assert(0);
563 }
564}
565
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200566STATIC 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 +0100567 int n = 0;
568 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000569 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100570 }
571 int total = n;
572 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000573 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100574 total += 1;
Damien3a205172013-10-12 15:01:56 +0100575 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100576 is_const = false;
577 }
578 }
579 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100580 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100581 is_const = false;
582 break;
583 }
584 }
585 if (total > 0 && is_const) {
586 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000587 vstr_t *vstr = vstr_new();
588 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000589 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000590 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100591 need_comma = true;
592 }
593 for (int i = 0; i < n; i++) {
594 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000595 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100596 }
Damien02f89412013-12-12 15:13:36 +0000597 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100598 need_comma = true;
599 }
600 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000601 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100602 } else {
Damien02f89412013-12-12 15:13:36 +0000603 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100604 }
Damien George0d3cb672015-01-28 23:43:01 +0000605 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000606 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100607 } else {
Damiend99b0522013-12-21 18:17:45 +0000608 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100609 compile_node(comp, pn);
610 }
611 for (int i = 0; i < n; i++) {
612 compile_node(comp, pns_list->nodes[i]);
613 }
Damien Georgeb9791222014-01-23 00:34:21 +0000614 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100615 }
616}
Damien3a205172013-10-12 15:01:56 +0100617#endif
618
619// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100620STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100621#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100622 cpython_c_tuple(comp, pn, pns_list);
623#else
624 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000625 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100626 compile_node(comp, pn);
627 total += 1;
628 }
629 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000630 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100631 for (int i = 0; i < n; i++) {
632 compile_node(comp, pns_list->nodes[i]);
633 }
634 total += n;
635 }
Damien Georgeb9791222014-01-23 00:34:21 +0000636 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100637#endif
638}
Damien429d7192013-10-04 19:53:11 +0100639
Damien George969a6b32014-12-10 22:07:04 +0000640STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100641 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000642 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100643}
644
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200645STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000646 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
647 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100648}
649
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200650STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000651 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
652 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100653}
654
Damien3ef4abb2013-10-12 16:53:13 +0100655#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100656// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200657STATIC 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 +0100658 if (node_is_const_false(pn)) {
659 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000660 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100661 }
662 return;
663 } else if (node_is_const_true(pn)) {
664 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000665 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100666 }
667 return;
Damiend99b0522013-12-21 18:17:45 +0000668 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
669 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
670 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
671 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100672 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100673 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100674 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100675 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100676 }
Damien3a205172013-10-12 15:01:56 +0100677 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000678 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100679 } else {
680 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100681 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100682 }
683 }
684 return;
Damiend99b0522013-12-21 18:17:45 +0000685 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100686 if (jump_if == false) {
687 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100688 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100689 }
690 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100691 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100692 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100693 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100694 }
Damien3a205172013-10-12 15:01:56 +0100695 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000696 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100697 }
698 return;
Damiend99b0522013-12-21 18:17:45 +0000699 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100700 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100701 return;
702 }
703 }
704
705 // nothing special, fall back to default compiling for node and jump
706 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000707 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100708}
Damien3a205172013-10-12 15:01:56 +0100709#endif
Damien429d7192013-10-04 19:53:11 +0100710
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200711STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100712#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100713 cpython_c_if_cond(comp, pn, jump_if, label, false);
714#else
715 if (node_is_const_false(pn)) {
716 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000717 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100718 }
719 return;
720 } else if (node_is_const_true(pn)) {
721 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000722 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100723 }
724 return;
Damiend99b0522013-12-21 18:17:45 +0000725 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
726 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
727 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
728 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100729 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000730 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100731 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100732 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000733 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100734 }
Damien George0b2fd912015-02-28 14:37:54 +0000735 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000736 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100737 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000738 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100739 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000740 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100741 }
742 }
743 return;
Damiend99b0522013-12-21 18:17:45 +0000744 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100745 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000746 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100747 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000748 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100749 }
Damiend99b0522013-12-21 18:17:45 +0000750 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100751 c_if_cond(comp, pns->nodes[0], !jump_if, label);
752 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100753 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
754 // cond is something in parenthesis
755 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
756 // empty tuple, acts as false for the condition
757 if (jump_if == false) {
758 EMIT_ARG(jump, label);
759 }
760 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
761 // non-empty tuple, acts as true for the condition
762 if (jump_if == true) {
763 EMIT_ARG(jump, label);
764 }
765 } else {
766 // parenthesis around 1 item, is just that item
767 c_if_cond(comp, pns->nodes[0], jump_if, label);
768 }
769 return;
Damien3a205172013-10-12 15:01:56 +0100770 }
771 }
772
773 // nothing special, fall back to default compiling for node and jump
774 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000775 EMIT_ARG(pop_jump_if, jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100776#endif
Damien429d7192013-10-04 19:53:11 +0100777}
778
779typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100780STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100781
Damien George6be0b0a2014-08-15 14:30:52 +0100782STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100783 if (assign_kind != ASSIGN_AUG_STORE) {
784 compile_node(comp, pns->nodes[0]);
785 }
786
Damiend99b0522013-12-21 18:17:45 +0000787 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
788 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
789 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
790 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100791 if (assign_kind != ASSIGN_AUG_STORE) {
792 for (int i = 0; i < n - 1; i++) {
793 compile_node(comp, pns1->nodes[i]);
794 }
795 }
Damiend99b0522013-12-21 18:17:45 +0000796 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
797 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100798 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000799 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100800 if (assign_kind == ASSIGN_AUG_STORE) {
801 EMIT(rot_three);
802 EMIT(store_subscr);
803 } else {
804 compile_node(comp, pns1->nodes[0]);
805 if (assign_kind == ASSIGN_AUG_LOAD) {
806 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100807 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100808 } else {
809 EMIT(store_subscr);
810 }
811 }
Damiend99b0522013-12-21 18:17:45 +0000812 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
813 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100814 if (assign_kind == ASSIGN_AUG_LOAD) {
815 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000816 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100817 } else {
818 if (assign_kind == ASSIGN_AUG_STORE) {
819 EMIT(rot_two);
820 }
Damien Georgeb9791222014-01-23 00:34:21 +0000821 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100822 }
823 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100824 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100825 }
826 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100827 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100828 }
829
Damiend99b0522013-12-21 18:17:45 +0000830 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100831 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100832 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100833
834 return;
835
836cannot_assign:
837 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100838}
839
Damien George0288cf02014-04-11 11:53:00 +0000840// 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 +0100841STATIC 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 +0000842 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
843
844 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000845 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000846 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
847 EMIT_ARG(unpack_ex, 0, num_tail);
848 have_star_index = 0;
849 }
Damien George963a5a32015-01-16 17:47:07 +0000850 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000851 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000852 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000853 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
854 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100855 } else {
Damien George9d181f62014-04-27 16:55:27 +0100856 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100857 return;
858 }
859 }
860 }
Damien George963a5a32015-01-16 17:47:07 +0000861 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000862 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100863 }
Damien George0288cf02014-04-11 11:53:00 +0000864 if (num_head != 0) {
865 if (0 == have_star_index) {
866 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100867 } else {
Damien George0288cf02014-04-11 11:53:00 +0000868 c_assign(comp, node_head, ASSIGN_STORE);
869 }
870 }
Damien George963a5a32015-01-16 17:47:07 +0000871 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000872 if (num_head + i == have_star_index) {
873 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
874 } else {
875 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100876 }
877 }
878}
879
880// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100881STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100882 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000883 assert(!MP_PARSE_NODE_IS_NULL(pn));
884 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000885 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000886 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100887 switch (assign_kind) {
888 case ASSIGN_STORE:
889 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000890 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100891 break;
892 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000893 default:
Damien George542bd6b2015-03-26 14:42:40 +0000894 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100895 break;
896 }
897 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100898 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100899 return;
900 }
901 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000902 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000903 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
904 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100905 case PN_power:
906 // lhs is an index or attribute
907 c_assign_power(comp, pns, assign_kind);
908 break;
909
910 case PN_testlist_star_expr:
911 case PN_exprlist:
912 // lhs is a tuple
913 if (assign_kind != ASSIGN_STORE) {
914 goto bad_aug;
915 }
Damien George0288cf02014-04-11 11:53:00 +0000916 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100917 break;
918
919 case PN_atom_paren:
920 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000921 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100922 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100923 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000924 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000925 if (assign_kind != ASSIGN_STORE) {
926 goto bad_aug;
927 }
Damiend99b0522013-12-21 18:17:45 +0000928 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100929 goto testlist_comp;
930 } else {
931 // parenthesis around 1 item, is just that item
932 pn = pns->nodes[0];
933 goto tail_recursion;
934 }
935 break;
936
937 case PN_atom_bracket:
938 // lhs is something in brackets
939 if (assign_kind != ASSIGN_STORE) {
940 goto bad_aug;
941 }
Damiend99b0522013-12-21 18:17:45 +0000942 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100943 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000944 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000945 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
946 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100947 goto testlist_comp;
948 } else {
949 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000950 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100951 }
952 break;
953
954 default:
Damien George9d181f62014-04-27 16:55:27 +0100955 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100956 }
957 return;
958
959 testlist_comp:
960 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000961 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
962 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
963 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100964 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000965 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000966 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000967 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100968 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000969 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
970 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000971 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100972 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100973 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100974 } else {
975 // sequence with 2 items
976 goto sequence_with_2_items;
977 }
978 } else {
979 // sequence with 2 items
980 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000981 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100982 }
983 return;
984 }
985 return;
986
Damien George9d181f62014-04-27 16:55:27 +0100987 cannot_assign:
988 compile_syntax_error(comp, pn, "can't assign to expression");
989 return;
990
Damien429d7192013-10-04 19:53:11 +0100991 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100992 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100993}
994
995// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100996// if we are not in CPython compatibility mode then:
997// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
998// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
999// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +01001000STATIC 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 +01001001 assert(n_pos_defaults >= 0);
1002 assert(n_kw_defaults >= 0);
1003
Damien429d7192013-10-04 19:53:11 +01001004 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +00001005 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +01001006 int nfree = 0;
1007 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +00001008 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
1009 id_info_t *id = &comp->scope_cur->id_info[i];
1010 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
1011 for (int j = 0; j < this_scope->id_info_len; j++) {
1012 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01001013 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +00001014#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +01001015 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001016#else
1017 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +00001018 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001019#endif
Damien318aec62013-12-10 18:28:17 +00001020 nfree += 1;
1021 }
1022 }
Damien429d7192013-10-04 19:53:11 +01001023 }
1024 }
1025 }
Damien429d7192013-10-04 19:53:11 +01001026
1027 // make the function/closure
1028 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001029 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001030 } else {
Damien George3558f622014-04-20 17:50:40 +01001031 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001032 }
1033}
1034
Damien George6be0b0a2014-08-15 14:30:52 +01001035STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001036 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001037 comp->have_star = true;
1038 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001039 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1040 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001041 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001042 } else {
1043 // named star
Damien429d7192013-10-04 19:53:11 +01001044 }
Damien George8b19db02014-04-11 23:25:34 +01001045 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001046
1047 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001048 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001049 // TODO do we need to do anything with this?
1050
1051 } else {
1052 mp_parse_node_t pn_id;
1053 mp_parse_node_t pn_colon;
1054 mp_parse_node_t pn_equal;
1055 if (MP_PARSE_NODE_IS_ID(pn)) {
1056 // this parameter is just an id
1057
1058 pn_id = pn;
1059 pn_colon = MP_PARSE_NODE_NULL;
1060 pn_equal = MP_PARSE_NODE_NULL;
1061
Damien George0bb97132015-02-27 14:25:47 +00001062 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001063 // this parameter has a colon and/or equal specifier
1064
Damien George0bb97132015-02-27 14:25:47 +00001065 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1066
Damien Georgef41fdd02014-03-03 23:19:11 +00001067 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1068 pn_id = pns->nodes[0];
1069 pn_colon = pns->nodes[1];
1070 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +00001071 }
1072
1073 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1074 // this parameter does not have a default value
1075
1076 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001077 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001078 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001079 return;
1080 }
1081
1082 } else {
1083 // this parameter has a default value
1084 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1085
Damien George8b19db02014-04-11 23:25:34 +01001086 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001087 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001088#if MICROPY_EMIT_CPYTHON
Damien George59fba2d2015-06-25 14:42:13 +00001089 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien Georgef0778a72014-06-07 22:01:00 +01001090 compile_node(comp, pn_equal);
1091#else
Damien George69b89d22014-04-11 13:38:30 +00001092 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1093 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001094 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1095 // we need to do this here before we start building the map for the default keywords
1096 if (comp->num_default_params > 0) {
1097 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001098 } else {
1099 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001100 }
Damien George69b89d22014-04-11 13:38:30 +00001101 // first default dict param, so make the map
1102 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001103 }
Damien Georgef0778a72014-06-07 22:01:00 +01001104
1105 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001106 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +00001107 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +00001108 EMIT(store_map);
1109#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001110 } else {
Damien George69b89d22014-04-11 13:38:30 +00001111 comp->num_default_params += 1;
1112 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001113 }
1114 }
1115
1116 // TODO pn_colon not implemented
1117 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001118 }
1119}
1120
1121// leaves function object on stack
1122// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001123STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001124 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001125 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001126 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001127 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001128 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001129 }
1130
1131 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001132 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001133 uint old_num_dict_params = comp->num_dict_params;
1134 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001135
1136 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001137 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001138 comp->num_dict_params = 0;
1139 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001140 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001141
Damien Georgea91ac202014-10-05 19:01:34 +01001142 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001143 return MP_QSTR_NULL;
1144 }
1145
Damien Georgee337f1e2014-03-31 15:18:37 +01001146#if !MICROPY_EMIT_CPYTHON
1147 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001148 // the default keywords args may have already made the tuple; if not, do it now
1149 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001150 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001151 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001152 }
1153#endif
1154
Damien429d7192013-10-04 19:53:11 +01001155 // get the scope for this function
1156 scope_t *fscope = (scope_t*)pns->nodes[4];
1157
1158 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001159 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001160
1161 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001162 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001163 comp->num_dict_params = old_num_dict_params;
1164 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001165
1166 // return its name (the 'f' in "def f(...):")
1167 return fscope->simple_name;
1168}
1169
1170// leaves class object on stack
1171// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001172STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001173 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001174 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001175 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001176 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001177 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001178 }
1179
1180 EMIT(load_build_class);
1181
1182 // scope for this class
1183 scope_t *cscope = (scope_t*)pns->nodes[3];
1184
1185 // compile the class
1186 close_over_variables_etc(comp, cscope, 0, 0);
1187
1188 // get its name
Damien George59fba2d2015-06-25 14:42:13 +00001189 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001190
1191 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001192 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1193 mp_parse_node_t parents = pns->nodes[1];
1194 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1195 parents = MP_PARSE_NODE_NULL;
1196 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001197 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001198 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001199
1200 // return its name (the 'C' in class C(...):")
1201 return cscope->simple_name;
1202}
1203
Damien6cdd3af2013-10-05 18:08:26 +01001204// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001205STATIC 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 +00001206 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001207 return false;
1208 }
1209
1210 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001211 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001212 return true;
1213 }
1214
Damiend99b0522013-12-21 18:17:45 +00001215 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001216 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001217 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001218#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001219 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001220 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001221 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001222 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001223#endif
Damien3ef4abb2013-10-12 16:53:13 +01001224#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001225 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001226 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001227#endif
Damien6cdd3af2013-10-05 18:08:26 +01001228 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001229 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001230 }
1231
1232 return true;
1233}
1234
Damien George969a6b32014-12-10 22:07:04 +00001235STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001236 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001237 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001238 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001239
Damien6cdd3af2013-10-05 18:08:26 +01001240 // inherit emit options for this function/class definition
1241 uint emit_options = comp->scope_cur->emit_options;
1242
1243 // compile each decorator
1244 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001245 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001246 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1247 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001248
1249 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001250 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001251 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001252
1253 // check for built-in decorators
1254 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1255 // this was a built-in
1256 num_built_in_decorators += 1;
1257
1258 } else {
1259 // not a built-in, compile normally
1260
1261 // compile the decorator function
1262 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001263 for (int j = 1; j < name_len; j++) {
1264 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1265 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001266 }
1267
1268 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001269 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001270 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001271 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001272 compile_node(comp, pns_decorator->nodes[1]);
1273 }
Damien429d7192013-10-04 19:53:11 +01001274 }
1275 }
1276
1277 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001278 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001279 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001280 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001281 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001282 } else {
Damien George0bb97132015-02-27 14:25:47 +00001283 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1284 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001285 }
1286
1287 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001288 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001289 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001290 }
1291
1292 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +00001293 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +01001294}
1295
Damien George969a6b32014-12-10 22:07:04 +00001296STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001297 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001298 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +00001299 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +01001300}
1301
Damien George6be0b0a2014-08-15 14:30:52 +01001302STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001303 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00001304 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001305 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1306 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001307
1308 compile_node(comp, pns->nodes[0]); // base of the power node
1309
Damiend99b0522013-12-21 18:17:45 +00001310 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1311 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1312 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1313 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001314 for (int i = 0; i < n - 1; i++) {
1315 compile_node(comp, pns1->nodes[i]);
1316 }
Damiend99b0522013-12-21 18:17:45 +00001317 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1318 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001319 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001320 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001321 compile_node(comp, pns1->nodes[0]);
1322 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001323 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1324 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001325 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001326 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001327 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001328 }
1329 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001330 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001331 }
1332
Damiend99b0522013-12-21 18:17:45 +00001333 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001334 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001335 }
Damiend99b0522013-12-21 18:17:45 +00001336 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1337 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1338 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1339 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001340 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1341
Damiend99b0522013-12-21 18:17:45 +00001342 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1343 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1344 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001345 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001346 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001347 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001348 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001349 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001350 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001351 c_del_stmt(comp, pns->nodes[0]);
1352 for (int i = 0; i < n; i++) {
1353 c_del_stmt(comp, pns1->nodes[i]);
1354 }
Damiend99b0522013-12-21 18:17:45 +00001355 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001356 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001357 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001358 } else {
1359 // sequence with 2 items
1360 goto sequence_with_2_items;
1361 }
1362 } else {
1363 // sequence with 2 items
1364 sequence_with_2_items:
1365 c_del_stmt(comp, pns->nodes[0]);
1366 c_del_stmt(comp, pns->nodes[1]);
1367 }
1368 } else {
1369 // tuple with 1 element
1370 c_del_stmt(comp, pn);
1371 }
1372 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001373 // TODO is there anything else to implement?
1374 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001375 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001376
1377 return;
1378
1379cannot_delete:
1380 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001381}
1382
Damien George969a6b32014-12-10 22:07:04 +00001383STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001384 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1385}
1386
Damien George969a6b32014-12-10 22:07:04 +00001387STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001388 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001389 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' 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(break_loop, comp->break_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_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001396 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001397 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001398 }
Damien George090c9232014-10-17 14:08:49 +00001399 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001400 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001401}
1402
Damien George969a6b32014-12-10 22:07:04 +00001403STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001404 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001405 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001406 return;
1407 }
Damiend99b0522013-12-21 18:17:45 +00001408 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001409 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001410 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001411 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001412 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001413 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1414 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 +01001415
Damien George6f355fd2014-04-10 14:11:31 +01001416 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001417 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1418 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1419 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001420 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001421 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1422 } else {
1423 compile_node(comp, pns->nodes[0]);
1424 }
1425 EMIT(return_value);
1426}
1427
Damien George969a6b32014-12-10 22:07:04 +00001428STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001429 compile_node(comp, pns->nodes[0]);
1430 EMIT(pop_top);
1431}
1432
Damien George969a6b32014-12-10 22:07:04 +00001433STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001434 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001435 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001436 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001437 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001438 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001439 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001440 compile_node(comp, pns->nodes[0]);
1441 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001442 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001443 } else {
1444 // raise x
1445 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001446 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001447 }
1448}
1449
Damien George635543c2014-04-10 12:56:52 +01001450// q_base holds the base of the name
1451// eg a -> q_base=a
1452// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001453STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001454 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001455 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1456 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001457 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001458 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001459 pn = pns->nodes[0];
1460 is_as = true;
1461 }
Damien George635543c2014-04-10 12:56:52 +01001462 if (MP_PARSE_NODE_IS_NULL(pn)) {
1463 // empty name (eg, from . import x)
1464 *q_base = MP_QSTR_;
1465 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1466 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001467 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001468 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001469 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001470 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001471 }
Damien George635543c2014-04-10 12:56:52 +01001472 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001473 } else {
1474 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001475 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001476 {
Damien429d7192013-10-04 19:53:11 +01001477 // a name of the form a.b.c
1478 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001479 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001480 }
Damiend99b0522013-12-21 18:17:45 +00001481 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001482 int len = n - 1;
1483 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001484 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001485 }
Damien George55baff42014-01-21 21:40:13 +00001486 byte *q_ptr;
1487 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001488 for (int i = 0; i < n; i++) {
1489 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001490 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001491 }
Damien George39dc1452014-10-03 19:52:22 +01001492 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001493 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001494 memcpy(str_dest, str_src, str_src_len);
1495 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001496 }
Damien George635543c2014-04-10 12:56:52 +01001497 qstr q_full = qstr_build_end(q_ptr);
1498 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001499 if (is_as) {
1500 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001501 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001502 }
1503 }
Damien429d7192013-10-04 19:53:11 +01001504 }
Damien429d7192013-10-04 19:53:11 +01001505 }
1506}
1507
Damien George6be0b0a2014-08-15 14:30:52 +01001508STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001509 EMIT_ARG(load_const_small_int, 0); // level 0 import
1510 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1511 qstr q_base;
1512 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001513 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001514}
1515
Damien George969a6b32014-12-10 22:07:04 +00001516STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001517 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1518}
1519
Damien George969a6b32014-12-10 22:07:04 +00001520STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001521 mp_parse_node_t pn_import_source = pns->nodes[0];
1522
1523 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1524 uint import_level = 0;
1525 do {
1526 mp_parse_node_t pn_rel;
1527 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)) {
1528 // This covers relative imports with dots only like "from .. import"
1529 pn_rel = pn_import_source;
1530 pn_import_source = MP_PARSE_NODE_NULL;
1531 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1532 // This covers relative imports starting with dot(s) like "from .foo import"
1533 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1534 pn_rel = pns_2b->nodes[0];
1535 pn_import_source = pns_2b->nodes[1];
1536 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1537 } else {
1538 // Not a relative import
1539 break;
1540 }
1541
1542 // get the list of . and/or ...'s
1543 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001544 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001545
1546 // count the total number of .'s
1547 for (int i = 0; i < n; i++) {
1548 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1549 import_level++;
1550 } else {
1551 // should be an MP_TOKEN_ELLIPSIS
1552 import_level += 3;
1553 }
1554 }
1555 } while (0);
1556
Damiend99b0522013-12-21 18:17:45 +00001557 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001558 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001559
1560 // build the "fromlist" tuple
1561#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001562 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001563#else
Damien George59fba2d2015-06-25 14:42:13 +00001564 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001565 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001566#endif
1567
1568 // do the import
Damien George635543c2014-04-10 12:56:52 +01001569 qstr dummy_q;
1570 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001571 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001572
Damien429d7192013-10-04 19:53:11 +01001573 } else {
Damien George635543c2014-04-10 12:56:52 +01001574 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001575
1576 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001577 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001578 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001579#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001580 {
1581 vstr_t *vstr = vstr_new();
1582 vstr_printf(vstr, "(");
1583 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001584 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1585 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1586 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001587 if (i > 0) {
1588 vstr_printf(vstr, ", ");
1589 }
1590 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001591 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001592 const byte *str = qstr_data(id2, &len);
1593 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001594 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001595 }
Damien02f89412013-12-12 15:13:36 +00001596 if (n == 1) {
1597 vstr_printf(vstr, ",");
1598 }
1599 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001600 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001601 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001602 }
Damiendb4c3612013-12-10 17:27:24 +00001603#else
1604 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001605 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1606 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1607 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001608 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001609 }
Damien Georgeb9791222014-01-23 00:34:21 +00001610 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001611#endif
1612
1613 // do the import
Damien George635543c2014-04-10 12:56:52 +01001614 qstr dummy_q;
1615 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001616 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001617 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1618 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1619 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001620 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001621 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001622 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001623 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001624 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001625 }
1626 }
1627 EMIT(pop_top);
1628 }
1629}
1630
Damien George584ba672014-12-21 17:26:45 +00001631STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1632 bool added;
1633 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1634 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001635 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001636 compile_syntax_error(comp, pn, "identifier already used");
1637 return;
1638 }
1639 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1640
1641 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1642 id_info = scope_find_global(comp->scope_cur, qst);
1643 if (id_info != NULL) {
1644 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1645 }
1646}
1647
Damien George969a6b32014-12-10 22:07:04 +00001648STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001649 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001650 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001651 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001652 for (int i = 0; i < n; i++) {
1653 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001654 }
1655 }
1656}
1657
Damien George584ba672014-12-21 17:26:45 +00001658STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1659 bool added;
1660 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1661 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001662 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001663 compile_syntax_error(comp, pn, "identifier already used");
1664 return;
1665 }
1666 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1667 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)) {
1668 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1669 return;
1670 }
1671 id_info->kind = ID_INFO_KIND_FREE;
1672 scope_close_over_in_parents(comp->scope_cur, qst);
1673}
1674
Damien George969a6b32014-12-10 22:07:04 +00001675STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001676 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001677 if (comp->scope_cur->kind == SCOPE_MODULE) {
1678 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1679 return;
1680 }
1681 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001682 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001683 for (int i = 0; i < n; i++) {
1684 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001685 }
1686 }
1687}
1688
Damien George969a6b32014-12-10 22:07:04 +00001689STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001690 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001691 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001692 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001693 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001694 // assertion message
1695 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001696 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001697 }
Damien Georgeb9791222014-01-23 00:34:21 +00001698 EMIT_ARG(raise_varargs, 1);
1699 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001700}
1701
Damien George969a6b32014-12-10 22:07:04 +00001702STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001703 // TODO proper and/or short circuiting
1704
Damien George6f355fd2014-04-10 14:11:31 +01001705 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001706
Damien George391db862014-10-17 17:57:33 +00001707 // optimisation: don't emit anything when "if False" (not in CPython)
1708 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1709 uint l_fail = comp_next_label(comp);
1710 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001711
Damien George391db862014-10-17 17:57:33 +00001712 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001713
Damien George391db862014-10-17 17:57:33 +00001714 // optimisation: skip everything else when "if True" (not in CPython)
1715 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1716 goto done;
1717 }
1718
1719 if (
1720 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1721 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1722 // optimisation: don't jump if last instruction was return
1723 && !EMIT(last_emit_was_return_value)
1724 ) {
1725 // jump over elif/else blocks
1726 EMIT_ARG(jump, l_end);
1727 }
1728
1729 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001730 }
1731
Damien George235f9b32014-10-17 17:30:16 +00001732 // compile elif blocks (if any)
1733 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001734 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 +00001735 for (int i = 0; i < n_elif; i++) {
1736 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1737 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001738
Damien George391db862014-10-17 17:57:33 +00001739 // optimisation: don't emit anything when "if False" (not in CPython)
1740 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1741 uint l_fail = comp_next_label(comp);
1742 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001743
Damien George391db862014-10-17 17:57:33 +00001744 compile_node(comp, pns_elif->nodes[1]); // elif block
1745
1746 // optimisation: skip everything else when "elif True" (not in CPython)
1747 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1748 goto done;
1749 }
1750
1751 // optimisation: don't jump if last instruction was return
1752 if (!EMIT(last_emit_was_return_value)) {
1753 EMIT_ARG(jump, l_end);
1754 }
1755 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001756 }
1757 }
1758
1759 // compile else block
1760 compile_node(comp, pns->nodes[3]); // can be null
1761
Damien George391db862014-10-17 17:57:33 +00001762done:
Damien Georgeb9791222014-01-23 00:34:21 +00001763 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001764}
1765
Damien Georgecbddb272014-02-01 20:08:18 +00001766#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001767 uint16_t old_break_label = comp->break_label; \
1768 uint16_t old_continue_label = comp->continue_label; \
1769 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001770 uint break_label = comp_next_label(comp); \
1771 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001772 comp->break_label = break_label; \
1773 comp->continue_label = continue_label; \
1774 comp->break_continue_except_level = comp->cur_except_level;
1775
1776#define END_BREAK_CONTINUE_BLOCK \
1777 comp->break_label = old_break_label; \
1778 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001779 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001780
Damien George969a6b32014-12-10 22:07:04 +00001781STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001782 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001783
Damience89a212013-10-15 22:25:17 +01001784 // compared to CPython, we have an optimised version of while loops
1785#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001786 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001787 EMIT_ARG(setup_loop, break_label);
1788 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001789 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1790 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001791 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001792 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001793 }
Damien Georgeb9791222014-01-23 00:34:21 +00001794 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001795 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1796 // this is a small hack to agree with CPython
1797 if (!node_is_const_true(pns->nodes[0])) {
1798 EMIT(pop_block);
1799 }
Damience89a212013-10-15 22:25:17 +01001800#else
Damien George391db862014-10-17 17:57:33 +00001801 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1802 uint top_label = comp_next_label(comp);
1803 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1804 EMIT_ARG(jump, continue_label);
1805 }
1806 EMIT_ARG(label_assign, top_label);
1807 compile_node(comp, pns->nodes[1]); // body
1808 EMIT_ARG(label_assign, continue_label);
1809 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1810 }
Damience89a212013-10-15 22:25:17 +01001811#endif
1812
1813 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001814 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001815
1816 compile_node(comp, pns->nodes[2]); // else
1817
Damien Georgeb9791222014-01-23 00:34:21 +00001818 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001819}
1820
Damien George2ac4af62014-08-15 16:45:41 +01001821#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001822// This function compiles an optimised for-loop of the form:
1823// for <var> in range(<start>, <end>, <step>):
1824// <body>
1825// else:
1826// <else>
1827// <var> must be an identifier and <step> must be a small-int.
1828//
1829// Semantics of for-loop require:
1830// - final failing value should not be stored in the loop variable
1831// - if the loop never runs, the loop variable should never be assigned
1832// - assignments to <var>, <end> or <step> in the body do not alter the loop
1833// (<step> is a constant for us, so no need to worry about it changing)
1834//
1835// If <end> is a small-int, then the stack during the for-loop contains just
1836// the current value of <var>. Otherwise, the stack contains <end> then the
1837// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001838STATIC 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 +00001839 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001840
Damien George6f355fd2014-04-10 14:11:31 +01001841 uint top_label = comp_next_label(comp);
1842 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001843
Damien Georgee181c0d2014-12-12 17:19:56 +00001844 // put the end value on the stack if it's not a small-int constant
1845 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1846 if (end_on_stack) {
1847 compile_node(comp, pn_end);
1848 }
1849
1850 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001851 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001852
Damien Georgeb9791222014-01-23 00:34:21 +00001853 EMIT_ARG(jump, entry_label);
1854 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001855
Damien Georgec33ce602014-12-11 17:35:23 +00001856 // duplicate next value and store it to var
1857 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001858 c_assign(comp, pn_var, ASSIGN_STORE);
1859
Damienf3822fc2013-11-09 20:12:03 +00001860 // compile body
1861 compile_node(comp, pn_body);
1862
Damien Georgeb9791222014-01-23 00:34:21 +00001863 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001864
Damien Georgee181c0d2014-12-12 17:19:56 +00001865 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001866 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001867 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001868
Damien Georgeb9791222014-01-23 00:34:21 +00001869 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001870
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001871 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001872 if (end_on_stack) {
1873 EMIT(dup_top_two);
1874 EMIT(rot_two);
1875 } else {
1876 EMIT(dup_top);
1877 compile_node(comp, pn_end);
1878 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001879 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1880 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001881 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001882 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001883 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001884 }
Damien George63f38322015-02-28 15:04:06 +00001885 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001886
1887 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001888 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001889
1890 compile_node(comp, pn_else);
1891
Damien Georgeb9791222014-01-23 00:34:21 +00001892 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001893
1894 // discard final value of var that failed the loop condition
1895 EMIT(pop_top);
1896
1897 // discard <end> value if it's on the stack
1898 if (end_on_stack) {
1899 EMIT(pop_top);
1900 }
Damienf72fd0e2013-11-06 20:20:49 +00001901}
Damien George2ac4af62014-08-15 16:45:41 +01001902#endif
Damienf72fd0e2013-11-06 20:20:49 +00001903
Damien George969a6b32014-12-10 22:07:04 +00001904STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001905#if !MICROPY_EMIT_CPYTHON
1906 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1907 // this is actually slower, but uses no heap memory
1908 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001909 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 +00001910 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001911 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1912 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1913 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1914 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001915 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1916 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001917 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001918 mp_parse_node_t pn_range_start;
1919 mp_parse_node_t pn_range_end;
1920 mp_parse_node_t pn_range_step;
1921 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001922 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001923 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001924 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001925 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001926 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001927 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001928 } else if (n_args == 2) {
1929 pn_range_start = args[0];
1930 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001931 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001932 } else {
1933 pn_range_start = args[0];
1934 pn_range_end = args[1];
1935 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001936 // We need to know sign of step. This is possible only if it's constant
1937 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1938 optimize = false;
1939 }
Damienf72fd0e2013-11-06 20:20:49 +00001940 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001941 }
1942 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001943 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1944 return;
1945 }
1946 }
1947 }
1948#endif
1949
Damien Georgecbddb272014-02-01 20:08:18 +00001950 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001951 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001952
Damien George6f355fd2014-04-10 14:11:31 +01001953 uint pop_label = comp_next_label(comp);
1954 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001955
Damience89a212013-10-15 22:25:17 +01001956 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1957#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001958 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001959#endif
1960
Damien429d7192013-10-04 19:53:11 +01001961 compile_node(comp, pns->nodes[1]); // iterator
1962 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001963 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001964 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001965 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1966 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001967 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001968 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001969 }
Damien Georgeb9791222014-01-23 00:34:21 +00001970 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001971 EMIT(for_iter_end);
1972
1973 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001974 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001975
Damience89a212013-10-15 22:25:17 +01001976#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001977 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001978#endif
Damien429d7192013-10-04 19:53:11 +01001979
1980 compile_node(comp, pns->nodes[3]); // else (not tested)
1981
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(label_assign, break_label);
1983 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001984}
1985
Damien George6be0b0a2014-08-15 14:30:52 +01001986STATIC 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 +01001987 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001988 uint l1 = comp_next_label(comp);
1989 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001990
Damien Georgeb9791222014-01-23 00:34:21 +00001991 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001992 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001993
Damien429d7192013-10-04 19:53:11 +01001994 compile_node(comp, pn_body); // body
1995 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001996 EMIT_ARG(jump, success_label); // jump over exception handler
1997
1998 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001999 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00002000
Damien George6f355fd2014-04-10 14:11:31 +01002001 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002002
2003 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00002004 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
2005 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01002006
2007 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01002008 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002009
Damiend99b0522013-12-21 18:17:45 +00002010 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002011 // this is a catch all exception handler
2012 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002013 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00002014 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002015 return;
2016 }
2017 } else {
2018 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00002019 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
2020 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
2021 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
2022 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01002023 // handler binds the exception to a local
2024 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002025 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002026 }
2027 }
2028 EMIT(dup_top);
2029 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002030 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00002031 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002032 }
2033
2034 EMIT(pop_top);
2035
2036 if (qstr_exception_local == 0) {
2037 EMIT(pop_top);
2038 } else {
Damien George542bd6b2015-03-26 14:42:40 +00002039 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002040 }
2041
2042 EMIT(pop_top);
2043
Damien George6f355fd2014-04-10 14:11:31 +01002044 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002045 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002046 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002047 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002048 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002049 }
2050 compile_node(comp, pns_except->nodes[1]);
2051 if (qstr_exception_local != 0) {
2052 EMIT(pop_block);
2053 }
2054 EMIT(pop_except);
2055 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002056 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2057 EMIT_ARG(label_assign, l3);
2058 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00002059 compile_store_id(comp, qstr_exception_local);
2060 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002061
Damien George8dcc0c72014-03-27 10:55:21 +00002062 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002063 EMIT(end_finally);
2064 }
Damien Georgeb9791222014-01-23 00:34:21 +00002065 EMIT_ARG(jump, l2);
2066 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002067 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002068 }
2069
Damien George8dcc0c72014-03-27 10:55:21 +00002070 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002071 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002072 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002073
Damien Georgeb9791222014-01-23 00:34:21 +00002074 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002075 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002076 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002077}
2078
Damien George6be0b0a2014-08-15 14:30:52 +01002079STATIC 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 +01002080 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002081
Damien Georgeb9791222014-01-23 00:34:21 +00002082 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002083 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002084
Damien429d7192013-10-04 19:53:11 +01002085 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002086 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002087 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002088 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002089 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002090 } else {
2091 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2092 }
2093 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002094 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2095 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002096 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002097
Damien George8dcc0c72014-03-27 10:55:21 +00002098 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002099 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002100}
2101
Damien George969a6b32014-12-10 22:07:04 +00002102STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00002103 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2104 {
Damiend99b0522013-12-21 18:17:45 +00002105 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2106 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002107 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002108 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2109 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002110 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002111 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002112 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002113 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002114 // no finally
2115 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2116 } else {
2117 // have finally
Damiend99b0522013-12-21 18:17:45 +00002118 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 +01002119 }
2120 } else {
2121 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002122 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002123 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002124 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002125 }
Damien429d7192013-10-04 19:53:11 +01002126 }
2127}
2128
Damien George6be0b0a2014-08-15 14:30:52 +01002129STATIC 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 +01002130 if (n == 0) {
2131 // no more pre-bits, compile the body of the with
2132 compile_node(comp, body);
2133 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002134 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002135 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002136 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002137 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002138 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002139 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002140 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2141 } else {
2142 // this pre-bit is just an expression
2143 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002144 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002145 EMIT(pop_top);
2146 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002147 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002148 // compile additional pre-bits and the body
2149 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2150 // finish this with block
2151 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002152 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2153 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002154 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002155 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002156 EMIT(end_finally);
2157 }
2158}
2159
Damien George969a6b32014-12-10 22:07:04 +00002160STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002161 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002162 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002163 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002164 assert(n > 0);
2165
2166 // compile in a nested fashion
2167 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2168}
2169
Damien George969a6b32014-12-10 22:07:04 +00002170STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002171 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002172 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2173 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00002174 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002175 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002176 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002177 EMIT(pop_top);
2178
Damien429d7192013-10-04 19:53:11 +01002179 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002180 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002181 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002182 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002183 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2184 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002185 // do nothing with a lonely constant
2186 } else {
2187 compile_node(comp, pns->nodes[0]); // just an expression
2188 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2189 }
Damien429d7192013-10-04 19:53:11 +01002190 }
2191 } else {
Damiend99b0522013-12-21 18:17:45 +00002192 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2193 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002194 if (kind == PN_expr_stmt_augassign) {
2195 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2196 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002197 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002198 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002199 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002200 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2201 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2202 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2203 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2204 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2205 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2206 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2207 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2208 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2209 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2210 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002211 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002212 }
Damien George7e5fb242014-02-01 22:18:47 +00002213 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002214 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2215 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002216 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2217 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002218 // following CPython, we store left-most first
2219 if (rhs > 0) {
2220 EMIT(dup_top);
2221 }
2222 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2223 for (int i = 0; i < rhs; i++) {
2224 if (i + 1 < rhs) {
2225 EMIT(dup_top);
2226 }
Damiend99b0522013-12-21 18:17:45 +00002227 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002228 }
Damien George0bb97132015-02-27 14:25:47 +00002229 } else {
2230 assert(kind == PN_expr_stmt_assign); // should be
Damien George42e0c592015-03-14 13:11:35 +00002231 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
2232 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002233 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2234 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2235 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002236 // optimisation for a, b = c, d; to match CPython's optimisation
Damien George4dea9222015-04-09 15:29:54 +00002237 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2238 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002239 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2240 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2241 // can't optimise when it's a star expression on the lhs
2242 goto no_optimisation;
2243 }
Damien429d7192013-10-04 19:53:11 +01002244 compile_node(comp, pns10->nodes[0]); // rhs
2245 compile_node(comp, pns10->nodes[1]); // rhs
2246 EMIT(rot_two);
2247 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2248 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00002249 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2250 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002251 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2252 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2253 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002254 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damien George4dea9222015-04-09 15:29:54 +00002255 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2256 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002257 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2258 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2259 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2260 // can't optimise when it's a star expression on the lhs
2261 goto no_optimisation;
2262 }
Damien429d7192013-10-04 19:53:11 +01002263 compile_node(comp, pns10->nodes[0]); // rhs
2264 compile_node(comp, pns10->nodes[1]); // rhs
2265 compile_node(comp, pns10->nodes[2]); // rhs
2266 EMIT(rot_three);
2267 EMIT(rot_two);
2268 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2269 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2270 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2271 } else {
Damien George495d7812014-04-08 17:51:47 +01002272 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002273 compile_node(comp, pns1->nodes[0]); // rhs
2274 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2275 }
Damien429d7192013-10-04 19:53:11 +01002276 }
2277 }
2278}
2279
Damien George6be0b0a2014-08-15 14:30:52 +01002280STATIC 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 +00002281 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002282 compile_node(comp, pns->nodes[0]);
2283 for (int i = 1; i < num_nodes; i += 1) {
2284 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002285 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002286 }
2287}
2288
Damien George969a6b32014-12-10 22:07:04 +00002289STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002290 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2291 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002292
Damien George6f355fd2014-04-10 14:11:31 +01002293 uint l_fail = comp_next_label(comp);
2294 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002295 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2296 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002297 EMIT_ARG(jump, l_end);
2298 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002299 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002300 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002301 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002302}
2303
Damien George969a6b32014-12-10 22:07:04 +00002304STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002305 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002306 //mp_parse_node_t pn_params = pns->nodes[0];
2307 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002308
Damien George36db6bc2014-05-07 17:24:22 +01002309 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002310 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002311 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 +01002312 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002313 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002314 }
2315
2316 // get the scope for this lambda
2317 scope_t *this_scope = (scope_t*)pns->nodes[2];
2318
2319 // make the lambda
2320 close_over_variables_etc(comp, this_scope, 0, 0);
2321}
2322
Damien George7711afb2015-02-28 15:10:18 +00002323STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002324 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002325 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002326 for (int i = 0; i < n; i += 1) {
2327 compile_node(comp, pns->nodes[i]);
2328 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002329 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002330 }
2331 }
Damien Georgeb9791222014-01-23 00:34:21 +00002332 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002333}
2334
Damien George7711afb2015-02-28 15:10:18 +00002335STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2336 compile_or_and_test(comp, pns, true);
2337}
2338
Damien George969a6b32014-12-10 22:07:04 +00002339STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002340 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002341}
2342
Damien George969a6b32014-12-10 22:07:04 +00002343STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002344 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002345 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002346}
2347
Damien George969a6b32014-12-10 22:07:04 +00002348STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002349 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002350 compile_node(comp, pns->nodes[0]);
2351 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002352 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002353 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002354 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002355 }
2356 for (int i = 1; i + 1 < num_nodes; i += 2) {
2357 compile_node(comp, pns->nodes[i + 1]);
2358 if (i + 2 < num_nodes) {
2359 EMIT(dup_top);
2360 EMIT(rot_three);
2361 }
Damien George7e5fb242014-02-01 22:18:47 +00002362 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002363 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002364 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002365 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2366 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2367 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2368 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2369 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2370 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002371 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002372 }
2373 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002374 } else {
2375 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002376 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2377 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002378 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002379 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002380 } else {
2381 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002382 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002383 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002384 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002385 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002386 }
Damien429d7192013-10-04 19:53:11 +01002387 }
Damien429d7192013-10-04 19:53:11 +01002388 }
2389 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002390 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002391 }
2392 }
2393 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002394 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002395 EMIT_ARG(jump, l_end);
2396 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002397 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002398 EMIT(rot_two);
2399 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002400 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002401 }
2402}
2403
Damien George969a6b32014-12-10 22:07:04 +00002404STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002405 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002406}
2407
Damien George969a6b32014-12-10 22:07:04 +00002408STATIC void compile_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_OR);
Damien429d7192013-10-04 19:53:11 +01002410}
2411
Damien George969a6b32014-12-10 22:07:04 +00002412STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002413 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002414}
2415
Damien George969a6b32014-12-10 22:07:04 +00002416STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002417 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002418}
2419
Damien George969a6b32014-12-10 22:07:04 +00002420STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002421 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002422 compile_node(comp, pns->nodes[0]);
2423 for (int i = 1; i + 1 < num_nodes; i += 2) {
2424 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002425 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002426 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002427 } else {
Damien George0bb97132015-02-27 14:25:47 +00002428 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2429 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002430 }
2431 }
2432}
2433
Damien George969a6b32014-12-10 22:07:04 +00002434STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002435 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002436 compile_node(comp, pns->nodes[0]);
2437 for (int i = 1; i + 1 < num_nodes; i += 2) {
2438 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002439 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002440 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002441 } else {
Damien George0bb97132015-02-27 14:25:47 +00002442 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2443 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002444 }
2445 }
2446}
2447
Damien George969a6b32014-12-10 22:07:04 +00002448STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002449 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002450 compile_node(comp, pns->nodes[0]);
2451 for (int i = 1; i + 1 < num_nodes; i += 2) {
2452 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002453 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002454 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002455 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002456 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002457 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002458 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002459 } else {
Damien George0bb97132015-02-27 14:25:47 +00002460 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2461 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002462 }
2463 }
2464}
2465
Damien George969a6b32014-12-10 22:07:04 +00002466STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002467 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002468 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002469 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002470 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002471 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002472 } else {
Damien George0bb97132015-02-27 14:25:47 +00002473 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2474 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002475 }
2476}
2477
Damien George969a6b32014-12-10 22:07:04 +00002478STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002479 // this is to handle special super() call
2480 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2481
2482 compile_generic_all_nodes(comp, pns);
2483}
2484
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002485STATIC 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 +01002486 // function to call is on top of stack
2487
Damien George35e2a4e2014-02-05 00:51:47 +00002488#if !MICROPY_EMIT_CPYTHON
2489 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002490 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 +00002491 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002492 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002493 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002494 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002495 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002496 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002497 EMIT_ARG(call_function, 2, 0, 0);
2498 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002499 }
2500 }
Damien George01039b52014-12-21 17:44:27 +00002501 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002502 return;
2503 }
2504#endif
2505
Damien George2c0842b2014-04-27 16:46:51 +01002506 // get the list of arguments
2507 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002508 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002509
Damien George2c0842b2014-04-27 16:46:51 +01002510 // compile the arguments
2511 // Rather than calling compile_node on the list, we go through the list of args
2512 // explicitly here so that we can count the number of arguments and give sensible
2513 // error messages.
2514 int n_positional = n_positional_extra;
2515 uint n_keyword = 0;
2516 uint star_flags = 0;
2517 for (int i = 0; i < n_args; i++) {
2518 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2519 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2520 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2521 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2522 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2523 return;
2524 }
2525 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2526 compile_node(comp, pns_arg->nodes[0]);
2527 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2528 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2529 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2530 return;
2531 }
2532 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2533 compile_node(comp, pns_arg->nodes[0]);
2534 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2535 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2536 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2537 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2538 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2539 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2540 return;
2541 }
Damien George59fba2d2015-06-25 14:42:13 +00002542 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien George2c0842b2014-04-27 16:46:51 +01002543 compile_node(comp, pns2->nodes[0]);
2544 n_keyword += 1;
2545 } else {
2546 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2547 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2548 n_positional++;
2549 }
2550 } else {
2551 goto normal_argument;
2552 }
2553 } else {
2554 normal_argument:
2555 if (n_keyword > 0) {
2556 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2557 return;
2558 }
2559 compile_node(comp, args[i]);
2560 n_positional++;
2561 }
Damien429d7192013-10-04 19:53:11 +01002562 }
2563
Damien George2c0842b2014-04-27 16:46:51 +01002564 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002565 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002566 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002567 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002568 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002569 }
Damien429d7192013-10-04 19:53:11 +01002570}
2571
Damien George969a6b32014-12-10 22:07:04 +00002572STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002573 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002574 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002575 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 +01002576 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002577 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2578 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002579 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002580 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002581 i += 1;
2582 } else {
2583 compile_node(comp, pns->nodes[i]);
2584 }
Damien George35e2a4e2014-02-05 00:51:47 +00002585 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002586 }
2587}
2588
Damien George969a6b32014-12-10 22:07:04 +00002589STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002590 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002591 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002592}
2593
Damien George969a6b32014-12-10 22:07:04 +00002594STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002595 // a list of strings
Damien63321742013-12-10 17:41:49 +00002596
2597 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002598 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002599 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002600 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002601 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002602 int pn_kind;
2603 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2604 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2605 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2606 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2607 } else {
2608 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2609 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002610 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2611 pn_kind = MP_PARSE_NODE_STRING;
2612 } else {
2613 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2614 pn_kind = MP_PARSE_NODE_BYTES;
2615 }
Damien George40f3c022014-07-03 13:25:24 +01002616 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002617 }
Damien63321742013-12-10 17:41:49 +00002618 if (i == 0) {
2619 string_kind = pn_kind;
2620 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002621 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002622 return;
2623 }
Damien429d7192013-10-04 19:53:11 +01002624 }
Damien63321742013-12-10 17:41:49 +00002625
Damien George65ef6b72015-01-14 21:17:27 +00002626 // if we are not in the last pass, just load a dummy object
2627 if (comp->pass != MP_PASS_EMIT) {
2628 EMIT_ARG(load_const_obj, mp_const_none);
2629 return;
2630 }
2631
Damien63321742013-12-10 17:41:49 +00002632 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002633 vstr_t vstr;
2634 vstr_init_len(&vstr, n_bytes);
2635 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002636 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002637 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002638 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002639 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2640 memcpy(s_dest, s, s_len);
2641 s_dest += s_len;
2642 } else {
2643 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002644 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2645 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002646 }
Damien63321742013-12-10 17:41:49 +00002647 }
Damien George65ef6b72015-01-14 21:17:27 +00002648
2649 // load the object
Damien George05005f62015-01-21 22:48:37 +00002650 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 +01002651}
2652
2653// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002654STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002655 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2656 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2657 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002658
Damien George36db6bc2014-05-07 17:24:22 +01002659 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002660 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002661 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 +01002662 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002663 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002664 }
2665
2666 // get the scope for this comprehension
2667 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2668
2669 // compile the comprehension
2670 close_over_variables_etc(comp, this_scope, 0, 0);
2671
2672 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2673 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002674 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002675}
2676
Damien George969a6b32014-12-10 22:07:04 +00002677STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002678 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002679 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002680 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2681 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2682 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2683 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2684 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2685 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2686 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002687 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002688 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002689 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002690 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002691 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002692 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002693 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002694 // generator expression
2695 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2696 } else {
2697 // tuple with 2 items
2698 goto tuple_with_2_items;
2699 }
2700 } else {
2701 // tuple with 2 items
2702 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002703 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002704 }
2705 } else {
2706 // parenthesis around a single item, is just that item
2707 compile_node(comp, pns->nodes[0]);
2708 }
2709}
2710
Damien George969a6b32014-12-10 22:07:04 +00002711STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002712 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002713 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002714 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002715 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2716 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2717 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2718 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2719 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002720 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002721 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002722 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002723 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002724 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002725 // list of many items
2726 compile_node(comp, pns2->nodes[0]);
2727 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002728 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002729 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002730 // list comprehension
2731 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2732 } else {
2733 // list with 2 items
2734 goto list_with_2_items;
2735 }
2736 } else {
2737 // list with 2 items
2738 list_with_2_items:
2739 compile_node(comp, pns2->nodes[0]);
2740 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002741 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002742 }
2743 } else {
2744 // list with 1 item
2745 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002746 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002747 }
2748}
2749
Damien George969a6b32014-12-10 22:07:04 +00002750STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002751 mp_parse_node_t pn = pns->nodes[0];
2752 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002753 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002754 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002755 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2756 pns = (mp_parse_node_struct_t*)pn;
2757 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002758 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002759 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002760 compile_node(comp, pn);
2761 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002762 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2763 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2764 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2765 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002766 // dict/set with multiple elements
2767
2768 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002769 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002770 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002771
2772 // first element sets whether it's a dict or set
2773 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002774 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002775 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002776 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002777 compile_node(comp, pns->nodes[0]);
2778 EMIT(store_map);
2779 is_dict = true;
2780 } else {
2781 // a set
2782 compile_node(comp, pns->nodes[0]); // 1st value of set
2783 is_dict = false;
2784 }
2785
2786 // process rest of elements
2787 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002788 mp_parse_node_t pn_i = nodes[i];
2789 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2790 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002791 if (is_dict) {
2792 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002793 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002794 return;
2795 }
2796 EMIT(store_map);
2797 } else {
2798 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002799 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002800 return;
2801 }
2802 }
2803 }
2804
Damien Georgee37dcaa2014-12-27 17:07:16 +00002805 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002806 // if it's a set, build it
2807 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002808 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002809 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002810 #endif
Damien George0bb97132015-02-27 14:25:47 +00002811 } else {
2812 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002813 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002814 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002815 // a dictionary comprehension
2816 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2817 } else {
2818 // a set comprehension
2819 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2820 }
Damien429d7192013-10-04 19:53:11 +01002821 }
2822 } else {
2823 // set with one element
2824 goto set_with_one_element;
2825 }
2826 } else {
2827 // set with one element
2828 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002829 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002830 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002831 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002832 #else
2833 assert(0);
2834 #endif
Damien429d7192013-10-04 19:53:11 +01002835 }
2836}
2837
Damien George969a6b32014-12-10 22:07:04 +00002838STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002839 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002840}
2841
Damien George969a6b32014-12-10 22:07:04 +00002842STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002843 // object who's index we want is on top of stack
2844 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002845 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002846}
2847
Damien George969a6b32014-12-10 22:07:04 +00002848STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002849 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002850 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002851}
2852
Damien George83204f32014-12-27 17:20:41 +00002853#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002854STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002855 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2856 mp_parse_node_t pn = pns->nodes[0];
2857 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002858 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002859 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2860 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002861 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2862 pns = (mp_parse_node_struct_t*)pn;
2863 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002864 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002865 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002866 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002867 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002868 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002869 } else {
2870 // [?::x]
2871 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002872 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002873 }
Damiend99b0522013-12-21 18:17:45 +00002874 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002875 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002876 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2877 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2878 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2879 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002880 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002881 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002882 } else {
2883 // [?:x:x]
2884 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002885 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002886 }
2887 } else {
2888 // [?:x]
2889 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002890 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002891 }
2892 } else {
2893 // [?:x]
2894 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002895 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002896 }
2897}
2898
Damien George969a6b32014-12-10 22:07:04 +00002899STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002900 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002901 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2902 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002903}
2904
Damien George969a6b32014-12-10 22:07:04 +00002905STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002906 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002907 compile_subscript_3_helper(comp, pns);
2908}
Damien George83204f32014-12-27 17:20:41 +00002909#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002910
Damien George969a6b32014-12-10 22:07:04 +00002911STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002912 // if this is called then we are compiling a dict key:value pair
2913 compile_node(comp, pns->nodes[1]); // value
2914 compile_node(comp, pns->nodes[0]); // key
2915}
2916
Damien George969a6b32014-12-10 22:07:04 +00002917STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002918 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002919 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002920 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002921}
2922
Damien George969a6b32014-12-10 22:07:04 +00002923STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002924 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002925 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002926 return;
2927 }
Damiend99b0522013-12-21 18:17:45 +00002928 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002929 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002930 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002931 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2932 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002933 compile_node(comp, pns->nodes[0]);
2934 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002935 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002936 EMIT(yield_from);
2937 } else {
2938 compile_node(comp, pns->nodes[0]);
2939 EMIT(yield_value);
2940 }
2941}
2942
Damien George65ef6b72015-01-14 21:17:27 +00002943STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2944 // only create and load the actual str object on the last pass
2945 if (comp->pass != MP_PASS_EMIT) {
2946 EMIT_ARG(load_const_obj, mp_const_none);
2947 } else {
2948 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2949 }
2950}
2951
2952STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2953 // only create and load the actual bytes object on the last pass
2954 if (comp->pass != MP_PASS_EMIT) {
2955 EMIT_ARG(load_const_obj, mp_const_none);
2956 } else {
2957 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2958 }
2959}
2960
Damien George7d414a12015-02-08 01:57:40 +00002961STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2962 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2963}
2964
Damiend99b0522013-12-21 18:17:45 +00002965typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002966STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002967#define nc NULL
2968#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002969#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002970#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002971#undef nc
2972#undef c
2973#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002974 NULL,
2975 compile_string,
2976 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002977 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002978};
2979
Damien George6be0b0a2014-08-15 14:30:52 +01002980STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002981 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002982 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002983 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002984 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002985 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002986 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002987 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002988 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002989 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002990 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2991 case MP_PARSE_NODE_BYTES:
2992 // only create and load the actual bytes object on the last pass
2993 if (comp->pass != MP_PASS_EMIT) {
2994 EMIT_ARG(load_const_obj, mp_const_none);
2995 } else {
2996 mp_uint_t len;
2997 const byte *data = qstr_data(arg, &len);
2998 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2999 }
3000 break;
Damien Georged2d64f02015-01-14 21:32:42 +00003001 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00003002 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01003003 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01003004 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01003005 // do nothing
3006 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00003007 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01003008 }
3009 break;
Damien429d7192013-10-04 19:53:11 +01003010 }
3011 } else {
Damiend99b0522013-12-21 18:17:45 +00003012 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00003013 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00003014 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
3015 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01003016#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00003017 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
3018 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01003019#endif
Damien George65ef6b72015-01-14 21:17:27 +00003020 compile_syntax_error(comp, pn, "internal compiler error");
3021 } else {
3022 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01003023 }
3024 }
3025}
3026
Damien George2ac4af62014-08-15 16:45:41 +01003027STATIC 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 +01003028 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01003029 qstr param_name = MP_QSTR_NULL;
3030 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003031 if (MP_PARSE_NODE_IS_ID(pn)) {
3032 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01003033 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003034 // comes after a star, so counts as a keyword-only parameter
3035 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01003036 } else {
Damien George2827d622014-04-27 15:50:52 +01003037 // comes before a star, so counts as a positional parameter
3038 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003039 }
Damienb14de212013-10-06 00:28:28 +01003040 } else {
Damiend99b0522013-12-21 18:17:45 +00003041 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3042 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3043 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3044 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003045 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003046 // comes after a star, so counts as a keyword-only parameter
3047 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003048 } else {
Damien George2827d622014-04-27 15:50:52 +01003049 // comes before a star, so counts as a positional parameter
3050 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003051 }
Damiend99b0522013-12-21 18:17:45 +00003052 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003053 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003054 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003055 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003056 // bare star
3057 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003058 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003059 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003060 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003061 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003062 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00003063 } else {
3064 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01003065 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003066 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003067 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3068 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003069 }
Damien George0bb97132015-02-27 14:25:47 +00003070 } else {
3071 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00003072 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003073 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003074 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003075 }
Damien429d7192013-10-04 19:53:11 +01003076 }
3077
Damien George2827d622014-04-27 15:50:52 +01003078 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003079 bool added;
3080 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3081 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003082 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003083 return;
3084 }
Damien429d7192013-10-04 19:53:11 +01003085 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003086 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003087 }
3088}
3089
Damien George2827d622014-04-27 15:50:52 +01003090STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003091 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003092}
3093
Damien George2827d622014-04-27 15:50:52 +01003094STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003095 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3096}
3097
3098STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3099 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3100 // no annotation
3101 return;
3102 }
3103
3104 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3105 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3106 // named parameter with possible annotation
3107 // fallthrough
3108 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3109 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3110 // named star with possible annotation
3111 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3112 // fallthrough
3113 } else {
3114 // no annotation
3115 return;
3116 }
3117 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3118 // double star with possible annotation
3119 // fallthrough
3120 } else {
3121 // no annotation
3122 return;
3123 }
3124
3125 mp_parse_node_t pn_annotation = pns->nodes[1];
3126
3127 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3128 #if MICROPY_EMIT_NATIVE
3129 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3130 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3131 assert(id_info != NULL);
3132
3133 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3134 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3135 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3136 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3137 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003138 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003139 }
3140 }
3141 #endif // MICROPY_EMIT_NATIVE
3142 }
Damien429d7192013-10-04 19:53:11 +01003143}
3144
Damien George6be0b0a2014-08-15 14:30:52 +01003145STATIC 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 +01003146 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003147 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003148 // no more nested if/for; compile inner expression
3149 compile_node(comp, pn_inner_expr);
3150 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003151 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003152 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003153 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003154 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003155 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003156 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003157 #endif
Damien429d7192013-10-04 19:53:11 +01003158 } else {
3159 EMIT(yield_value);
3160 EMIT(pop_top);
3161 }
Damiend99b0522013-12-21 18:17:45 +00003162 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003163 // if condition
Damiend99b0522013-12-21 18:17:45 +00003164 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003165 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3166 pn_iter = pns_comp_if->nodes[1];
3167 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00003168 } else {
3169 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01003170 // for loop
Damiend99b0522013-12-21 18:17:45 +00003171 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003172 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003173 uint l_end2 = comp_next_label(comp);
3174 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003175 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003176 EMIT_ARG(label_assign, l_top2);
3177 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003178 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3179 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
Damien Georgeb9791222014-01-23 00:34:21 +00003180 EMIT_ARG(jump, l_top2);
3181 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003182 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01003183 }
3184}
3185
Damien George1463c1f2014-04-25 23:52:57 +01003186STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3187#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003188 // see http://www.python.org/dev/peps/pep-0257/
3189
3190 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003191 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003192 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003193 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003194 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003195 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3196 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003197 for (int i = 0; i < num_nodes; i++) {
3198 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003199 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 +00003200 // not a newline, so this is the first statement; finish search
3201 break;
3202 }
3203 }
3204 // 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 +00003205 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003206 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003207 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003208 } else {
3209 return;
3210 }
3211
3212 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003213 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00003214 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003215 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3216 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3217 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3218 // compile the doc string
3219 compile_node(comp, pns->nodes[0]);
3220 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00003221 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003222 }
3223 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003224#else
3225 (void)comp;
3226 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003227#endif
Damien429d7192013-10-04 19:53:11 +01003228}
3229
Damien George1463c1f2014-04-25 23:52:57 +01003230STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003231 comp->pass = pass;
3232 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003233 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003234 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003235
Damien George36db6bc2014-05-07 17:24:22 +01003236 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003237 // reset maximum stack sizes in scope
3238 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003239 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003240 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003241 }
3242
Damien5ac1b2e2013-10-18 19:58:12 +01003243#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003244 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003245 scope_print_info(scope);
3246 }
Damien5ac1b2e2013-10-18 19:58:12 +01003247#endif
Damien429d7192013-10-04 19:53:11 +01003248
3249 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003250 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3251 assert(scope->kind == SCOPE_MODULE);
3252 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3253 compile_node(comp, pns->nodes[0]); // compile the expression
3254 EMIT(return_value);
3255 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003256 if (!comp->is_repl) {
3257 check_for_doc_string(comp, scope->pn);
3258 }
Damien429d7192013-10-04 19:53:11 +01003259 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003260 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003261 EMIT(return_value);
3262 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003263 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3264 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3265 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003266
3267 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003268 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003269 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003270 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003271 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003272 } else {
3273 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003274
Damien George2ac4af62014-08-15 16:45:41 +01003275 // argument annotations
3276 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3277
3278 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003279 mp_parse_node_t pn_annotation = pns->nodes[2];
3280 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3281 #if MICROPY_EMIT_NATIVE
3282 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3283 // nodes[2] can be null or a test-expr
3284 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3285 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3286 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3287 } else {
3288 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3289 }
Damien George2ac4af62014-08-15 16:45:41 +01003290 }
Damien Georgea5190a72014-08-15 22:39:08 +01003291 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003292 }
Damien George2ac4af62014-08-15 16:45:41 +01003293 }
Damien429d7192013-10-04 19:53:11 +01003294
3295 compile_node(comp, pns->nodes[3]); // 3 is function body
3296 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003297 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003298 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003299 EMIT(return_value);
3300 }
3301 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003302 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3303 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3304 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003305
3306 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003307 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003308 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003309 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003310 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3311 }
3312
3313 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003314
3315 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3316 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3317 EMIT(pop_top);
3318 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3319 }
Damien429d7192013-10-04 19:53:11 +01003320 EMIT(return_value);
3321 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3322 // a bit of a hack at the moment
3323
Damiend99b0522013-12-21 18:17:45 +00003324 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3325 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3326 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3327 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3328 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003329
Damien George708c0732014-04-27 19:23:46 +01003330 // We need a unique name for the comprehension argument (the iterator).
3331 // CPython uses .0, but we should be able to use anything that won't
3332 // clash with a user defined variable. Best to use an existing qstr,
3333 // so we use the blank qstr.
3334#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003335 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003336#else
3337 qstr qstr_arg = MP_QSTR_;
3338#endif
Damien George36db6bc2014-05-07 17:24:22 +01003339 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003340 bool added;
3341 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3342 assert(added);
3343 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003344 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003345 }
3346
3347 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003348 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003349 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003350 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003351 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003352 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003353 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003354 #endif
Damien429d7192013-10-04 19:53:11 +01003355 }
3356
Damien George6f355fd2014-04-10 14:11:31 +01003357 uint l_end = comp_next_label(comp);
3358 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00003359 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00003360 EMIT_ARG(label_assign, l_top);
3361 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003362 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3363 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003364 EMIT_ARG(jump, l_top);
3365 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003366 EMIT(for_iter_end);
3367
3368 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003369 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003370 }
3371 EMIT(return_value);
3372 } else {
3373 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003374 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3375 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3376 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003377
Damien George36db6bc2014-05-07 17:24:22 +01003378 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003379 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003380 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003381 assert(added);
3382 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003383 }
3384
Damien George542bd6b2015-03-26 14:42:40 +00003385 compile_load_id(comp, MP_QSTR___name__);
3386 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003387 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003388 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003389
3390 check_for_doc_string(comp, pns->nodes[2]);
3391 compile_node(comp, pns->nodes[2]); // 2 is class body
3392
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003393 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003394 assert(id != NULL);
3395 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003396 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003397 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003398#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003399 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003400#else
Damien George542bd6b2015-03-26 14:42:40 +00003401 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003402#endif
Damien429d7192013-10-04 19:53:11 +01003403 }
3404 EMIT(return_value);
3405 }
3406
Damien415eb6f2013-10-05 12:19:06 +01003407 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003408
3409 // make sure we match all the exception levels
3410 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003411}
3412
Damien George094d4502014-04-02 17:31:27 +01003413#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003414// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003415STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003416 comp->pass = pass;
3417 comp->scope_cur = scope;
3418 comp->next_label = 1;
3419
3420 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003421 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003422 return;
3423 }
3424
Damien George36db6bc2014-05-07 17:24:22 +01003425 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003426 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003427 }
3428
Damien826005c2013-10-05 23:17:28 +01003429 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003430 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3431 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3432 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003433
Damiend99b0522013-12-21 18:17:45 +00003434 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003435
Damiena2f2f7d2013-10-06 00:14:13 +01003436 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003437 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003438 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003439 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003440 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003441 if (comp->compile_error != MP_OBJ_NULL) {
3442 goto inline_asm_error;
3443 }
Damiena2f2f7d2013-10-06 00:14:13 +01003444 }
3445
Damiend99b0522013-12-21 18:17:45 +00003446 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003447
Damiend99b0522013-12-21 18:17:45 +00003448 mp_parse_node_t pn_body = pns->nodes[3]; // body
3449 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003450 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003451
Damien826005c2013-10-05 23:17:28 +01003452 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003453 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3454 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003455 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3456 // no instructions
3457 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003458 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003459 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003460 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003461 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003462 return;
3463 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003464
3465 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003466 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003467 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3468 goto not_an_instruction;
3469 }
Damiend99b0522013-12-21 18:17:45 +00003470 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003471 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3472 goto not_an_instruction;
3473 }
3474 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3475 goto not_an_instruction;
3476 }
3477 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3478 goto not_an_instruction;
3479 }
Damiend99b0522013-12-21 18:17:45 +00003480 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003481
3482 // parse node looks like an instruction
3483 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003484 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3485 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3486 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003487 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003488
3489 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003490 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003491 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003492 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003493 return;
3494 }
Damien George6f355fd2014-04-10 14:11:31 +01003495 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003496 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003497 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3498 compile_syntax_error(comp, nodes[i], "label redefined");
3499 return;
3500 }
Damien826005c2013-10-05 23:17:28 +01003501 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003502 } else if (op == MP_QSTR_align) {
3503 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003504 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003505 return;
3506 }
Damien George36db6bc2014-05-07 17:24:22 +01003507 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003508 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3509 }
3510 } else if (op == MP_QSTR_data) {
3511 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003512 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003513 return;
3514 }
Damien George36db6bc2014-05-07 17:24:22 +01003515 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003516 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003517 for (uint j = 1; j < n_args; j++) {
3518 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003519 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003520 return;
3521 }
Damien George50912e72015-01-20 11:55:10 +00003522 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003523 }
3524 }
Damien826005c2013-10-05 23:17:28 +01003525 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003526 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003527 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003528 }
3529 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003530
3531 if (comp->compile_error != MP_OBJ_NULL) {
3532 pns = pns2; // this is the parse node that had the error
3533 goto inline_asm_error;
3534 }
Damien826005c2013-10-05 23:17:28 +01003535 }
3536
Damien George36db6bc2014-05-07 17:24:22 +01003537 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003538 EMIT_INLINE_ASM(end_pass);
3539 }
3540
3541 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003542 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003543 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003544 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003545 }
Damien429d7192013-10-04 19:53:11 +01003546}
Damien George094d4502014-04-02 17:31:27 +01003547#endif
Damien429d7192013-10-04 19:53:11 +01003548
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003549STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003550#if !MICROPY_EMIT_CPYTHON
3551 // in Micro Python we put the *x parameter after all other parameters (except **y)
3552 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3553 id_info_t *id_param = NULL;
3554 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3555 id_info_t *id = &scope->id_info[i];
3556 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3557 if (id_param != NULL) {
3558 // swap star param with last param
3559 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3560 }
3561 break;
3562 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3563 id_param = id;
3564 }
3565 }
3566 }
3567#endif
3568
Damien429d7192013-10-04 19:53:11 +01003569 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003570 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003571 scope->num_locals = 0;
3572 for (int i = 0; i < scope->id_info_len; i++) {
3573 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003574 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003575 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3576 continue;
3577 }
3578 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3579 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3580 }
Damien George2827d622014-04-27 15:50:52 +01003581 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003582 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003583 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003584 }
3585 }
3586
3587 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003588#if MICROPY_EMIT_CPYTHON
3589 int num_cell = 0;
3590#endif
Damien9ecbcff2013-12-11 00:41:43 +00003591 for (int i = 0; i < scope->id_info_len; i++) {
3592 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003593#if MICROPY_EMIT_CPYTHON
3594 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003595 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003596 id->local_num = num_cell;
3597 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003598 }
Damien George6baf76e2013-12-30 22:32:17 +00003599#else
3600 // in Micro Python the cells come right after the fast locals
3601 // parameters are not counted here, since they remain at the start
3602 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003603 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003604 id->local_num = scope->num_locals;
3605 scope->num_locals += 1;
3606 }
3607#endif
Damien9ecbcff2013-12-11 00:41:43 +00003608 }
Damien9ecbcff2013-12-11 00:41:43 +00003609
3610 // compute the index of free vars (freevars[idx] in CPython)
3611 // make sure they are in the order of the parent scope
3612 if (scope->parent != NULL) {
3613 int num_free = 0;
3614 for (int i = 0; i < scope->parent->id_info_len; i++) {
3615 id_info_t *id = &scope->parent->id_info[i];
3616 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3617 for (int j = 0; j < scope->id_info_len; j++) {
3618 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003619 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003620 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003621#if MICROPY_EMIT_CPYTHON
3622 // in CPython the frees are numbered after the cells
3623 id2->local_num = num_cell + num_free;
3624#else
3625 // in Micro Python the frees come first, before the params
3626 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003627#endif
3628 num_free += 1;
3629 }
3630 }
3631 }
Damien429d7192013-10-04 19:53:11 +01003632 }
Damien George6baf76e2013-12-30 22:32:17 +00003633#if !MICROPY_EMIT_CPYTHON
3634 // in Micro Python shift all other locals after the free locals
3635 if (num_free > 0) {
3636 for (int i = 0; i < scope->id_info_len; i++) {
3637 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003638 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003639 id->local_num += num_free;
3640 }
3641 }
Damien George2827d622014-04-27 15:50:52 +01003642 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 +00003643 scope->num_locals += num_free;
3644 }
3645#endif
Damien429d7192013-10-04 19:53:11 +01003646 }
3647
Damien George8725f8f2014-02-15 19:33:11 +00003648 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003649
3650#if MICROPY_EMIT_CPYTHON
3651 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003652 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) {
3653 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003654 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003655 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003656 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 +00003657 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003658 }
3659 }
Damien George882b3632014-04-02 15:56:31 +01003660#endif
3661
Damien429d7192013-10-04 19:53:11 +01003662 int num_free = 0;
3663 for (int i = 0; i < scope->id_info_len; i++) {
3664 id_info_t *id = &scope->id_info[i];
3665 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3666 num_free += 1;
3667 }
3668 }
3669 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003670 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003671 }
3672}
3673
Damien George65cad122014-04-06 11:48:15 +01003674mp_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 +01003675 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003676 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003677 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003678 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003679
Damien George62a3a282015-03-01 12:04:05 +00003680 // create the module scope
3681 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3682
3683 // optimise constants (scope must be set for error messages to work)
3684 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003685 mp_map_t consts;
3686 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003687 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003688 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003689
Damien Georgea210c772015-03-26 15:49:53 +00003690 // create standard emitter; it's used at least for MP_PASS_SCOPE
3691 #if MICROPY_EMIT_CPYTHON
3692 emit_t *emit_cpython = emit_cpython_new();
3693 #else
3694 emit_t *emit_bc = emit_bc_new();
3695 #endif
3696
Damien826005c2013-10-05 23:17:28 +01003697 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003698 #if MICROPY_EMIT_CPYTHON
3699 comp->emit = emit_cpython;
3700 comp->emit_method_table = &emit_cpython_method_table;
3701 #else
3702 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003703 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003704 comp->emit_method_table = &emit_bc_method_table;
3705 #endif
Damien George41125902015-03-26 16:44:14 +00003706 #endif
Damien Georgea77ffe62015-03-14 12:59:31 +00003707 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003708 comp->emit_inline_asm = NULL;
3709 comp->emit_inline_asm_method_table = NULL;
Damien Georgea77ffe62015-03-14 12:59:31 +00003710 #endif
Damien826005c2013-10-05 23:17:28 +01003711 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003712 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003713 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003714#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003715 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003716 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003717#endif
Damien826005c2013-10-05 23:17:28 +01003718 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003719 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003720 }
3721
3722 // update maximim number of labels needed
3723 if (comp->next_label > max_num_labels) {
3724 max_num_labels = comp->next_label;
3725 }
Damien429d7192013-10-04 19:53:11 +01003726 }
3727
Damien826005c2013-10-05 23:17:28 +01003728 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003729 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 +00003730 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003731 }
3732
Damien Georgea210c772015-03-26 15:49:53 +00003733 // set max number of labels now that it's calculated
3734 #if MICROPY_EMIT_CPYTHON
3735 emit_cpython_set_max_num_labels(emit_cpython, max_num_labels);
3736 #else
3737 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
3738 #endif
3739
Damien826005c2013-10-05 23:17:28 +01003740 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003741#if !MICROPY_EMIT_CPYTHON
Damien Georgee67ed5d2014-01-04 13:55:24 +00003742#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003743 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003744#endif
Damien3ef4abb2013-10-12 16:53:13 +01003745#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003746 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003747#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003748#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003749 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003750 if (false) {
3751 // dummy
3752
Damien3ef4abb2013-10-12 16:53:13 +01003753#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003754 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003755 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003756 if (emit_inline_thumb == NULL) {
3757 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3758 }
3759 comp->emit = NULL;
3760 comp->emit_method_table = NULL;
3761 comp->emit_inline_asm = emit_inline_thumb;
3762 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003763 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003764 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003765 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003766 }
Damienc025ebb2013-10-12 14:30:21 +01003767#endif
3768
Damien826005c2013-10-05 23:17:28 +01003769 } else {
Damienc025ebb2013-10-12 14:30:21 +01003770
3771 // choose the emit type
3772
Damien3ef4abb2013-10-12 16:53:13 +01003773#if MICROPY_EMIT_CPYTHON
Damien Georgea210c772015-03-26 15:49:53 +00003774 comp->emit = emit_cpython;
Damienc025ebb2013-10-12 14:30:21 +01003775 comp->emit_method_table = &emit_cpython_method_table;
3776#else
Damien826005c2013-10-05 23:17:28 +01003777 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003778
3779#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003780 case MP_EMIT_OPT_NATIVE_PYTHON:
3781 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003782#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003783 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003784 emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003785 }
Damien13ed3a62013-10-08 09:05:10 +01003786 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003787#elif MICROPY_EMIT_X86
3788 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003789 emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels);
Damien Georgec90f59e2014-09-06 23:06:36 +01003790 }
3791 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003792#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003793 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003794 emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels);
Damienc025ebb2013-10-12 14:30:21 +01003795 }
3796 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003797#elif MICROPY_EMIT_ARM
3798 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003799 emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003800 }
3801 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003802#endif
3803 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003804 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003805 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003806#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003807
Damien826005c2013-10-05 23:17:28 +01003808 default:
Damien826005c2013-10-05 23:17:28 +01003809 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003810 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003811 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003812 #endif
Damien826005c2013-10-05 23:17:28 +01003813 break;
3814 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003815#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003816
Damien George1e1779e2015-01-14 00:20:28 +00003817 // need a pass to compute stack size
3818 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3819
Damien George36db6bc2014-05-07 17:24:22 +01003820 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003821 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003822 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3823 }
3824
3825 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003826 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003827 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003828 }
Damien6cdd3af2013-10-05 18:08:26 +01003829 }
Damien429d7192013-10-04 19:53:11 +01003830 }
3831
Damien Georgecfc4c332015-07-29 22:16:01 +00003832 if (comp->compile_error != MP_OBJ_NULL) {
3833 // if there is no line number for the error then use the line
3834 // number for the start of this scope
3835 compile_error_set_line(comp, comp->scope_cur->pn);
3836 // add a traceback to the exception using relevant source info
3837 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3838 comp->compile_error_line, comp->scope_cur->simple_name);
3839 }
3840
Damien George41d02b62014-01-24 22:42:28 +00003841 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003842
3843#if MICROPY_EMIT_CPYTHON
3844 emit_cpython_free(emit_cpython);
3845#else
3846 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003847#if MICROPY_EMIT_NATIVE
3848 if (emit_native != NULL) {
3849#if MICROPY_EMIT_X64
3850 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003851#elif MICROPY_EMIT_X86
3852 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003853#elif MICROPY_EMIT_THUMB
3854 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003855#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003856 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003857#endif
3858 }
3859#endif
3860#if MICROPY_EMIT_INLINE_THUMB
3861 if (emit_inline_thumb != NULL) {
3862 emit_inline_thumb_free(emit_inline_thumb);
3863 }
3864#endif
Damien Georgea210c772015-03-26 15:49:53 +00003865#endif // MICROPY_EMIT_CPYTHON
Damien George41d02b62014-01-24 22:42:28 +00003866
Damien George52b5d762014-09-23 15:31:56 +00003867 // free the parse tree
Damien George62a3a282015-03-01 12:04:05 +00003868 mp_parse_node_free(module_scope->pn);
Damien George52b5d762014-09-23 15:31:56 +00003869
Damien George41d02b62014-01-24 22:42:28 +00003870 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003871 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003872 for (scope_t *s = module_scope; s;) {
3873 scope_t *next = s->next;
3874 scope_free(s);
3875 s = next;
3876 }
Damien5ac1b2e2013-10-18 19:58:12 +01003877
Damien George41d02b62014-01-24 22:42:28 +00003878 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003879 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003880 m_del_obj(compiler_t, comp);
3881
Damien Georgea91ac202014-10-05 19:01:34 +01003882 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003883 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003884 } else {
3885#if MICROPY_EMIT_CPYTHON
3886 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003887 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003888 return mp_const_true;
3889#else
3890 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003891 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003892#endif
3893 }
Damien429d7192013-10-04 19:53:11 +01003894}