blob: aa02ed22d4585045d43beaf41e3e440acfada486 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
36#include "py/smallint.h"
37#include "py/runtime.h"
38#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010039
40// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien George41125902015-03-26 16:44:14 +000052#define NEED_METHOD_TABLE (MICROPY_EMIT_CPYTHON || MICROPY_EMIT_NATIVE)
53
54#if NEED_METHOD_TABLE
55
56// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000057#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
58#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000059#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
60#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien Georgeb9791222014-01-23 00:34:21 +000061#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
62#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010063
Damien George41125902015-03-26 16:44:14 +000064#else
65
66// if we only have the bytecode emitter enabled then we can do a direct call to the functions
67#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
68#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
69#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
70#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
71
72#endif
73
Damien Georgea91ac202014-10-05 19:01:34 +010074// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010075typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000076 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010077
Damien George78035b92014-04-09 12:27:39 +010078 uint8_t is_repl;
79 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010080 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010081 uint8_t have_star;
82
83 // try to keep compiler clean from nlr
84 // this is set to an exception object if we have a compile error
85 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010086
Damien George6f355fd2014-04-10 14:11:31 +010087 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010088
Damien George69b89d22014-04-11 13:38:30 +000089 uint16_t num_dict_params;
90 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010091
Damien Georgea91ac202014-10-05 19:01:34 +010092 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
93 uint16_t continue_label;
94 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000095 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010096
Damien429d7192013-10-04 19:53:11 +010097 scope_t *scope_head;
98 scope_t *scope_cur;
99
Damien6cdd3af2013-10-05 18:08:26 +0100100 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000101 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100102 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000103 #endif
Damien826005c2013-10-05 23:17:28 +0100104
Damien Georgea77ffe62015-03-14 12:59:31 +0000105 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +0100106 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
107 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien Georgea77ffe62015-03-14 12:59:31 +0000108 #endif
Damien429d7192013-10-04 19:53:11 +0100109} compiler_t;
110
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100111STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +0100112 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
113 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100114 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George8dfbd2d2015-02-13 01:00:51 +0000115 mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100116 } else {
Damien Georgea91ac202014-10-05 19:01:34 +0100117 // we don't have a line number, so just pass 0
Damien George8dfbd2d2015-02-13 01:00:51 +0000118 mp_obj_exception_add_traceback(exc, comp->source_file, 0, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100119 }
Damien Georgea91ac202014-10-05 19:01:34 +0100120 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000121}
122
Damien Georgeddd1e182015-01-10 14:07:24 +0000123#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100124STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300125 #if MICROPY_PY_UCTYPES
126 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
127 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100128 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100129 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100130};
Damien Georgeddd1e182015-01-10 14:07:24 +0000131STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
132#endif
Damien George57e99eb2014-04-10 22:42:11 +0100133
Damien Georgeffae48d2014-05-08 15:58:39 +0000134// this function is essentially a simple preprocessor
135STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
136 if (0) {
137 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100138#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000139 } else if (MP_PARSE_NODE_IS_ID(pn)) {
140 // lookup identifier in table of dynamic constants
141 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
142 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
143 if (elem != NULL) {
144 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
145 }
146#endif
147 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000148 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100149
Damien Georgeffae48d2014-05-08 15:58:39 +0000150 // fold some parse nodes before folding their arguments
151 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100152#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000153 case PN_expr_stmt:
154 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
155 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
156 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
157 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
158 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
159 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
160 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
161 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
162 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
163 ) {
164 // code to assign dynamic constants: id = const(value)
165
166 // get the id
167 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
168
169 // get the value
170 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
171 pn_value = fold_constants(comp, pn_value, consts);
172 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
173 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
174 break;
175 }
Damien George40f3c022014-07-03 13:25:24 +0100176 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000177
178 // store the value in the table of dynamic constants
179 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
180 if (elem->value != MP_OBJ_NULL) {
181 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
182 break;
183 }
184 elem->value = MP_OBJ_NEW_SMALL_INT(value);
185
186 // replace const(value) with value
187 pns1->nodes[0] = pn_value;
188
189 // finished folding this assignment
190 return pn;
191 }
192 }
193 }
194 break;
195#endif
Damien George5042bce2014-05-25 22:06:06 +0100196 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000197 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000198 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100199 return pn;
Damien429d7192013-10-04 19:53:11 +0100200 }
201
Damien Georgeffae48d2014-05-08 15:58:39 +0000202 // fold arguments
203 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
204 for (int i = 0; i < n; i++) {
205 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
206 }
207
208 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000209 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100210 case PN_atom_paren:
211 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
212 // (int)
213 pn = pns->nodes[0];
214 }
215 break;
216
217 case PN_expr:
218 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
219 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100220 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
221 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100222 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
223 }
224 break;
225
226 case PN_and_expr:
227 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
228 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100229 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
230 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100231 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
232 }
233 break;
234
Damien429d7192013-10-04 19:53:11 +0100235 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000236 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100237 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
238 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000239 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100240 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000241 if (!(arg1 >= (mp_int_t)BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
Damien Georgea26dc502014-04-12 17:54:52 +0100242 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
243 }
Damien George0bb97132015-02-27 14:25:47 +0000244 } else {
245 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100246 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000247 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200248 // Shifting to big amounts is underfined behavior
249 // in C and is CPU-dependent; propagate sign bit.
250 arg1 = BITS_PER_WORD - 1;
251 }
Damiend99b0522013-12-21 18:17:45 +0000252 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100253 }
254 }
255 break;
256
257 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100258 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000259 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100260 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
261 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000262 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100263 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000264 arg0 += arg1;
Damien George0bb97132015-02-27 14:25:47 +0000265 } else {
266 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100267 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000268 arg0 -= arg1;
Damien0efb3a12013-10-12 16:16:56 +0100269 }
Damien Georged1e355e2014-05-28 14:51:12 +0100270 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100271 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000272 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100273 }
274 }
275 break;
276
277 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000278 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100279 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
280 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000281 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100282 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000283 if (!mp_small_int_mul_overflow(arg0, arg1)) {
284 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100285 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000286 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
287 }
288 }
Damiend99b0522013-12-21 18:17:45 +0000289 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100290 // int / int
291 // pass
Damiend99b0522013-12-21 18:17:45 +0000292 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100293 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000294 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damien George0bb97132015-02-27 14:25:47 +0000295 } else {
296 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300297 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100298 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000299 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300300 }
Damien429d7192013-10-04 19:53:11 +0100301 }
302 }
303 break;
304
305 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000306 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100307 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000308 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100309 // +int
Damiend99b0522013-12-21 18:17:45 +0000310 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
311 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100312 // -int
Damiend99b0522013-12-21 18:17:45 +0000313 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
Damien George0bb97132015-02-27 14:25:47 +0000314 } else {
315 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100316 // ~int
Damiend99b0522013-12-21 18:17:45 +0000317 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100318 }
319 }
320 break;
321
322 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100323 if (0) {
324#if MICROPY_EMIT_CPYTHON
325 } else if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100326 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100327 // can overflow; enabled only to compare with CPython
Damien George4dea9222015-04-09 15:29:54 +0000328 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
Damiend99b0522013-12-21 18:17:45 +0000329 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200330 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100331 if (power >= 0) {
332 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200333 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100334 for (; power > 0; power--) {
335 ans *= base;
336 }
Damiend99b0522013-12-21 18:17:45 +0000337 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100338 }
339 }
Damien George57e99eb2014-04-10 22:42:11 +0100340#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000341#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100342 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
343 // id.id
344 // look it up in constant table, see if it can be replaced with an integer
Damien George4dea9222015-04-09 15:29:54 +0000345 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George57e99eb2014-04-10 22:42:11 +0100346 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
347 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
348 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
349 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
350 if (elem != NULL) {
351 mp_obj_t dest[2];
352 mp_load_method_maybe(elem->value, q_attr, dest);
353 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100354 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000355 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100356 }
357 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000358#endif
Damien429d7192013-10-04 19:53:11 +0100359 }
360 break;
361 }
362 }
363
364 return pn;
365}
366
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200367STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George6be0b0a2014-08-15 14:30:52 +0100368STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000369STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100370
Damien George6f355fd2014-04-10 14:11:31 +0100371STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100372 return comp->next_label++;
373}
374
Damien George8dcc0c72014-03-27 10:55:21 +0000375STATIC void compile_increase_except_level(compiler_t *comp) {
376 comp->cur_except_level += 1;
377 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
378 comp->scope_cur->exc_stack_size = comp->cur_except_level;
379 }
380}
381
382STATIC void compile_decrease_except_level(compiler_t *comp) {
383 assert(comp->cur_except_level > 0);
384 comp->cur_except_level -= 1;
385}
386
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200387STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgedf8127a2014-04-13 11:04:33 +0100388 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100389 scope->parent = comp->scope_cur;
390 scope->next = NULL;
391 if (comp->scope_head == NULL) {
392 comp->scope_head = scope;
393 } else {
394 scope_t *s = comp->scope_head;
395 while (s->next != NULL) {
396 s = s->next;
397 }
398 s->next = scope;
399 }
400 return scope;
401}
402
Damien George91bc32d2015-04-09 15:31:53 +0000403typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
404
405STATIC 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 +0000406 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000407 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
408 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100409 for (int i = 0; i < num_nodes; i++) {
410 f(comp, pns->nodes[i]);
411 }
Damiend99b0522013-12-21 18:17:45 +0000412 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100413 f(comp, pn);
414 }
415}
416
Damien George969a6b32014-12-10 22:07:04 +0000417STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000418 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100419 for (int i = 0; i < num_nodes; i++) {
420 compile_node(comp, pns->nodes[i]);
421 }
422}
423
Damien George542bd6b2015-03-26 14:42:40 +0000424STATIC void compile_load_id(compiler_t *comp, qstr qst) {
425 if (comp->pass == MP_PASS_SCOPE) {
426 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
427 } else {
Damien George41125902015-03-26 16:44:14 +0000428 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000429 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000430 #else
431 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
432 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000433 }
434}
435
436STATIC void compile_store_id(compiler_t *comp, qstr qst) {
437 if (comp->pass == MP_PASS_SCOPE) {
438 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
439 } else {
Damien George41125902015-03-26 16:44:14 +0000440 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000441 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000442 #else
443 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
444 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000445 }
446}
447
448STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
449 if (comp->pass == MP_PASS_SCOPE) {
450 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
451 } else {
Damien George41125902015-03-26 16:44:14 +0000452 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000453 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000454 #else
455 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
456 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000457 }
458}
459
Damien3ef4abb2013-10-12 16:53:13 +0100460#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200461STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100462 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
463 return true;
464 }
Damien George4c81ba82015-01-13 16:21:23 +0000465 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
466 return true;
467 }
Damien George7d414a12015-02-08 01:57:40 +0000468 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
469 return true;
470 }
Damiend99b0522013-12-21 18:17:45 +0000471 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100472 return false;
473 }
Damiend99b0522013-12-21 18:17:45 +0000474 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100475 return false;
476 }
477 return true;
478}
479
Damien George5042bce2014-05-25 22:06:06 +0100480STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000481 bool has_single_quote = false;
482 bool has_double_quote = false;
483 for (int i = 0; i < len; i++) {
484 if (str[i] == '\'') {
485 has_single_quote = true;
486 } else if (str[i] == '"') {
487 has_double_quote = true;
488 }
489 }
490 if (bytes) {
491 vstr_printf(vstr, "b");
492 }
493 bool quote_single = false;
494 if (has_single_quote && !has_double_quote) {
495 vstr_printf(vstr, "\"");
496 } else {
497 quote_single = true;
498 vstr_printf(vstr, "'");
499 }
500 for (int i = 0; i < len; i++) {
501 if (str[i] == '\n') {
502 vstr_printf(vstr, "\\n");
503 } else if (str[i] == '\\') {
504 vstr_printf(vstr, "\\\\");
505 } else if (str[i] == '\'' && quote_single) {
506 vstr_printf(vstr, "\\'");
507 } else {
508 vstr_printf(vstr, "%c", str[i]);
509 }
510 }
511 if (has_single_quote && !has_double_quote) {
512 vstr_printf(vstr, "\"");
513 } else {
514 vstr_printf(vstr, "'");
515 }
516}
517
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200518STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000519 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 +0100520 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000521 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 +0100522 return;
523 }
524
Damien George7d414a12015-02-08 01:57:40 +0000525 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
526 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
527 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
528 return;
529 }
530
Damiend99b0522013-12-21 18:17:45 +0000531 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200532 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
533 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
534 return;
535 }
536
Damien George42f3de92014-10-03 17:44:14 +0000537 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000538 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
539 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100540 case MP_PARSE_NODE_STRING:
541 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100542 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100543 const byte *str = qstr_data(arg, &len);
544 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
545 break;
546 }
Damiend99b0522013-12-21 18:17:45 +0000547 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100548 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000549 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
550 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
551 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100552 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100553 }
554 break;
555 default: assert(0);
556 }
557}
558
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200559STATIC 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 +0100560 int n = 0;
561 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000562 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100563 }
564 int total = n;
565 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000566 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100567 total += 1;
Damien3a205172013-10-12 15:01:56 +0100568 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100569 is_const = false;
570 }
571 }
572 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100573 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100574 is_const = false;
575 break;
576 }
577 }
578 if (total > 0 && is_const) {
579 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000580 vstr_t *vstr = vstr_new();
581 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000582 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000583 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100584 need_comma = true;
585 }
586 for (int i = 0; i < n; i++) {
587 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000588 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100589 }
Damien02f89412013-12-12 15:13:36 +0000590 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100591 need_comma = true;
592 }
593 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000594 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100595 } else {
Damien02f89412013-12-12 15:13:36 +0000596 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100597 }
Damien George0d3cb672015-01-28 23:43:01 +0000598 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000599 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100600 } else {
Damiend99b0522013-12-21 18:17:45 +0000601 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100602 compile_node(comp, pn);
603 }
604 for (int i = 0; i < n; i++) {
605 compile_node(comp, pns_list->nodes[i]);
606 }
Damien Georgeb9791222014-01-23 00:34:21 +0000607 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100608 }
609}
Damien3a205172013-10-12 15:01:56 +0100610#endif
611
612// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100613STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100614#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100615 cpython_c_tuple(comp, pn, pns_list);
616#else
617 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000618 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100619 compile_node(comp, pn);
620 total += 1;
621 }
622 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000623 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100624 for (int i = 0; i < n; i++) {
625 compile_node(comp, pns_list->nodes[i]);
626 }
627 total += n;
628 }
Damien Georgeb9791222014-01-23 00:34:21 +0000629 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100630#endif
631}
Damien429d7192013-10-04 19:53:11 +0100632
Damien George969a6b32014-12-10 22:07:04 +0000633STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100634 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000635 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100636}
637
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200638STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000639 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
640 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100641}
642
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200643STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000644 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
645 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100646}
647
Damien3ef4abb2013-10-12 16:53:13 +0100648#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100649// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200650STATIC 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 +0100651 if (node_is_const_false(pn)) {
652 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000653 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100654 }
655 return;
656 } else if (node_is_const_true(pn)) {
657 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000658 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100659 }
660 return;
Damiend99b0522013-12-21 18:17:45 +0000661 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
662 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
663 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
664 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100665 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100666 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100667 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100668 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100669 }
Damien3a205172013-10-12 15:01:56 +0100670 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000671 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100672 } else {
673 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100674 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100675 }
676 }
677 return;
Damiend99b0522013-12-21 18:17:45 +0000678 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100679 if (jump_if == false) {
680 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100681 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100682 }
683 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100684 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100685 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100686 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100687 }
Damien3a205172013-10-12 15:01:56 +0100688 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000689 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100690 }
691 return;
Damiend99b0522013-12-21 18:17:45 +0000692 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100693 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100694 return;
695 }
696 }
697
698 // nothing special, fall back to default compiling for node and jump
699 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000700 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100701}
Damien3a205172013-10-12 15:01:56 +0100702#endif
Damien429d7192013-10-04 19:53:11 +0100703
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200704STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100705#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100706 cpython_c_if_cond(comp, pn, jump_if, label, false);
707#else
708 if (node_is_const_false(pn)) {
709 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000710 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100711 }
712 return;
713 } else if (node_is_const_true(pn)) {
714 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000715 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100716 }
717 return;
Damiend99b0522013-12-21 18:17:45 +0000718 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
719 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
720 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
721 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100722 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000723 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100724 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100725 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000726 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100727 }
Damien George0b2fd912015-02-28 14:37:54 +0000728 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000729 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100730 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000731 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100732 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000733 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100734 }
735 }
736 return;
Damiend99b0522013-12-21 18:17:45 +0000737 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100738 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000739 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100740 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000741 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100742 }
Damiend99b0522013-12-21 18:17:45 +0000743 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100744 c_if_cond(comp, pns->nodes[0], !jump_if, label);
745 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100746 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
747 // cond is something in parenthesis
748 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
749 // empty tuple, acts as false for the condition
750 if (jump_if == false) {
751 EMIT_ARG(jump, label);
752 }
753 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
754 // non-empty tuple, acts as true for the condition
755 if (jump_if == true) {
756 EMIT_ARG(jump, label);
757 }
758 } else {
759 // parenthesis around 1 item, is just that item
760 c_if_cond(comp, pns->nodes[0], jump_if, label);
761 }
762 return;
Damien3a205172013-10-12 15:01:56 +0100763 }
764 }
765
766 // nothing special, fall back to default compiling for node and jump
767 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000768 EMIT_ARG(pop_jump_if, jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100769#endif
Damien429d7192013-10-04 19:53:11 +0100770}
771
772typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100773STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100774
Damien George6be0b0a2014-08-15 14:30:52 +0100775STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100776 if (assign_kind != ASSIGN_AUG_STORE) {
777 compile_node(comp, pns->nodes[0]);
778 }
779
Damiend99b0522013-12-21 18:17:45 +0000780 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
781 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
782 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
783 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100784 if (assign_kind != ASSIGN_AUG_STORE) {
785 for (int i = 0; i < n - 1; i++) {
786 compile_node(comp, pns1->nodes[i]);
787 }
788 }
Damiend99b0522013-12-21 18:17:45 +0000789 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
790 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100791 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000792 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100793 if (assign_kind == ASSIGN_AUG_STORE) {
794 EMIT(rot_three);
795 EMIT(store_subscr);
796 } else {
797 compile_node(comp, pns1->nodes[0]);
798 if (assign_kind == ASSIGN_AUG_LOAD) {
799 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100800 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100801 } else {
802 EMIT(store_subscr);
803 }
804 }
Damiend99b0522013-12-21 18:17:45 +0000805 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
806 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100807 if (assign_kind == ASSIGN_AUG_LOAD) {
808 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000809 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100810 } else {
811 if (assign_kind == ASSIGN_AUG_STORE) {
812 EMIT(rot_two);
813 }
Damien Georgeb9791222014-01-23 00:34:21 +0000814 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100815 }
816 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100817 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100818 }
819 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100820 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100821 }
822
Damiend99b0522013-12-21 18:17:45 +0000823 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100824 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100825 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100826
827 return;
828
829cannot_assign:
830 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100831}
832
Damien George0288cf02014-04-11 11:53:00 +0000833// 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 +0100834STATIC 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 +0000835 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
836
837 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000838 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000839 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
840 EMIT_ARG(unpack_ex, 0, num_tail);
841 have_star_index = 0;
842 }
Damien George963a5a32015-01-16 17:47:07 +0000843 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000844 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000845 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000846 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
847 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100848 } else {
Damien George9d181f62014-04-27 16:55:27 +0100849 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100850 return;
851 }
852 }
853 }
Damien George963a5a32015-01-16 17:47:07 +0000854 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000855 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100856 }
Damien George0288cf02014-04-11 11:53:00 +0000857 if (num_head != 0) {
858 if (0 == have_star_index) {
859 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100860 } else {
Damien George0288cf02014-04-11 11:53:00 +0000861 c_assign(comp, node_head, ASSIGN_STORE);
862 }
863 }
Damien George963a5a32015-01-16 17:47:07 +0000864 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000865 if (num_head + i == have_star_index) {
866 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
867 } else {
868 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100869 }
870 }
871}
872
873// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100874STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100875 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000876 assert(!MP_PARSE_NODE_IS_NULL(pn));
877 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000878 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000879 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100880 switch (assign_kind) {
881 case ASSIGN_STORE:
882 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000883 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100884 break;
885 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000886 default:
Damien George542bd6b2015-03-26 14:42:40 +0000887 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100888 break;
889 }
890 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100891 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100892 return;
893 }
894 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000895 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000896 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
897 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100898 case PN_power:
899 // lhs is an index or attribute
900 c_assign_power(comp, pns, assign_kind);
901 break;
902
903 case PN_testlist_star_expr:
904 case PN_exprlist:
905 // lhs is a tuple
906 if (assign_kind != ASSIGN_STORE) {
907 goto bad_aug;
908 }
Damien George0288cf02014-04-11 11:53:00 +0000909 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100910 break;
911
912 case PN_atom_paren:
913 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000914 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100915 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100916 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000917 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000918 if (assign_kind != ASSIGN_STORE) {
919 goto bad_aug;
920 }
Damiend99b0522013-12-21 18:17:45 +0000921 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100922 goto testlist_comp;
923 } else {
924 // parenthesis around 1 item, is just that item
925 pn = pns->nodes[0];
926 goto tail_recursion;
927 }
928 break;
929
930 case PN_atom_bracket:
931 // lhs is something in brackets
932 if (assign_kind != ASSIGN_STORE) {
933 goto bad_aug;
934 }
Damiend99b0522013-12-21 18:17:45 +0000935 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100936 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000937 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000938 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
939 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100940 goto testlist_comp;
941 } else {
942 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000943 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100944 }
945 break;
946
947 default:
Damien George9d181f62014-04-27 16:55:27 +0100948 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100949 }
950 return;
951
952 testlist_comp:
953 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000954 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
955 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
956 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100957 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000958 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000959 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000960 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100961 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000962 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
963 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000964 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100965 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100966 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100967 } else {
968 // sequence with 2 items
969 goto sequence_with_2_items;
970 }
971 } else {
972 // sequence with 2 items
973 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000974 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100975 }
976 return;
977 }
978 return;
979
Damien George9d181f62014-04-27 16:55:27 +0100980 cannot_assign:
981 compile_syntax_error(comp, pn, "can't assign to expression");
982 return;
983
Damien429d7192013-10-04 19:53:11 +0100984 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100985 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100986}
987
988// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100989// if we are not in CPython compatibility mode then:
990// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
991// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
992// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100993STATIC 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 +0100994 assert(n_pos_defaults >= 0);
995 assert(n_kw_defaults >= 0);
996
Damien429d7192013-10-04 19:53:11 +0100997 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000998 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100999 int nfree = 0;
1000 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +00001001 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
1002 id_info_t *id = &comp->scope_cur->id_info[i];
1003 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
1004 for (int j = 0; j < this_scope->id_info_len; j++) {
1005 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01001006 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +00001007#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +01001008 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001009#else
1010 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +00001011 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001012#endif
Damien318aec62013-12-10 18:28:17 +00001013 nfree += 1;
1014 }
1015 }
Damien429d7192013-10-04 19:53:11 +01001016 }
1017 }
1018 }
Damien429d7192013-10-04 19:53:11 +01001019
1020 // make the function/closure
1021 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001022 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001023 } else {
Damien George3558f622014-04-20 17:50:40 +01001024 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001025 }
1026}
1027
Damien George6be0b0a2014-08-15 14:30:52 +01001028STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001029 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001030 comp->have_star = true;
1031 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001032 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1033 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001034 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001035 } else {
1036 // named star
Damien429d7192013-10-04 19:53:11 +01001037 }
Damien George8b19db02014-04-11 23:25:34 +01001038 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001039
1040 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001041 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001042 // TODO do we need to do anything with this?
1043
1044 } else {
1045 mp_parse_node_t pn_id;
1046 mp_parse_node_t pn_colon;
1047 mp_parse_node_t pn_equal;
1048 if (MP_PARSE_NODE_IS_ID(pn)) {
1049 // this parameter is just an id
1050
1051 pn_id = pn;
1052 pn_colon = MP_PARSE_NODE_NULL;
1053 pn_equal = MP_PARSE_NODE_NULL;
1054
Damien George0bb97132015-02-27 14:25:47 +00001055 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001056 // this parameter has a colon and/or equal specifier
1057
Damien George0bb97132015-02-27 14:25:47 +00001058 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1059
Damien Georgef41fdd02014-03-03 23:19:11 +00001060 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1061 pn_id = pns->nodes[0];
1062 pn_colon = pns->nodes[1];
1063 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +00001064 }
1065
1066 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1067 // this parameter does not have a default value
1068
1069 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001070 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001071 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001072 return;
1073 }
1074
1075 } else {
1076 // this parameter has a default value
1077 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1078
Damien George8b19db02014-04-11 23:25:34 +01001079 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001080 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001081#if MICROPY_EMIT_CPYTHON
1082 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1083 compile_node(comp, pn_equal);
1084#else
Damien George69b89d22014-04-11 13:38:30 +00001085 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1086 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001087 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1088 // we need to do this here before we start building the map for the default keywords
1089 if (comp->num_default_params > 0) {
1090 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001091 } else {
1092 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001093 }
Damien George69b89d22014-04-11 13:38:30 +00001094 // first default dict param, so make the map
1095 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001096 }
Damien Georgef0778a72014-06-07 22:01:00 +01001097
1098 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001099 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001100 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001101 EMIT(store_map);
1102#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001103 } else {
Damien George69b89d22014-04-11 13:38:30 +00001104 comp->num_default_params += 1;
1105 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001106 }
1107 }
1108
1109 // TODO pn_colon not implemented
1110 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001111 }
1112}
1113
1114// leaves function object on stack
1115// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001116STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001117 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001118 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001119 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001120 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001121 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001122 }
1123
1124 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001125 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001126 uint old_num_dict_params = comp->num_dict_params;
1127 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001128
1129 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001130 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001131 comp->num_dict_params = 0;
1132 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001133 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001134
Damien Georgea91ac202014-10-05 19:01:34 +01001135 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001136 return MP_QSTR_NULL;
1137 }
1138
Damien Georgee337f1e2014-03-31 15:18:37 +01001139#if !MICROPY_EMIT_CPYTHON
1140 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001141 // the default keywords args may have already made the tuple; if not, do it now
1142 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001143 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001144 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001145 }
1146#endif
1147
Damien429d7192013-10-04 19:53:11 +01001148 // get the scope for this function
1149 scope_t *fscope = (scope_t*)pns->nodes[4];
1150
1151 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001152 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001153
1154 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001155 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001156 comp->num_dict_params = old_num_dict_params;
1157 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001158
1159 // return its name (the 'f' in "def f(...):")
1160 return fscope->simple_name;
1161}
1162
1163// leaves class object on stack
1164// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001165STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001166 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001167 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001168 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001169 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001170 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001171 }
1172
1173 EMIT(load_build_class);
1174
1175 // scope for this class
1176 scope_t *cscope = (scope_t*)pns->nodes[3];
1177
1178 // compile the class
1179 close_over_variables_etc(comp, cscope, 0, 0);
1180
1181 // get its name
Damien George968bf342014-04-27 19:12:05 +01001182 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001183
1184 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001185 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1186 mp_parse_node_t parents = pns->nodes[1];
1187 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1188 parents = MP_PARSE_NODE_NULL;
1189 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001190 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001191 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001192
1193 // return its name (the 'C' in class C(...):")
1194 return cscope->simple_name;
1195}
1196
Damien6cdd3af2013-10-05 18:08:26 +01001197// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001198STATIC 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 +00001199 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001200 return false;
1201 }
1202
1203 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001204 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001205 return true;
1206 }
1207
Damiend99b0522013-12-21 18:17:45 +00001208 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001209 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001210 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001211#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001212 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001213 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001214 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001215 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001216#endif
Damien3ef4abb2013-10-12 16:53:13 +01001217#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001218 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001219 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001220#endif
Damien6cdd3af2013-10-05 18:08:26 +01001221 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001222 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001223 }
1224
1225 return true;
1226}
1227
Damien George969a6b32014-12-10 22:07:04 +00001228STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001229 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001230 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001231 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001232
Damien6cdd3af2013-10-05 18:08:26 +01001233 // inherit emit options for this function/class definition
1234 uint emit_options = comp->scope_cur->emit_options;
1235
1236 // compile each decorator
1237 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001238 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001239 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1240 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001241
1242 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001243 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001244 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001245
1246 // check for built-in decorators
1247 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1248 // this was a built-in
1249 num_built_in_decorators += 1;
1250
1251 } else {
1252 // not a built-in, compile normally
1253
1254 // compile the decorator function
1255 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001256 for (int j = 1; j < name_len; j++) {
1257 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1258 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001259 }
1260
1261 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001262 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001263 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001264 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001265 compile_node(comp, pns_decorator->nodes[1]);
1266 }
Damien429d7192013-10-04 19:53:11 +01001267 }
1268 }
1269
1270 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001271 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001272 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001273 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001274 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001275 } else {
Damien George0bb97132015-02-27 14:25:47 +00001276 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1277 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001278 }
1279
1280 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001281 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001282 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001283 }
1284
1285 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +00001286 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +01001287}
1288
Damien George969a6b32014-12-10 22:07:04 +00001289STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001290 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001291 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +00001292 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +01001293}
1294
Damien George6be0b0a2014-08-15 14:30:52 +01001295STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001296 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00001297 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001298 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1299 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001300
1301 compile_node(comp, pns->nodes[0]); // base of the power node
1302
Damiend99b0522013-12-21 18:17:45 +00001303 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1304 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1305 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1306 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001307 for (int i = 0; i < n - 1; i++) {
1308 compile_node(comp, pns1->nodes[i]);
1309 }
Damiend99b0522013-12-21 18:17:45 +00001310 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1311 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001312 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001313 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001314 compile_node(comp, pns1->nodes[0]);
1315 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001316 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1317 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001318 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001319 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001320 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001321 }
1322 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001323 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001324 }
1325
Damiend99b0522013-12-21 18:17:45 +00001326 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001327 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001328 }
Damiend99b0522013-12-21 18:17:45 +00001329 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1330 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1331 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1332 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001333 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1334
Damiend99b0522013-12-21 18:17:45 +00001335 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1336 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1337 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001338 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001339 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001340 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001341 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001342 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001343 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001344 c_del_stmt(comp, pns->nodes[0]);
1345 for (int i = 0; i < n; i++) {
1346 c_del_stmt(comp, pns1->nodes[i]);
1347 }
Damiend99b0522013-12-21 18:17:45 +00001348 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001349 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001350 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001351 } else {
1352 // sequence with 2 items
1353 goto sequence_with_2_items;
1354 }
1355 } else {
1356 // sequence with 2 items
1357 sequence_with_2_items:
1358 c_del_stmt(comp, pns->nodes[0]);
1359 c_del_stmt(comp, pns->nodes[1]);
1360 }
1361 } else {
1362 // tuple with 1 element
1363 c_del_stmt(comp, pn);
1364 }
1365 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001366 // TODO is there anything else to implement?
1367 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001368 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001369
1370 return;
1371
1372cannot_delete:
1373 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001374}
1375
Damien George969a6b32014-12-10 22:07:04 +00001376STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001377 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1378}
1379
Damien George969a6b32014-12-10 22:07:04 +00001380STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001381 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001382 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001383 }
Damien George090c9232014-10-17 14:08:49 +00001384 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001385 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001386}
1387
Damien George969a6b32014-12-10 22:07:04 +00001388STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001389 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001390 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001391 }
Damien George090c9232014-10-17 14:08:49 +00001392 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001393 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001394}
1395
Damien George969a6b32014-12-10 22:07:04 +00001396STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001397 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001398 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001399 return;
1400 }
Damiend99b0522013-12-21 18:17:45 +00001401 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001402 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001403 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001404 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001405 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001406 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1407 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 +01001408
Damien George6f355fd2014-04-10 14:11:31 +01001409 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001410 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1411 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1412 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001413 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001414 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1415 } else {
1416 compile_node(comp, pns->nodes[0]);
1417 }
1418 EMIT(return_value);
1419}
1420
Damien George969a6b32014-12-10 22:07:04 +00001421STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001422 compile_node(comp, pns->nodes[0]);
1423 EMIT(pop_top);
1424}
1425
Damien George969a6b32014-12-10 22:07:04 +00001426STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001427 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001428 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001429 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001430 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001431 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001432 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001433 compile_node(comp, pns->nodes[0]);
1434 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001435 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001436 } else {
1437 // raise x
1438 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001439 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001440 }
1441}
1442
Damien George635543c2014-04-10 12:56:52 +01001443// q_base holds the base of the name
1444// eg a -> q_base=a
1445// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001446STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001447 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001448 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1449 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001450 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001451 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001452 pn = pns->nodes[0];
1453 is_as = true;
1454 }
Damien George635543c2014-04-10 12:56:52 +01001455 if (MP_PARSE_NODE_IS_NULL(pn)) {
1456 // empty name (eg, from . import x)
1457 *q_base = MP_QSTR_;
1458 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1459 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001460 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001461 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001462 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001463 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001464 }
Damien George635543c2014-04-10 12:56:52 +01001465 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001466 } else {
1467 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001468 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001469 {
Damien429d7192013-10-04 19:53:11 +01001470 // a name of the form a.b.c
1471 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001472 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001473 }
Damiend99b0522013-12-21 18:17:45 +00001474 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001475 int len = n - 1;
1476 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001477 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001478 }
Damien George55baff42014-01-21 21:40:13 +00001479 byte *q_ptr;
1480 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001481 for (int i = 0; i < n; i++) {
1482 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001483 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001484 }
Damien George39dc1452014-10-03 19:52:22 +01001485 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001486 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001487 memcpy(str_dest, str_src, str_src_len);
1488 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001489 }
Damien George635543c2014-04-10 12:56:52 +01001490 qstr q_full = qstr_build_end(q_ptr);
1491 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001492 if (is_as) {
1493 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001494 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001495 }
1496 }
Damien429d7192013-10-04 19:53:11 +01001497 }
Damien429d7192013-10-04 19:53:11 +01001498 }
1499}
1500
Damien George6be0b0a2014-08-15 14:30:52 +01001501STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001502 EMIT_ARG(load_const_small_int, 0); // level 0 import
1503 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1504 qstr q_base;
1505 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001506 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001507}
1508
Damien George969a6b32014-12-10 22:07:04 +00001509STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001510 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1511}
1512
Damien George969a6b32014-12-10 22:07:04 +00001513STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001514 mp_parse_node_t pn_import_source = pns->nodes[0];
1515
1516 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1517 uint import_level = 0;
1518 do {
1519 mp_parse_node_t pn_rel;
1520 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)) {
1521 // This covers relative imports with dots only like "from .. import"
1522 pn_rel = pn_import_source;
1523 pn_import_source = MP_PARSE_NODE_NULL;
1524 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1525 // This covers relative imports starting with dot(s) like "from .foo import"
1526 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1527 pn_rel = pns_2b->nodes[0];
1528 pn_import_source = pns_2b->nodes[1];
1529 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1530 } else {
1531 // Not a relative import
1532 break;
1533 }
1534
1535 // get the list of . and/or ...'s
1536 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001537 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001538
1539 // count the total number of .'s
1540 for (int i = 0; i < n; i++) {
1541 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1542 import_level++;
1543 } else {
1544 // should be an MP_TOKEN_ELLIPSIS
1545 import_level += 3;
1546 }
1547 }
1548 } while (0);
1549
Damiend99b0522013-12-21 18:17:45 +00001550 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001551 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001552
1553 // build the "fromlist" tuple
1554#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001555 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001556#else
Damien George708c0732014-04-27 19:23:46 +01001557 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001558 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001559#endif
1560
1561 // do the import
Damien George635543c2014-04-10 12:56:52 +01001562 qstr dummy_q;
1563 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001564 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001565
Damien429d7192013-10-04 19:53:11 +01001566 } else {
Damien George635543c2014-04-10 12:56:52 +01001567 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001568
1569 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001570 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001571 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001572#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001573 {
1574 vstr_t *vstr = vstr_new();
1575 vstr_printf(vstr, "(");
1576 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001577 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1578 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1579 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001580 if (i > 0) {
1581 vstr_printf(vstr, ", ");
1582 }
1583 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001584 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001585 const byte *str = qstr_data(id2, &len);
1586 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001587 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001588 }
Damien02f89412013-12-12 15:13:36 +00001589 if (n == 1) {
1590 vstr_printf(vstr, ",");
1591 }
1592 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001593 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001594 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001595 }
Damiendb4c3612013-12-10 17:27:24 +00001596#else
1597 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001598 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1599 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1600 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001601 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001602 }
Damien Georgeb9791222014-01-23 00:34:21 +00001603 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001604#endif
1605
1606 // do the import
Damien George635543c2014-04-10 12:56:52 +01001607 qstr dummy_q;
1608 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001609 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001610 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1611 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1612 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001613 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001614 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001615 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001616 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001617 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001618 }
1619 }
1620 EMIT(pop_top);
1621 }
1622}
1623
Damien George584ba672014-12-21 17:26:45 +00001624STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1625 bool added;
1626 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1627 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001628 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001629 compile_syntax_error(comp, pn, "identifier already used");
1630 return;
1631 }
1632 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1633
1634 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1635 id_info = scope_find_global(comp->scope_cur, qst);
1636 if (id_info != NULL) {
1637 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1638 }
1639}
1640
Damien George969a6b32014-12-10 22:07:04 +00001641STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001642 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001643 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001644 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001645 for (int i = 0; i < n; i++) {
1646 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001647 }
1648 }
1649}
1650
Damien George584ba672014-12-21 17:26:45 +00001651STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1652 bool added;
1653 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1654 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001655 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001656 compile_syntax_error(comp, pn, "identifier already used");
1657 return;
1658 }
1659 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1660 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)) {
1661 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1662 return;
1663 }
1664 id_info->kind = ID_INFO_KIND_FREE;
1665 scope_close_over_in_parents(comp->scope_cur, qst);
1666}
1667
Damien George969a6b32014-12-10 22:07:04 +00001668STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001669 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001670 if (comp->scope_cur->kind == SCOPE_MODULE) {
1671 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1672 return;
1673 }
1674 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001675 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001676 for (int i = 0; i < n; i++) {
1677 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001678 }
1679 }
1680}
1681
Damien George969a6b32014-12-10 22:07:04 +00001682STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001683 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001684 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001685 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001686 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001687 // assertion message
1688 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001689 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001690 }
Damien Georgeb9791222014-01-23 00:34:21 +00001691 EMIT_ARG(raise_varargs, 1);
1692 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001693}
1694
Damien George969a6b32014-12-10 22:07:04 +00001695STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001696 // TODO proper and/or short circuiting
1697
Damien George6f355fd2014-04-10 14:11:31 +01001698 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001699
Damien George391db862014-10-17 17:57:33 +00001700 // optimisation: don't emit anything when "if False" (not in CPython)
1701 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1702 uint l_fail = comp_next_label(comp);
1703 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001704
Damien George391db862014-10-17 17:57:33 +00001705 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001706
Damien George391db862014-10-17 17:57:33 +00001707 // optimisation: skip everything else when "if True" (not in CPython)
1708 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1709 goto done;
1710 }
1711
1712 if (
1713 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1714 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1715 // optimisation: don't jump if last instruction was return
1716 && !EMIT(last_emit_was_return_value)
1717 ) {
1718 // jump over elif/else blocks
1719 EMIT_ARG(jump, l_end);
1720 }
1721
1722 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001723 }
1724
Damien George235f9b32014-10-17 17:30:16 +00001725 // compile elif blocks (if any)
1726 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001727 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 +00001728 for (int i = 0; i < n_elif; i++) {
1729 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1730 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001731
Damien George391db862014-10-17 17:57:33 +00001732 // optimisation: don't emit anything when "if False" (not in CPython)
1733 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1734 uint l_fail = comp_next_label(comp);
1735 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001736
Damien George391db862014-10-17 17:57:33 +00001737 compile_node(comp, pns_elif->nodes[1]); // elif block
1738
1739 // optimisation: skip everything else when "elif True" (not in CPython)
1740 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1741 goto done;
1742 }
1743
1744 // optimisation: don't jump if last instruction was return
1745 if (!EMIT(last_emit_was_return_value)) {
1746 EMIT_ARG(jump, l_end);
1747 }
1748 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001749 }
1750 }
1751
1752 // compile else block
1753 compile_node(comp, pns->nodes[3]); // can be null
1754
Damien George391db862014-10-17 17:57:33 +00001755done:
Damien Georgeb9791222014-01-23 00:34:21 +00001756 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001757}
1758
Damien Georgecbddb272014-02-01 20:08:18 +00001759#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001760 uint16_t old_break_label = comp->break_label; \
1761 uint16_t old_continue_label = comp->continue_label; \
1762 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001763 uint break_label = comp_next_label(comp); \
1764 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001765 comp->break_label = break_label; \
1766 comp->continue_label = continue_label; \
1767 comp->break_continue_except_level = comp->cur_except_level;
1768
1769#define END_BREAK_CONTINUE_BLOCK \
1770 comp->break_label = old_break_label; \
1771 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001772 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001773
Damien George969a6b32014-12-10 22:07:04 +00001774STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001775 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001776
Damience89a212013-10-15 22:25:17 +01001777 // compared to CPython, we have an optimised version of while loops
1778#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001779 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001780 EMIT_ARG(setup_loop, break_label);
1781 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001782 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1783 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001784 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001785 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001786 }
Damien Georgeb9791222014-01-23 00:34:21 +00001787 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001788 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1789 // this is a small hack to agree with CPython
1790 if (!node_is_const_true(pns->nodes[0])) {
1791 EMIT(pop_block);
1792 }
Damience89a212013-10-15 22:25:17 +01001793#else
Damien George391db862014-10-17 17:57:33 +00001794 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1795 uint top_label = comp_next_label(comp);
1796 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1797 EMIT_ARG(jump, continue_label);
1798 }
1799 EMIT_ARG(label_assign, top_label);
1800 compile_node(comp, pns->nodes[1]); // body
1801 EMIT_ARG(label_assign, continue_label);
1802 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1803 }
Damience89a212013-10-15 22:25:17 +01001804#endif
1805
1806 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001807 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001808
1809 compile_node(comp, pns->nodes[2]); // else
1810
Damien Georgeb9791222014-01-23 00:34:21 +00001811 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001812}
1813
Damien George2ac4af62014-08-15 16:45:41 +01001814#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001815// This function compiles an optimised for-loop of the form:
1816// for <var> in range(<start>, <end>, <step>):
1817// <body>
1818// else:
1819// <else>
1820// <var> must be an identifier and <step> must be a small-int.
1821//
1822// Semantics of for-loop require:
1823// - final failing value should not be stored in the loop variable
1824// - if the loop never runs, the loop variable should never be assigned
1825// - assignments to <var>, <end> or <step> in the body do not alter the loop
1826// (<step> is a constant for us, so no need to worry about it changing)
1827//
1828// If <end> is a small-int, then the stack during the for-loop contains just
1829// the current value of <var>. Otherwise, the stack contains <end> then the
1830// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001831STATIC 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 +00001832 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001833
Damien George6f355fd2014-04-10 14:11:31 +01001834 uint top_label = comp_next_label(comp);
1835 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001836
Damien Georgee181c0d2014-12-12 17:19:56 +00001837 // put the end value on the stack if it's not a small-int constant
1838 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1839 if (end_on_stack) {
1840 compile_node(comp, pn_end);
1841 }
1842
1843 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001844 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001845
Damien Georgeb9791222014-01-23 00:34:21 +00001846 EMIT_ARG(jump, entry_label);
1847 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001848
Damien Georgec33ce602014-12-11 17:35:23 +00001849 // duplicate next value and store it to var
1850 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001851 c_assign(comp, pn_var, ASSIGN_STORE);
1852
Damienf3822fc2013-11-09 20:12:03 +00001853 // compile body
1854 compile_node(comp, pn_body);
1855
Damien Georgeb9791222014-01-23 00:34:21 +00001856 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001857
Damien Georgee181c0d2014-12-12 17:19:56 +00001858 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001859 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001860 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001861
Damien Georgeb9791222014-01-23 00:34:21 +00001862 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001863
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001864 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001865 if (end_on_stack) {
1866 EMIT(dup_top_two);
1867 EMIT(rot_two);
1868 } else {
1869 EMIT(dup_top);
1870 compile_node(comp, pn_end);
1871 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001872 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1873 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001874 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001875 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001876 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001877 }
Damien George63f38322015-02-28 15:04:06 +00001878 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001879
1880 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001881 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001882
1883 compile_node(comp, pn_else);
1884
Damien Georgeb9791222014-01-23 00:34:21 +00001885 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001886
1887 // discard final value of var that failed the loop condition
1888 EMIT(pop_top);
1889
1890 // discard <end> value if it's on the stack
1891 if (end_on_stack) {
1892 EMIT(pop_top);
1893 }
Damienf72fd0e2013-11-06 20:20:49 +00001894}
Damien George2ac4af62014-08-15 16:45:41 +01001895#endif
Damienf72fd0e2013-11-06 20:20:49 +00001896
Damien George969a6b32014-12-10 22:07:04 +00001897STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001898#if !MICROPY_EMIT_CPYTHON
1899 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1900 // this is actually slower, but uses no heap memory
1901 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001902 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 +00001903 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001904 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1905 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1906 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1907 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001908 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1909 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001910 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001911 mp_parse_node_t pn_range_start;
1912 mp_parse_node_t pn_range_end;
1913 mp_parse_node_t pn_range_step;
1914 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001915 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001916 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001917 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001918 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001919 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001920 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001921 } else if (n_args == 2) {
1922 pn_range_start = args[0];
1923 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001924 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001925 } else {
1926 pn_range_start = args[0];
1927 pn_range_end = args[1];
1928 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001929 // We need to know sign of step. This is possible only if it's constant
1930 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1931 optimize = false;
1932 }
Damienf72fd0e2013-11-06 20:20:49 +00001933 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001934 }
1935 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001936 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1937 return;
1938 }
1939 }
1940 }
1941#endif
1942
Damien Georgecbddb272014-02-01 20:08:18 +00001943 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001944 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001945
Damien George6f355fd2014-04-10 14:11:31 +01001946 uint pop_label = comp_next_label(comp);
1947 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001948
Damience89a212013-10-15 22:25:17 +01001949 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1950#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001951 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001952#endif
1953
Damien429d7192013-10-04 19:53:11 +01001954 compile_node(comp, pns->nodes[1]); // iterator
1955 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001956 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001957 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001958 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1959 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001960 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001961 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001962 }
Damien Georgeb9791222014-01-23 00:34:21 +00001963 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001964 EMIT(for_iter_end);
1965
1966 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001967 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001968
Damience89a212013-10-15 22:25:17 +01001969#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001970 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001971#endif
Damien429d7192013-10-04 19:53:11 +01001972
1973 compile_node(comp, pns->nodes[3]); // else (not tested)
1974
Damien Georgeb9791222014-01-23 00:34:21 +00001975 EMIT_ARG(label_assign, break_label);
1976 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001977}
1978
Damien George6be0b0a2014-08-15 14:30:52 +01001979STATIC 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 +01001980 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001981 uint l1 = comp_next_label(comp);
1982 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001983
Damien Georgeb9791222014-01-23 00:34:21 +00001984 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001985 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001986
Damien429d7192013-10-04 19:53:11 +01001987 compile_node(comp, pn_body); // body
1988 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001989 EMIT_ARG(jump, success_label); // jump over exception handler
1990
1991 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001992 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001993
Damien George6f355fd2014-04-10 14:11:31 +01001994 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001995
1996 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001997 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1998 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001999
2000 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01002001 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002002
Damiend99b0522013-12-21 18:17:45 +00002003 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002004 // this is a catch all exception handler
2005 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002006 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00002007 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002008 return;
2009 }
2010 } else {
2011 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00002012 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
2013 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
2014 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
2015 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01002016 // handler binds the exception to a local
2017 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002018 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002019 }
2020 }
2021 EMIT(dup_top);
2022 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002023 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00002024 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002025 }
2026
2027 EMIT(pop_top);
2028
2029 if (qstr_exception_local == 0) {
2030 EMIT(pop_top);
2031 } else {
Damien George542bd6b2015-03-26 14:42:40 +00002032 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002033 }
2034
2035 EMIT(pop_top);
2036
Damien George6f355fd2014-04-10 14:11:31 +01002037 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002038 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002039 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002040 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002041 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002042 }
2043 compile_node(comp, pns_except->nodes[1]);
2044 if (qstr_exception_local != 0) {
2045 EMIT(pop_block);
2046 }
2047 EMIT(pop_except);
2048 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002049 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2050 EMIT_ARG(label_assign, l3);
2051 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00002052 compile_store_id(comp, qstr_exception_local);
2053 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002054
Damien George8dcc0c72014-03-27 10:55:21 +00002055 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002056 EMIT(end_finally);
2057 }
Damien Georgeb9791222014-01-23 00:34:21 +00002058 EMIT_ARG(jump, l2);
2059 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002060 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002061 }
2062
Damien George8dcc0c72014-03-27 10:55:21 +00002063 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002064 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002065 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002066
Damien Georgeb9791222014-01-23 00:34:21 +00002067 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002068 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002069 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002070}
2071
Damien George6be0b0a2014-08-15 14:30:52 +01002072STATIC 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 +01002073 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002074
Damien Georgeb9791222014-01-23 00:34:21 +00002075 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002076 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002077
Damien429d7192013-10-04 19:53:11 +01002078 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002079 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002080 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002081 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002082 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002083 } else {
2084 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2085 }
2086 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002087 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2088 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002089 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002090
Damien George8dcc0c72014-03-27 10:55:21 +00002091 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002092 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002093}
2094
Damien George969a6b32014-12-10 22:07:04 +00002095STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00002096 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2097 {
Damiend99b0522013-12-21 18:17:45 +00002098 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2099 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002100 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002101 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2102 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002103 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002104 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002105 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002106 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002107 // no finally
2108 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2109 } else {
2110 // have finally
Damiend99b0522013-12-21 18:17:45 +00002111 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 +01002112 }
2113 } else {
2114 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002115 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002116 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002117 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002118 }
Damien429d7192013-10-04 19:53:11 +01002119 }
2120}
2121
Damien George6be0b0a2014-08-15 14:30:52 +01002122STATIC 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 +01002123 if (n == 0) {
2124 // no more pre-bits, compile the body of the with
2125 compile_node(comp, body);
2126 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002127 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002128 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002129 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002130 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002131 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002132 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002133 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2134 } else {
2135 // this pre-bit is just an expression
2136 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002137 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002138 EMIT(pop_top);
2139 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002140 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002141 // compile additional pre-bits and the body
2142 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2143 // finish this with block
2144 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002145 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2146 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002147 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002148 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002149 EMIT(end_finally);
2150 }
2151}
2152
Damien George969a6b32014-12-10 22:07:04 +00002153STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002154 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002155 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002156 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002157 assert(n > 0);
2158
2159 // compile in a nested fashion
2160 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2161}
2162
Damien George969a6b32014-12-10 22:07:04 +00002163STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002164 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002165 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2166 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00002167 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002168 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002169 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002170 EMIT(pop_top);
2171
Damien429d7192013-10-04 19:53:11 +01002172 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002173 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002174 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002175 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002176 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2177 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002178 // do nothing with a lonely constant
2179 } else {
2180 compile_node(comp, pns->nodes[0]); // just an expression
2181 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2182 }
Damien429d7192013-10-04 19:53:11 +01002183 }
2184 } else {
Damiend99b0522013-12-21 18:17:45 +00002185 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2186 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002187 if (kind == PN_expr_stmt_augassign) {
2188 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2189 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002190 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002191 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002192 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002193 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2194 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2195 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2196 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2197 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2198 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2199 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2200 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2201 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2202 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2203 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002204 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002205 }
Damien George7e5fb242014-02-01 22:18:47 +00002206 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002207 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2208 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002209 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2210 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002211 // following CPython, we store left-most first
2212 if (rhs > 0) {
2213 EMIT(dup_top);
2214 }
2215 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2216 for (int i = 0; i < rhs; i++) {
2217 if (i + 1 < rhs) {
2218 EMIT(dup_top);
2219 }
Damiend99b0522013-12-21 18:17:45 +00002220 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002221 }
Damien George0bb97132015-02-27 14:25:47 +00002222 } else {
2223 assert(kind == PN_expr_stmt_assign); // should be
Damien George42e0c592015-03-14 13:11:35 +00002224 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
2225 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002226 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2227 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2228 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002229 // optimisation for a, b = c, d; to match CPython's optimisation
Damien George4dea9222015-04-09 15:29:54 +00002230 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2231 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002232 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2233 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2234 // can't optimise when it's a star expression on the lhs
2235 goto no_optimisation;
2236 }
Damien429d7192013-10-04 19:53:11 +01002237 compile_node(comp, pns10->nodes[0]); // rhs
2238 compile_node(comp, pns10->nodes[1]); // rhs
2239 EMIT(rot_two);
2240 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2241 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00002242 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2243 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002244 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2245 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2246 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002247 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damien George4dea9222015-04-09 15:29:54 +00002248 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2249 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002250 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2251 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2252 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2253 // can't optimise when it's a star expression on the lhs
2254 goto no_optimisation;
2255 }
Damien429d7192013-10-04 19:53:11 +01002256 compile_node(comp, pns10->nodes[0]); // rhs
2257 compile_node(comp, pns10->nodes[1]); // rhs
2258 compile_node(comp, pns10->nodes[2]); // rhs
2259 EMIT(rot_three);
2260 EMIT(rot_two);
2261 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2262 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2263 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2264 } else {
Damien George495d7812014-04-08 17:51:47 +01002265 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002266 compile_node(comp, pns1->nodes[0]); // rhs
2267 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2268 }
Damien429d7192013-10-04 19:53:11 +01002269 }
2270 }
2271}
2272
Damien George6be0b0a2014-08-15 14:30:52 +01002273STATIC 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 +00002274 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002275 compile_node(comp, pns->nodes[0]);
2276 for (int i = 1; i < num_nodes; i += 1) {
2277 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002278 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002279 }
2280}
2281
Damien George969a6b32014-12-10 22:07:04 +00002282STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002283 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2284 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002285
Damien George6f355fd2014-04-10 14:11:31 +01002286 uint l_fail = comp_next_label(comp);
2287 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002288 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2289 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002290 EMIT_ARG(jump, l_end);
2291 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002292 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002293 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002294 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002295}
2296
Damien George969a6b32014-12-10 22:07:04 +00002297STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002298 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002299 //mp_parse_node_t pn_params = pns->nodes[0];
2300 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002301
Damien George36db6bc2014-05-07 17:24:22 +01002302 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002303 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002304 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 +01002305 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002306 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002307 }
2308
2309 // get the scope for this lambda
2310 scope_t *this_scope = (scope_t*)pns->nodes[2];
2311
2312 // make the lambda
2313 close_over_variables_etc(comp, this_scope, 0, 0);
2314}
2315
Damien George7711afb2015-02-28 15:10:18 +00002316STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002317 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002318 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002319 for (int i = 0; i < n; i += 1) {
2320 compile_node(comp, pns->nodes[i]);
2321 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002322 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002323 }
2324 }
Damien Georgeb9791222014-01-23 00:34:21 +00002325 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002326}
2327
Damien George7711afb2015-02-28 15:10:18 +00002328STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2329 compile_or_and_test(comp, pns, true);
2330}
2331
Damien George969a6b32014-12-10 22:07:04 +00002332STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002333 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002334}
2335
Damien George969a6b32014-12-10 22:07:04 +00002336STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002337 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002338 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002339}
2340
Damien George969a6b32014-12-10 22:07:04 +00002341STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002342 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002343 compile_node(comp, pns->nodes[0]);
2344 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002345 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002346 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002347 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002348 }
2349 for (int i = 1; i + 1 < num_nodes; i += 2) {
2350 compile_node(comp, pns->nodes[i + 1]);
2351 if (i + 2 < num_nodes) {
2352 EMIT(dup_top);
2353 EMIT(rot_three);
2354 }
Damien George7e5fb242014-02-01 22:18:47 +00002355 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002356 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002357 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002358 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2359 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2360 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2361 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2362 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2363 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002364 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002365 }
2366 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002367 } else {
2368 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002369 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2370 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002371 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002372 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002373 } else {
2374 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002375 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002376 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002377 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002378 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002379 }
Damien429d7192013-10-04 19:53:11 +01002380 }
Damien429d7192013-10-04 19:53:11 +01002381 }
2382 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002383 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002384 }
2385 }
2386 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002387 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002388 EMIT_ARG(jump, l_end);
2389 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002390 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002391 EMIT(rot_two);
2392 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002393 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002394 }
2395}
2396
Damien George969a6b32014-12-10 22:07:04 +00002397STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002398 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002399}
2400
Damien George969a6b32014-12-10 22:07:04 +00002401STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002402 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002403}
2404
Damien George969a6b32014-12-10 22:07:04 +00002405STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002406 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002407}
2408
Damien George969a6b32014-12-10 22:07:04 +00002409STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002410 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002411}
2412
Damien George969a6b32014-12-10 22:07:04 +00002413STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002414 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002415 compile_node(comp, pns->nodes[0]);
2416 for (int i = 1; i + 1 < num_nodes; i += 2) {
2417 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002418 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002419 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002420 } else {
Damien George0bb97132015-02-27 14:25:47 +00002421 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2422 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002423 }
2424 }
2425}
2426
Damien George969a6b32014-12-10 22:07:04 +00002427STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002428 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002429 compile_node(comp, pns->nodes[0]);
2430 for (int i = 1; i + 1 < num_nodes; i += 2) {
2431 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002432 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002433 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002434 } else {
Damien George0bb97132015-02-27 14:25:47 +00002435 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2436 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002437 }
2438 }
2439}
2440
Damien George969a6b32014-12-10 22:07:04 +00002441STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002442 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002443 compile_node(comp, pns->nodes[0]);
2444 for (int i = 1; i + 1 < num_nodes; i += 2) {
2445 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002446 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002447 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002448 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002449 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002450 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002451 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002452 } else {
Damien George0bb97132015-02-27 14:25:47 +00002453 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2454 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002455 }
2456 }
2457}
2458
Damien George969a6b32014-12-10 22:07:04 +00002459STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002460 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002461 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002462 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002463 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002464 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002465 } else {
Damien George0bb97132015-02-27 14:25:47 +00002466 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2467 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002468 }
2469}
2470
Damien George969a6b32014-12-10 22:07:04 +00002471STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002472 // this is to handle special super() call
2473 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2474
2475 compile_generic_all_nodes(comp, pns);
2476}
2477
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002478STATIC 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 +01002479 // function to call is on top of stack
2480
Damien George35e2a4e2014-02-05 00:51:47 +00002481#if !MICROPY_EMIT_CPYTHON
2482 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002483 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 +00002484 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002485 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002486 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002487 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002488 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002489 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002490 EMIT_ARG(call_function, 2, 0, 0);
2491 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002492 }
2493 }
Damien George01039b52014-12-21 17:44:27 +00002494 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002495 return;
2496 }
2497#endif
2498
Damien George2c0842b2014-04-27 16:46:51 +01002499 // get the list of arguments
2500 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002501 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002502
Damien George2c0842b2014-04-27 16:46:51 +01002503 // compile the arguments
2504 // Rather than calling compile_node on the list, we go through the list of args
2505 // explicitly here so that we can count the number of arguments and give sensible
2506 // error messages.
2507 int n_positional = n_positional_extra;
2508 uint n_keyword = 0;
2509 uint star_flags = 0;
2510 for (int i = 0; i < n_args; i++) {
2511 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2512 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2513 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2514 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2515 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2516 return;
2517 }
2518 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2519 compile_node(comp, pns_arg->nodes[0]);
2520 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2521 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
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_DOUBLE;
2526 compile_node(comp, pns_arg->nodes[0]);
2527 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2528 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2529 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2530 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2531 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2532 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2533 return;
2534 }
Damien George968bf342014-04-27 19:12:05 +01002535 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002536 compile_node(comp, pns2->nodes[0]);
2537 n_keyword += 1;
2538 } else {
2539 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2540 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2541 n_positional++;
2542 }
2543 } else {
2544 goto normal_argument;
2545 }
2546 } else {
2547 normal_argument:
2548 if (n_keyword > 0) {
2549 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2550 return;
2551 }
2552 compile_node(comp, args[i]);
2553 n_positional++;
2554 }
Damien429d7192013-10-04 19:53:11 +01002555 }
2556
Damien George2c0842b2014-04-27 16:46:51 +01002557 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002558 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002559 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002560 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002561 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002562 }
Damien429d7192013-10-04 19:53:11 +01002563}
2564
Damien George969a6b32014-12-10 22:07:04 +00002565STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002566 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002567 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002568 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 +01002569 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002570 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2571 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002572 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002573 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002574 i += 1;
2575 } else {
2576 compile_node(comp, pns->nodes[i]);
2577 }
Damien George35e2a4e2014-02-05 00:51:47 +00002578 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002579 }
2580}
2581
Damien George969a6b32014-12-10 22:07:04 +00002582STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002583 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002584 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002585}
2586
Damien George969a6b32014-12-10 22:07:04 +00002587STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002588 // a list of strings
Damien63321742013-12-10 17:41:49 +00002589
2590 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002591 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002592 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002593 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002594 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002595 int pn_kind;
2596 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2597 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2598 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2599 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2600 } else {
2601 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2602 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002603 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2604 pn_kind = MP_PARSE_NODE_STRING;
2605 } else {
2606 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2607 pn_kind = MP_PARSE_NODE_BYTES;
2608 }
Damien George40f3c022014-07-03 13:25:24 +01002609 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002610 }
Damien63321742013-12-10 17:41:49 +00002611 if (i == 0) {
2612 string_kind = pn_kind;
2613 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002614 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002615 return;
2616 }
Damien429d7192013-10-04 19:53:11 +01002617 }
Damien63321742013-12-10 17:41:49 +00002618
Damien George65ef6b72015-01-14 21:17:27 +00002619 // if we are not in the last pass, just load a dummy object
2620 if (comp->pass != MP_PASS_EMIT) {
2621 EMIT_ARG(load_const_obj, mp_const_none);
2622 return;
2623 }
2624
Damien63321742013-12-10 17:41:49 +00002625 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002626 vstr_t vstr;
2627 vstr_init_len(&vstr, n_bytes);
2628 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002629 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002630 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002631 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002632 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2633 memcpy(s_dest, s, s_len);
2634 s_dest += s_len;
2635 } else {
2636 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002637 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2638 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002639 }
Damien63321742013-12-10 17:41:49 +00002640 }
Damien George65ef6b72015-01-14 21:17:27 +00002641
2642 // load the object
Damien George05005f62015-01-21 22:48:37 +00002643 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 +01002644}
2645
2646// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002647STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002648 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2649 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2650 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002651
Damien George36db6bc2014-05-07 17:24:22 +01002652 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002653 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002654 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 +01002655 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002656 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002657 }
2658
2659 // get the scope for this comprehension
2660 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2661
2662 // compile the comprehension
2663 close_over_variables_etc(comp, this_scope, 0, 0);
2664
2665 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2666 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002667 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002668}
2669
Damien George969a6b32014-12-10 22:07:04 +00002670STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002671 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002672 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002673 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2674 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2675 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2676 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2677 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2678 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2679 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002680 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002681 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002682 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002683 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002684 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002685 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002686 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002687 // generator expression
2688 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2689 } else {
2690 // tuple with 2 items
2691 goto tuple_with_2_items;
2692 }
2693 } else {
2694 // tuple with 2 items
2695 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002696 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002697 }
2698 } else {
2699 // parenthesis around a single item, is just that item
2700 compile_node(comp, pns->nodes[0]);
2701 }
2702}
2703
Damien George969a6b32014-12-10 22:07:04 +00002704STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002705 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002706 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002707 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002708 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2709 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2710 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2711 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2712 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002713 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002714 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002715 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002716 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002717 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002718 // list of many items
2719 compile_node(comp, pns2->nodes[0]);
2720 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002721 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002722 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002723 // list comprehension
2724 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2725 } else {
2726 // list with 2 items
2727 goto list_with_2_items;
2728 }
2729 } else {
2730 // list with 2 items
2731 list_with_2_items:
2732 compile_node(comp, pns2->nodes[0]);
2733 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002734 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002735 }
2736 } else {
2737 // list with 1 item
2738 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002739 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002740 }
2741}
2742
Damien George969a6b32014-12-10 22:07:04 +00002743STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002744 mp_parse_node_t pn = pns->nodes[0];
2745 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002746 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002747 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002748 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2749 pns = (mp_parse_node_struct_t*)pn;
2750 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002751 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002752 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002753 compile_node(comp, pn);
2754 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002755 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2756 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2757 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2758 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002759 // dict/set with multiple elements
2760
2761 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002762 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002763 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002764
2765 // first element sets whether it's a dict or set
2766 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002767 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002768 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002769 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002770 compile_node(comp, pns->nodes[0]);
2771 EMIT(store_map);
2772 is_dict = true;
2773 } else {
2774 // a set
2775 compile_node(comp, pns->nodes[0]); // 1st value of set
2776 is_dict = false;
2777 }
2778
2779 // process rest of elements
2780 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002781 mp_parse_node_t pn_i = nodes[i];
2782 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2783 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002784 if (is_dict) {
2785 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002786 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002787 return;
2788 }
2789 EMIT(store_map);
2790 } else {
2791 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002792 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002793 return;
2794 }
2795 }
2796 }
2797
Damien Georgee37dcaa2014-12-27 17:07:16 +00002798 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002799 // if it's a set, build it
2800 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002801 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002802 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002803 #endif
Damien George0bb97132015-02-27 14:25:47 +00002804 } else {
2805 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002806 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002807 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002808 // a dictionary comprehension
2809 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2810 } else {
2811 // a set comprehension
2812 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2813 }
Damien429d7192013-10-04 19:53:11 +01002814 }
2815 } else {
2816 // set with one element
2817 goto set_with_one_element;
2818 }
2819 } else {
2820 // set with one element
2821 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002822 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002823 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002824 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002825 #else
2826 assert(0);
2827 #endif
Damien429d7192013-10-04 19:53:11 +01002828 }
2829}
2830
Damien George969a6b32014-12-10 22:07:04 +00002831STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002832 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002833}
2834
Damien George969a6b32014-12-10 22:07:04 +00002835STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002836 // object who's index we want is on top of stack
2837 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002838 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002839}
2840
Damien George969a6b32014-12-10 22:07:04 +00002841STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002842 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002843 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002844}
2845
Damien George83204f32014-12-27 17:20:41 +00002846#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002847STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002848 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2849 mp_parse_node_t pn = pns->nodes[0];
2850 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002851 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002852 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2853 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002854 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2855 pns = (mp_parse_node_struct_t*)pn;
2856 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002857 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002858 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002859 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002860 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002861 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002862 } else {
2863 // [?::x]
2864 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002865 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002866 }
Damiend99b0522013-12-21 18:17:45 +00002867 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002868 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002869 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2870 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2871 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2872 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002873 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002874 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002875 } else {
2876 // [?:x:x]
2877 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002878 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002879 }
2880 } else {
2881 // [?:x]
2882 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002883 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002884 }
2885 } else {
2886 // [?:x]
2887 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002888 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002889 }
2890}
2891
Damien George969a6b32014-12-10 22:07:04 +00002892STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002893 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002894 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2895 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002896}
2897
Damien George969a6b32014-12-10 22:07:04 +00002898STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002899 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002900 compile_subscript_3_helper(comp, pns);
2901}
Damien George83204f32014-12-27 17:20:41 +00002902#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002903
Damien George969a6b32014-12-10 22:07:04 +00002904STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002905 // if this is called then we are compiling a dict key:value pair
2906 compile_node(comp, pns->nodes[1]); // value
2907 compile_node(comp, pns->nodes[0]); // key
2908}
2909
Damien George969a6b32014-12-10 22:07:04 +00002910STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002911 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002912 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002913 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002914}
2915
Damien George969a6b32014-12-10 22:07:04 +00002916STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002917 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002918 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002919 return;
2920 }
Damiend99b0522013-12-21 18:17:45 +00002921 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002922 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002923 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002924 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2925 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002926 compile_node(comp, pns->nodes[0]);
2927 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002928 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002929 EMIT(yield_from);
2930 } else {
2931 compile_node(comp, pns->nodes[0]);
2932 EMIT(yield_value);
2933 }
2934}
2935
Damien George65ef6b72015-01-14 21:17:27 +00002936STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2937 // only create and load the actual str object on the last pass
2938 if (comp->pass != MP_PASS_EMIT) {
2939 EMIT_ARG(load_const_obj, mp_const_none);
2940 } else {
2941 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2942 }
2943}
2944
2945STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2946 // only create and load the actual bytes object on the last pass
2947 if (comp->pass != MP_PASS_EMIT) {
2948 EMIT_ARG(load_const_obj, mp_const_none);
2949 } else {
2950 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2951 }
2952}
2953
Damien George7d414a12015-02-08 01:57:40 +00002954STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2955 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2956}
2957
Damiend99b0522013-12-21 18:17:45 +00002958typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002959STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002960#define nc NULL
2961#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002962#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002963#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002964#undef nc
2965#undef c
2966#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002967 NULL,
2968 compile_string,
2969 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002970 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002971};
2972
Damien George6be0b0a2014-08-15 14:30:52 +01002973STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002974 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002975 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002976 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002977 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002978 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002979 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002980 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002981 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002982 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002983 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2984 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damien Georged2d64f02015-01-14 21:32:42 +00002985 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002986 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002987 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002988 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002989 // do nothing
2990 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002991 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002992 }
2993 break;
Damien429d7192013-10-04 19:53:11 +01002994 }
2995 } else {
Damiend99b0522013-12-21 18:17:45 +00002996 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002997 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002998 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2999 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01003000#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00003001 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
3002 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01003003#endif
Damien George65ef6b72015-01-14 21:17:27 +00003004 compile_syntax_error(comp, pn, "internal compiler error");
3005 } else {
3006 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01003007 }
3008 }
3009}
3010
Damien George2ac4af62014-08-15 16:45:41 +01003011STATIC 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 +01003012 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01003013 qstr param_name = MP_QSTR_NULL;
3014 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003015 if (MP_PARSE_NODE_IS_ID(pn)) {
3016 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01003017 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003018 // comes after a star, so counts as a keyword-only parameter
3019 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01003020 } else {
Damien George2827d622014-04-27 15:50:52 +01003021 // comes before a star, so counts as a positional parameter
3022 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003023 }
Damienb14de212013-10-06 00:28:28 +01003024 } else {
Damiend99b0522013-12-21 18:17:45 +00003025 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3026 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3027 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3028 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003029 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003030 // comes after a star, so counts as a keyword-only parameter
3031 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003032 } else {
Damien George2827d622014-04-27 15:50:52 +01003033 // comes before a star, so counts as a positional parameter
3034 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003035 }
Damiend99b0522013-12-21 18:17:45 +00003036 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003037 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003038 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003039 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003040 // bare star
3041 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003042 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003043 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003044 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003045 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003046 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00003047 } else {
3048 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01003049 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003050 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003051 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3052 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003053 }
Damien George0bb97132015-02-27 14:25:47 +00003054 } else {
3055 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00003056 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003057 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003058 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003059 }
Damien429d7192013-10-04 19:53:11 +01003060 }
3061
Damien George2827d622014-04-27 15:50:52 +01003062 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003063 bool added;
3064 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3065 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003066 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003067 return;
3068 }
Damien429d7192013-10-04 19:53:11 +01003069 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003070 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003071 }
3072}
3073
Damien George2827d622014-04-27 15:50:52 +01003074STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003075 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003076}
3077
Damien George2827d622014-04-27 15:50:52 +01003078STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003079 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3080}
3081
3082STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3083 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3084 // no annotation
3085 return;
3086 }
3087
3088 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3089 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3090 // named parameter with possible annotation
3091 // fallthrough
3092 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3093 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3094 // named star with possible annotation
3095 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3096 // fallthrough
3097 } else {
3098 // no annotation
3099 return;
3100 }
3101 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3102 // double star with possible annotation
3103 // fallthrough
3104 } else {
3105 // no annotation
3106 return;
3107 }
3108
3109 mp_parse_node_t pn_annotation = pns->nodes[1];
3110
3111 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3112 #if MICROPY_EMIT_NATIVE
3113 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3114 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3115 assert(id_info != NULL);
3116
3117 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3118 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3119 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3120 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3121 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003122 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003123 }
3124 }
3125 #endif // MICROPY_EMIT_NATIVE
3126 }
Damien429d7192013-10-04 19:53:11 +01003127}
3128
Damien George6be0b0a2014-08-15 14:30:52 +01003129STATIC 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 +01003130 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003131 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003132 // no more nested if/for; compile inner expression
3133 compile_node(comp, pn_inner_expr);
3134 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003135 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003136 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003137 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003138 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003139 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003140 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003141 #endif
Damien429d7192013-10-04 19:53:11 +01003142 } else {
3143 EMIT(yield_value);
3144 EMIT(pop_top);
3145 }
Damiend99b0522013-12-21 18:17:45 +00003146 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003147 // if condition
Damiend99b0522013-12-21 18:17:45 +00003148 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003149 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3150 pn_iter = pns_comp_if->nodes[1];
3151 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00003152 } else {
3153 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01003154 // for loop
Damiend99b0522013-12-21 18:17:45 +00003155 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003156 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003157 uint l_end2 = comp_next_label(comp);
3158 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003159 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003160 EMIT_ARG(label_assign, l_top2);
3161 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003162 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3163 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 +00003164 EMIT_ARG(jump, l_top2);
3165 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003166 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01003167 }
3168}
3169
Damien George1463c1f2014-04-25 23:52:57 +01003170STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3171#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003172 // see http://www.python.org/dev/peps/pep-0257/
3173
3174 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003175 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003176 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003177 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003178 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003179 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3180 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003181 for (int i = 0; i < num_nodes; i++) {
3182 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003183 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 +00003184 // not a newline, so this is the first statement; finish search
3185 break;
3186 }
3187 }
3188 // 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 +00003189 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003190 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003191 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003192 } else {
3193 return;
3194 }
3195
3196 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003197 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00003198 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003199 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3200 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3201 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3202 // compile the doc string
3203 compile_node(comp, pns->nodes[0]);
3204 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00003205 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003206 }
3207 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003208#else
3209 (void)comp;
3210 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003211#endif
Damien429d7192013-10-04 19:53:11 +01003212}
3213
Damien George1463c1f2014-04-25 23:52:57 +01003214STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003215 comp->pass = pass;
3216 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003217 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003218 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003219
Damien George36db6bc2014-05-07 17:24:22 +01003220 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003221 // reset maximum stack sizes in scope
3222 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003223 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003224 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003225 }
3226
Damien5ac1b2e2013-10-18 19:58:12 +01003227#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003228 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003229 scope_print_info(scope);
3230 }
Damien5ac1b2e2013-10-18 19:58:12 +01003231#endif
Damien429d7192013-10-04 19:53:11 +01003232
3233 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003234 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3235 assert(scope->kind == SCOPE_MODULE);
3236 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3237 compile_node(comp, pns->nodes[0]); // compile the expression
3238 EMIT(return_value);
3239 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003240 if (!comp->is_repl) {
3241 check_for_doc_string(comp, scope->pn);
3242 }
Damien429d7192013-10-04 19:53:11 +01003243 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003244 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003245 EMIT(return_value);
3246 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003247 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3248 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3249 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003250
3251 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003252 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003253 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003254 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003255 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003256 } else {
3257 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003258
Damien George2ac4af62014-08-15 16:45:41 +01003259 // argument annotations
3260 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3261
3262 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003263 mp_parse_node_t pn_annotation = pns->nodes[2];
3264 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3265 #if MICROPY_EMIT_NATIVE
3266 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3267 // nodes[2] can be null or a test-expr
3268 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3269 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3270 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3271 } else {
3272 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3273 }
Damien George2ac4af62014-08-15 16:45:41 +01003274 }
Damien Georgea5190a72014-08-15 22:39:08 +01003275 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003276 }
Damien George2ac4af62014-08-15 16:45:41 +01003277 }
Damien429d7192013-10-04 19:53:11 +01003278
3279 compile_node(comp, pns->nodes[3]); // 3 is function body
3280 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003281 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003282 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003283 EMIT(return_value);
3284 }
3285 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003286 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3287 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3288 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003289
3290 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003291 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003292 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003293 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003294 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3295 }
3296
3297 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003298
3299 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3300 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3301 EMIT(pop_top);
3302 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3303 }
Damien429d7192013-10-04 19:53:11 +01003304 EMIT(return_value);
3305 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3306 // a bit of a hack at the moment
3307
Damiend99b0522013-12-21 18:17:45 +00003308 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3309 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3310 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3311 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3312 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003313
Damien George708c0732014-04-27 19:23:46 +01003314 // We need a unique name for the comprehension argument (the iterator).
3315 // CPython uses .0, but we should be able to use anything that won't
3316 // clash with a user defined variable. Best to use an existing qstr,
3317 // so we use the blank qstr.
3318#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003319 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003320#else
3321 qstr qstr_arg = MP_QSTR_;
3322#endif
Damien George36db6bc2014-05-07 17:24:22 +01003323 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003324 bool added;
3325 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3326 assert(added);
3327 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003328 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003329 }
3330
3331 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003332 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003333 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003334 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003335 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003336 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003337 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003338 #endif
Damien429d7192013-10-04 19:53:11 +01003339 }
3340
Damien George6f355fd2014-04-10 14:11:31 +01003341 uint l_end = comp_next_label(comp);
3342 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00003343 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00003344 EMIT_ARG(label_assign, l_top);
3345 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003346 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3347 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003348 EMIT_ARG(jump, l_top);
3349 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003350 EMIT(for_iter_end);
3351
3352 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003353 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003354 }
3355 EMIT(return_value);
3356 } else {
3357 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003358 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3359 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3360 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003361
Damien George36db6bc2014-05-07 17:24:22 +01003362 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003363 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003364 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003365 assert(added);
3366 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003367 }
3368
Damien George542bd6b2015-03-26 14:42:40 +00003369 compile_load_id(comp, MP_QSTR___name__);
3370 compile_store_id(comp, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003371 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), false); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003372 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003373
3374 check_for_doc_string(comp, pns->nodes[2]);
3375 compile_node(comp, pns->nodes[2]); // 2 is class body
3376
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003377 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003378 assert(id != NULL);
3379 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003380 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003381 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003382#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003383 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003384#else
Damien George542bd6b2015-03-26 14:42:40 +00003385 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003386#endif
Damien429d7192013-10-04 19:53:11 +01003387 }
3388 EMIT(return_value);
3389 }
3390
Damien415eb6f2013-10-05 12:19:06 +01003391 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003392
3393 // make sure we match all the exception levels
3394 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003395}
3396
Damien George094d4502014-04-02 17:31:27 +01003397#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003398// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003399STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003400 comp->pass = pass;
3401 comp->scope_cur = scope;
3402 comp->next_label = 1;
3403
3404 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003405 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003406 return;
3407 }
3408
Damien George36db6bc2014-05-07 17:24:22 +01003409 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003410 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003411 }
3412
Damien826005c2013-10-05 23:17:28 +01003413 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003414 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3415 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3416 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003417
Damiend99b0522013-12-21 18:17:45 +00003418 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003419
Damiena2f2f7d2013-10-06 00:14:13 +01003420 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003421 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003422 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003423 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003424 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003425 if (comp->compile_error != MP_OBJ_NULL) {
3426 goto inline_asm_error;
3427 }
Damiena2f2f7d2013-10-06 00:14:13 +01003428 }
3429
Damiend99b0522013-12-21 18:17:45 +00003430 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003431
Damiend99b0522013-12-21 18:17:45 +00003432 mp_parse_node_t pn_body = pns->nodes[3]; // body
3433 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003434 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003435
Damien Georgecbd2f742014-01-19 11:48:48 +00003436 /*
Damien George36db6bc2014-05-07 17:24:22 +01003437 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003438 //printf("----\n");
3439 scope_print_info(scope);
3440 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003441 */
Damien826005c2013-10-05 23:17:28 +01003442
3443 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003444 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3445 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003446 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3447 // no instructions
3448 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003449 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003450 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003451 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003452 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003453 return;
3454 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003455
3456 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003457 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003458 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3459 goto not_an_instruction;
3460 }
Damiend99b0522013-12-21 18:17:45 +00003461 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003462 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3463 goto not_an_instruction;
3464 }
3465 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3466 goto not_an_instruction;
3467 }
3468 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3469 goto not_an_instruction;
3470 }
Damiend99b0522013-12-21 18:17:45 +00003471 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003472
3473 // parse node looks like an instruction
3474 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003475 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3476 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3477 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003478 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003479
3480 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003481 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003482 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003483 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003484 return;
3485 }
Damien George6f355fd2014-04-10 14:11:31 +01003486 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003487 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003488 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3489 compile_syntax_error(comp, nodes[i], "label redefined");
3490 return;
3491 }
Damien826005c2013-10-05 23:17:28 +01003492 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003493 } else if (op == MP_QSTR_align) {
3494 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003495 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003496 return;
3497 }
Damien George36db6bc2014-05-07 17:24:22 +01003498 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003499 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3500 }
3501 } else if (op == MP_QSTR_data) {
3502 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003503 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003504 return;
3505 }
Damien George36db6bc2014-05-07 17:24:22 +01003506 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003507 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003508 for (uint j = 1; j < n_args; j++) {
3509 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003510 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003511 return;
3512 }
Damien George50912e72015-01-20 11:55:10 +00003513 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003514 }
3515 }
Damien826005c2013-10-05 23:17:28 +01003516 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003517 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003518 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003519 }
3520 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003521
3522 if (comp->compile_error != MP_OBJ_NULL) {
3523 pns = pns2; // this is the parse node that had the error
3524 goto inline_asm_error;
3525 }
Damien826005c2013-10-05 23:17:28 +01003526 }
3527
Damien George36db6bc2014-05-07 17:24:22 +01003528 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003529 EMIT_INLINE_ASM(end_pass);
3530 }
3531
3532 if (comp->compile_error != MP_OBJ_NULL) {
3533 // inline assembler had an error; add traceback to its exception
3534 inline_asm_error:
3535 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, (mp_uint_t)pns->source_line, comp->scope_cur->simple_name);
Damienb05d7072013-10-05 13:37:10 +01003536 }
Damien429d7192013-10-04 19:53:11 +01003537}
Damien George094d4502014-04-02 17:31:27 +01003538#endif
Damien429d7192013-10-04 19:53:11 +01003539
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003540STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003541#if !MICROPY_EMIT_CPYTHON
3542 // in Micro Python we put the *x parameter after all other parameters (except **y)
3543 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3544 id_info_t *id_param = NULL;
3545 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3546 id_info_t *id = &scope->id_info[i];
3547 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3548 if (id_param != NULL) {
3549 // swap star param with last param
3550 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3551 }
3552 break;
3553 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3554 id_param = id;
3555 }
3556 }
3557 }
3558#endif
3559
Damien429d7192013-10-04 19:53:11 +01003560 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003561 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003562 scope->num_locals = 0;
3563 for (int i = 0; i < scope->id_info_len; i++) {
3564 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003565 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003566 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3567 continue;
3568 }
3569 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3570 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3571 }
Damien George2827d622014-04-27 15:50:52 +01003572 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003573 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003574 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003575 }
3576 }
3577
3578 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003579#if MICROPY_EMIT_CPYTHON
3580 int num_cell = 0;
3581#endif
Damien9ecbcff2013-12-11 00:41:43 +00003582 for (int i = 0; i < scope->id_info_len; i++) {
3583 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003584#if MICROPY_EMIT_CPYTHON
3585 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003586 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003587 id->local_num = num_cell;
3588 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003589 }
Damien George6baf76e2013-12-30 22:32:17 +00003590#else
3591 // in Micro Python the cells come right after the fast locals
3592 // parameters are not counted here, since they remain at the start
3593 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003594 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003595 id->local_num = scope->num_locals;
3596 scope->num_locals += 1;
3597 }
3598#endif
Damien9ecbcff2013-12-11 00:41:43 +00003599 }
Damien9ecbcff2013-12-11 00:41:43 +00003600
3601 // compute the index of free vars (freevars[idx] in CPython)
3602 // make sure they are in the order of the parent scope
3603 if (scope->parent != NULL) {
3604 int num_free = 0;
3605 for (int i = 0; i < scope->parent->id_info_len; i++) {
3606 id_info_t *id = &scope->parent->id_info[i];
3607 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3608 for (int j = 0; j < scope->id_info_len; j++) {
3609 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003610 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003611 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003612#if MICROPY_EMIT_CPYTHON
3613 // in CPython the frees are numbered after the cells
3614 id2->local_num = num_cell + num_free;
3615#else
3616 // in Micro Python the frees come first, before the params
3617 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003618#endif
3619 num_free += 1;
3620 }
3621 }
3622 }
Damien429d7192013-10-04 19:53:11 +01003623 }
Damien George6baf76e2013-12-30 22:32:17 +00003624#if !MICROPY_EMIT_CPYTHON
3625 // in Micro Python shift all other locals after the free locals
3626 if (num_free > 0) {
3627 for (int i = 0; i < scope->id_info_len; i++) {
3628 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003629 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003630 id->local_num += num_free;
3631 }
3632 }
Damien George2827d622014-04-27 15:50:52 +01003633 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 +00003634 scope->num_locals += num_free;
3635 }
3636#endif
Damien429d7192013-10-04 19:53:11 +01003637 }
3638
Damien George8725f8f2014-02-15 19:33:11 +00003639 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003640
3641#if MICROPY_EMIT_CPYTHON
3642 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003643 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) {
3644 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003645 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003646 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003647 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 +00003648 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003649 }
3650 }
Damien George882b3632014-04-02 15:56:31 +01003651#endif
3652
Damien429d7192013-10-04 19:53:11 +01003653 int num_free = 0;
3654 for (int i = 0; i < scope->id_info_len; i++) {
3655 id_info_t *id = &scope->id_info[i];
3656 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3657 num_free += 1;
3658 }
3659 }
3660 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003661 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003662 }
3663}
3664
Damien George65cad122014-04-06 11:48:15 +01003665mp_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 +01003666 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003667 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003668 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003669 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003670
Damien George62a3a282015-03-01 12:04:05 +00003671 // create the module scope
3672 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3673
3674 // optimise constants (scope must be set for error messages to work)
3675 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003676 mp_map_t consts;
3677 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003678 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003679 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003680
Damien Georgea210c772015-03-26 15:49:53 +00003681 // create standard emitter; it's used at least for MP_PASS_SCOPE
3682 #if MICROPY_EMIT_CPYTHON
3683 emit_t *emit_cpython = emit_cpython_new();
3684 #else
3685 emit_t *emit_bc = emit_bc_new();
3686 #endif
3687
Damien826005c2013-10-05 23:17:28 +01003688 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003689 #if MICROPY_EMIT_CPYTHON
3690 comp->emit = emit_cpython;
3691 comp->emit_method_table = &emit_cpython_method_table;
3692 #else
3693 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003694 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003695 comp->emit_method_table = &emit_bc_method_table;
3696 #endif
Damien George41125902015-03-26 16:44:14 +00003697 #endif
Damien Georgea77ffe62015-03-14 12:59:31 +00003698 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003699 comp->emit_inline_asm = NULL;
3700 comp->emit_inline_asm_method_table = NULL;
Damien Georgea77ffe62015-03-14 12:59:31 +00003701 #endif
Damien826005c2013-10-05 23:17:28 +01003702 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003703 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003704 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003705#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003706 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003707 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003708#endif
Damien826005c2013-10-05 23:17:28 +01003709 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003710 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003711 }
3712
3713 // update maximim number of labels needed
3714 if (comp->next_label > max_num_labels) {
3715 max_num_labels = comp->next_label;
3716 }
Damien429d7192013-10-04 19:53:11 +01003717 }
3718
Damien826005c2013-10-05 23:17:28 +01003719 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003720 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 +00003721 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003722 }
3723
Damien Georgea210c772015-03-26 15:49:53 +00003724 // set max number of labels now that it's calculated
3725 #if MICROPY_EMIT_CPYTHON
3726 emit_cpython_set_max_num_labels(emit_cpython, max_num_labels);
3727 #else
3728 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
3729 #endif
3730
Damien826005c2013-10-05 23:17:28 +01003731 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003732#if !MICROPY_EMIT_CPYTHON
Damien Georgee67ed5d2014-01-04 13:55:24 +00003733#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003734 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003735#endif
Damien3ef4abb2013-10-12 16:53:13 +01003736#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003737 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003738#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003739#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003740 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003741 if (false) {
3742 // dummy
3743
Damien3ef4abb2013-10-12 16:53:13 +01003744#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003745 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003746 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003747 if (emit_inline_thumb == NULL) {
3748 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3749 }
3750 comp->emit = NULL;
3751 comp->emit_method_table = NULL;
3752 comp->emit_inline_asm = emit_inline_thumb;
3753 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003754 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003755 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003756 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003757 }
Damienc025ebb2013-10-12 14:30:21 +01003758#endif
3759
Damien826005c2013-10-05 23:17:28 +01003760 } else {
Damienc025ebb2013-10-12 14:30:21 +01003761
3762 // choose the emit type
3763
Damien3ef4abb2013-10-12 16:53:13 +01003764#if MICROPY_EMIT_CPYTHON
Damien Georgea210c772015-03-26 15:49:53 +00003765 comp->emit = emit_cpython;
Damienc025ebb2013-10-12 14:30:21 +01003766 comp->emit_method_table = &emit_cpython_method_table;
3767#else
Damien826005c2013-10-05 23:17:28 +01003768 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003769
3770#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003771 case MP_EMIT_OPT_NATIVE_PYTHON:
3772 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003773#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003774 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003775 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003776 }
Damien13ed3a62013-10-08 09:05:10 +01003777 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003778#elif MICROPY_EMIT_X86
3779 if (emit_native == NULL) {
3780 emit_native = emit_native_x86_new(max_num_labels);
3781 }
3782 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003783#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003784 if (emit_native == NULL) {
3785 emit_native = emit_native_thumb_new(max_num_labels);
3786 }
3787 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003788#elif MICROPY_EMIT_ARM
3789 if (emit_native == NULL) {
3790 emit_native = emit_native_arm_new(max_num_labels);
3791 }
3792 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003793#endif
3794 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003795 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003796 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003797#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003798
Damien826005c2013-10-05 23:17:28 +01003799 default:
Damien826005c2013-10-05 23:17:28 +01003800 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003801 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003802 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003803 #endif
Damien826005c2013-10-05 23:17:28 +01003804 break;
3805 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003806#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003807
Damien George1e1779e2015-01-14 00:20:28 +00003808 // need a pass to compute stack size
3809 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3810
Damien George36db6bc2014-05-07 17:24:22 +01003811 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003812 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003813 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3814 }
3815
3816 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003817 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003818 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003819 }
Damien6cdd3af2013-10-05 18:08:26 +01003820 }
Damien429d7192013-10-04 19:53:11 +01003821 }
3822
Damien George41d02b62014-01-24 22:42:28 +00003823 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003824
3825#if MICROPY_EMIT_CPYTHON
3826 emit_cpython_free(emit_cpython);
3827#else
3828 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003829#if MICROPY_EMIT_NATIVE
3830 if (emit_native != NULL) {
3831#if MICROPY_EMIT_X64
3832 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003833#elif MICROPY_EMIT_X86
3834 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003835#elif MICROPY_EMIT_THUMB
3836 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003837#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003838 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003839#endif
3840 }
3841#endif
3842#if MICROPY_EMIT_INLINE_THUMB
3843 if (emit_inline_thumb != NULL) {
3844 emit_inline_thumb_free(emit_inline_thumb);
3845 }
3846#endif
Damien Georgea210c772015-03-26 15:49:53 +00003847#endif // MICROPY_EMIT_CPYTHON
Damien George41d02b62014-01-24 22:42:28 +00003848
Damien George52b5d762014-09-23 15:31:56 +00003849 // free the parse tree
Damien George62a3a282015-03-01 12:04:05 +00003850 mp_parse_node_free(module_scope->pn);
Damien George52b5d762014-09-23 15:31:56 +00003851
Damien George41d02b62014-01-24 22:42:28 +00003852 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003853 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003854 for (scope_t *s = module_scope; s;) {
3855 scope_t *next = s->next;
3856 scope_free(s);
3857 s = next;
3858 }
Damien5ac1b2e2013-10-18 19:58:12 +01003859
Damien George41d02b62014-01-24 22:42:28 +00003860 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003861 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003862 m_del_obj(compiler_t, comp);
3863
Damien Georgea91ac202014-10-05 19:01:34 +01003864 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003865 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003866 } else {
3867#if MICROPY_EMIT_CPYTHON
3868 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003869 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003870 return mp_const_true;
3871#else
3872 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003873 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003874#endif
3875 }
Damien429d7192013-10-04 19:53:11 +01003876}