blob: a3e8c49d1edd0707ff251378226395d5e1606c44 [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 *
Damien George9d5e5c02015-09-24 13:15:57 +01006 * Copyright (c) 2013-2015 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
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 George65dc9602015-08-14 12:24:11 +010052#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
Damien George41125902015-03-26 16:44:14 +000053
54#if NEED_METHOD_TABLE
55
56// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000057#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
58#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000059#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
60#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien Georgeb9791222014-01-23 00:34:21 +000061#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
62#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010063
Damien George41125902015-03-26 16:44:14 +000064#else
65
66// if we only have the bytecode emitter enabled then we can do a direct call to the functions
67#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
68#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
69#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
70#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
71
72#endif
73
Damien Georgea91ac202014-10-05 19:01:34 +010074// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010075typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000076 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010077
Damien George78035b92014-04-09 12:27:39 +010078 uint8_t is_repl;
79 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010080 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010081 uint8_t have_star;
82
83 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +000084 mp_obj_t compile_error; // set to an exception object if there's an error
85 mp_uint_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +010086
Damien George6f355fd2014-04-10 14:11:31 +010087 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010088
Damien George69b89d22014-04-11 13:38:30 +000089 uint16_t num_dict_params;
90 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010091
Damien Georgea91ac202014-10-05 19:01:34 +010092 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
93 uint16_t continue_label;
94 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000095 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010096
Damien429d7192013-10-04 19:53:11 +010097 scope_t *scope_head;
98 scope_t *scope_cur;
99
Damien6cdd3af2013-10-05 18:08:26 +0100100 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000101 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100102 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000103 #endif
Damien826005c2013-10-05 23:17:28 +0100104
Damien Georgea77ffe62015-03-14 12:59:31 +0000105 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +0100106 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
107 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien Georgea77ffe62015-03-14 12:59:31 +0000108 #endif
Damien429d7192013-10-04 19:53:11 +0100109} compiler_t;
110
Damien Georgecfc4c332015-07-29 22:16:01 +0000111STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
112 // if the line of the error is unknown then try to update it from the pn
113 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
114 comp->compile_error_line = (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100115 }
Damien George84d59c22015-07-27 22:20:00 +0100116}
117
118STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000119 // only register the error if there has been no other error
120 if (comp->compile_error == MP_OBJ_NULL) {
121 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
122 compile_error_set_line(comp, pn);
123 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000124}
125
Damien Georgeddd1e182015-01-10 14:07:24 +0000126#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100127STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300128 #if MICROPY_PY_UCTYPES
129 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
130 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100131 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100132 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100133};
Damien Georgeddd1e182015-01-10 14:07:24 +0000134STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
135#endif
Damien George57e99eb2014-04-10 22:42:11 +0100136
Damien Georgeffae48d2014-05-08 15:58:39 +0000137// this function is essentially a simple preprocessor
138STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
139 if (0) {
140 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100141#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000142 } else if (MP_PARSE_NODE_IS_ID(pn)) {
143 // lookup identifier in table of dynamic constants
144 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
145 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
146 if (elem != NULL) {
147 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
148 }
149#endif
150 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000151 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100152
Damien Georgeffae48d2014-05-08 15:58:39 +0000153 // fold some parse nodes before folding their arguments
154 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100155#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000156 case PN_expr_stmt:
157 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
158 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
159 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
160 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
161 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
162 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
163 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
164 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
165 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
166 ) {
167 // code to assign dynamic constants: id = const(value)
168
169 // get the id
170 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
171
172 // get the value
173 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
174 pn_value = fold_constants(comp, pn_value, consts);
175 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
176 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
177 break;
178 }
Damien George40f3c022014-07-03 13:25:24 +0100179 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000180
181 // store the value in the table of dynamic constants
182 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
183 if (elem->value != MP_OBJ_NULL) {
184 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
185 break;
186 }
187 elem->value = MP_OBJ_NEW_SMALL_INT(value);
188
189 // replace const(value) with value
190 pns1->nodes[0] = pn_value;
191
192 // finished folding this assignment
193 return pn;
194 }
195 }
196 }
197 break;
198#endif
Damien George5042bce2014-05-25 22:06:06 +0100199 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000200 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000201 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100202 return pn;
Damien429d7192013-10-04 19:53:11 +0100203 }
204
Damien Georgeffae48d2014-05-08 15:58:39 +0000205 // fold arguments
206 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
207 for (int i = 0; i < n; i++) {
208 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
209 }
210
211 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000212 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100213 case PN_atom_paren:
214 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
215 // (int)
216 pn = pns->nodes[0];
217 }
218 break;
219
220 case PN_expr:
221 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
222 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100223 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
224 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100225 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
226 }
227 break;
228
229 case PN_and_expr:
230 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
231 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100232 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
233 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100234 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
235 }
236 break;
237
Damien429d7192013-10-04 19:53:11 +0100238 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000239 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100240 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
241 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000242 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100243 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000244 if (!(arg1 >= (mp_int_t)BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
Damien Georgea26dc502014-04-12 17:54:52 +0100245 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
246 }
Damien George0bb97132015-02-27 14:25:47 +0000247 } else {
248 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100249 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000250 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200251 // Shifting to big amounts is underfined behavior
252 // in C and is CPU-dependent; propagate sign bit.
253 arg1 = BITS_PER_WORD - 1;
254 }
Damiend99b0522013-12-21 18:17:45 +0000255 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100256 }
257 }
258 break;
259
260 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100261 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000262 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100263 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
264 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000265 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100266 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000267 arg0 += arg1;
Damien George0bb97132015-02-27 14:25:47 +0000268 } else {
269 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100270 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000271 arg0 -= arg1;
Damien0efb3a12013-10-12 16:16:56 +0100272 }
Damien Georged1e355e2014-05-28 14:51:12 +0100273 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000274 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100275 }
276 }
277 break;
278
279 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000280 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100281 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
282 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000283 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100284 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000285 if (!mp_small_int_mul_overflow(arg0, arg1)) {
286 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100287 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000288 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
289 }
290 }
Damiend99b0522013-12-21 18:17:45 +0000291 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100292 // int / int
293 // pass
Damiend99b0522013-12-21 18:17:45 +0000294 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100295 // int%int
Damien Georgee5635f42015-10-01 22:48:48 +0100296 if (arg1 != 0) {
297 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
298 }
Damien George0bb97132015-02-27 14:25:47 +0000299 } else {
300 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300301 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100302 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000303 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 +0300304 }
Damien429d7192013-10-04 19:53:11 +0100305 }
306 }
307 break;
308
309 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000310 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100311 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000312 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100313 // +int
Damiend99b0522013-12-21 18:17:45 +0000314 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
315 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
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);
Damien George0bb97132015-02-27 14:25:47 +0000318 } else {
319 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100320 // ~int
Damiend99b0522013-12-21 18:17:45 +0000321 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100322 }
323 }
324 break;
325
326 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100327 if (0) {
Damien Georgeddd1e182015-01-10 14:07:24 +0000328#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100329 } 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])) {
330 // id.id
331 // look it up in constant table, see if it can be replaced with an integer
Damien George4dea9222015-04-09 15:29:54 +0000332 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George57e99eb2014-04-10 22:42:11 +0100333 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
334 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
335 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
336 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
337 if (elem != NULL) {
338 mp_obj_t dest[2];
339 mp_load_method_maybe(elem->value, q_attr, dest);
340 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100341 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000342 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100343 }
344 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000345#endif
Damien429d7192013-10-04 19:53:11 +0100346 }
347 break;
348 }
349 }
350
351 return pn;
352}
353
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200354STATIC 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 +0100355STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000356STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100357
Damien George6f355fd2014-04-10 14:11:31 +0100358STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100359 return comp->next_label++;
360}
361
Damien George8dcc0c72014-03-27 10:55:21 +0000362STATIC void compile_increase_except_level(compiler_t *comp) {
363 comp->cur_except_level += 1;
364 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
365 comp->scope_cur->exc_stack_size = comp->cur_except_level;
366 }
367}
368
369STATIC void compile_decrease_except_level(compiler_t *comp) {
370 assert(comp->cur_except_level > 0);
371 comp->cur_except_level -= 1;
372}
373
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200374STATIC 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 +0100375 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100376 scope->parent = comp->scope_cur;
377 scope->next = NULL;
378 if (comp->scope_head == NULL) {
379 comp->scope_head = scope;
380 } else {
381 scope_t *s = comp->scope_head;
382 while (s->next != NULL) {
383 s = s->next;
384 }
385 s->next = scope;
386 }
387 return scope;
388}
389
Damien George91bc32d2015-04-09 15:31:53 +0000390typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
391
392STATIC 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 +0000393 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000394 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
395 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100396 for (int i = 0; i < num_nodes; i++) {
397 f(comp, pns->nodes[i]);
398 }
Damiend99b0522013-12-21 18:17:45 +0000399 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100400 f(comp, pn);
401 }
402}
403
Damien George969a6b32014-12-10 22:07:04 +0000404STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000405 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100406 for (int i = 0; i < num_nodes; i++) {
407 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000408 if (comp->compile_error != MP_OBJ_NULL) {
409 // add line info for the error in case it didn't have a line number
410 compile_error_set_line(comp, pns->nodes[i]);
411 return;
412 }
Damien429d7192013-10-04 19:53:11 +0100413 }
414}
415
Damien George542bd6b2015-03-26 14:42:40 +0000416STATIC void compile_load_id(compiler_t *comp, qstr qst) {
417 if (comp->pass == MP_PASS_SCOPE) {
418 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
419 } else {
Damien George41125902015-03-26 16:44:14 +0000420 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000421 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000422 #else
423 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
424 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000425 }
426}
427
428STATIC void compile_store_id(compiler_t *comp, qstr qst) {
429 if (comp->pass == MP_PASS_SCOPE) {
430 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
431 } else {
Damien George41125902015-03-26 16:44:14 +0000432 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000433 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000434 #else
435 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
436 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000437 }
438}
439
440STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
441 if (comp->pass == MP_PASS_SCOPE) {
442 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
443 } else {
Damien George41125902015-03-26 16:44:14 +0000444 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000445 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000446 #else
447 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
448 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000449 }
450}
451
Damien George2c0842b2014-04-27 16:46:51 +0100452STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100453 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000454 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100455 compile_node(comp, pn);
456 total += 1;
457 }
458 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000459 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100460 for (int i = 0; i < n; i++) {
461 compile_node(comp, pns_list->nodes[i]);
462 }
463 total += n;
464 }
Damien Georgeb9791222014-01-23 00:34:21 +0000465 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100466}
Damien429d7192013-10-04 19:53:11 +0100467
Damien George969a6b32014-12-10 22:07:04 +0000468STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100469 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000470 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100471}
472
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200473STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000474 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
475 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100476}
477
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200478STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000479 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
480 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100481}
482
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200483STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3a205172013-10-12 15:01:56 +0100484 if (node_is_const_false(pn)) {
485 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000486 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100487 }
488 return;
489 } else if (node_is_const_true(pn)) {
490 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000491 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100492 }
493 return;
Damiend99b0522013-12-21 18:17:45 +0000494 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
495 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
496 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
497 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100498 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000499 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100500 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100501 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000502 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100503 }
Damien George0b2fd912015-02-28 14:37:54 +0000504 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000505 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100506 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000507 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100508 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000509 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100510 }
511 }
512 return;
Damiend99b0522013-12-21 18:17:45 +0000513 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100514 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000515 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100516 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000517 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100518 }
Damiend99b0522013-12-21 18:17:45 +0000519 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100520 c_if_cond(comp, pns->nodes[0], !jump_if, label);
521 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100522 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
523 // cond is something in parenthesis
524 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
525 // empty tuple, acts as false for the condition
526 if (jump_if == false) {
527 EMIT_ARG(jump, label);
528 }
529 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
530 // non-empty tuple, acts as true for the condition
531 if (jump_if == true) {
532 EMIT_ARG(jump, label);
533 }
534 } else {
535 // parenthesis around 1 item, is just that item
536 c_if_cond(comp, pns->nodes[0], jump_if, label);
537 }
538 return;
Damien3a205172013-10-12 15:01:56 +0100539 }
540 }
541
542 // nothing special, fall back to default compiling for node and jump
543 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000544 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100545}
546
547typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100548STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100549
Damien George6be0b0a2014-08-15 14:30:52 +0100550STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100551 if (assign_kind != ASSIGN_AUG_STORE) {
552 compile_node(comp, pns->nodes[0]);
553 }
554
Damiend99b0522013-12-21 18:17:45 +0000555 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
556 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
557 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
558 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100559 if (assign_kind != ASSIGN_AUG_STORE) {
560 for (int i = 0; i < n - 1; i++) {
561 compile_node(comp, pns1->nodes[i]);
562 }
563 }
Damiend99b0522013-12-21 18:17:45 +0000564 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
565 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100566 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000567 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100568 if (assign_kind == ASSIGN_AUG_STORE) {
569 EMIT(rot_three);
570 EMIT(store_subscr);
571 } else {
572 compile_node(comp, pns1->nodes[0]);
573 if (assign_kind == ASSIGN_AUG_LOAD) {
574 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100575 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100576 } else {
577 EMIT(store_subscr);
578 }
579 }
Damiend99b0522013-12-21 18:17:45 +0000580 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
581 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100582 if (assign_kind == ASSIGN_AUG_LOAD) {
583 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000584 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100585 } else {
586 if (assign_kind == ASSIGN_AUG_STORE) {
587 EMIT(rot_two);
588 }
Damien Georgeb9791222014-01-23 00:34:21 +0000589 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100590 }
591 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100592 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100593 }
594 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100595 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100596 }
597
Damiend99b0522013-12-21 18:17:45 +0000598 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100599 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100600 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100601
602 return;
603
604cannot_assign:
605 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100606}
607
Damien George0288cf02014-04-11 11:53:00 +0000608// 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 +0100609STATIC 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 +0000610 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
611
612 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000613 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000614 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
615 EMIT_ARG(unpack_ex, 0, num_tail);
616 have_star_index = 0;
617 }
Damien George963a5a32015-01-16 17:47:07 +0000618 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000619 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000620 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000621 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
622 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100623 } else {
Damien George9d181f62014-04-27 16:55:27 +0100624 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100625 return;
626 }
627 }
628 }
Damien George963a5a32015-01-16 17:47:07 +0000629 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000630 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100631 }
Damien George0288cf02014-04-11 11:53:00 +0000632 if (num_head != 0) {
633 if (0 == have_star_index) {
634 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100635 } else {
Damien George0288cf02014-04-11 11:53:00 +0000636 c_assign(comp, node_head, ASSIGN_STORE);
637 }
638 }
Damien George963a5a32015-01-16 17:47:07 +0000639 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000640 if (num_head + i == have_star_index) {
641 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
642 } else {
643 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100644 }
645 }
646}
647
648// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100649STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100650 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000651 assert(!MP_PARSE_NODE_IS_NULL(pn));
652 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000653 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000654 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100655 switch (assign_kind) {
656 case ASSIGN_STORE:
657 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000658 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100659 break;
660 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000661 default:
Damien George542bd6b2015-03-26 14:42:40 +0000662 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100663 break;
664 }
665 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100666 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100667 return;
668 }
669 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000670 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000671 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
672 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100673 case PN_power:
674 // lhs is an index or attribute
675 c_assign_power(comp, pns, assign_kind);
676 break;
677
678 case PN_testlist_star_expr:
679 case PN_exprlist:
680 // lhs is a tuple
681 if (assign_kind != ASSIGN_STORE) {
682 goto bad_aug;
683 }
Damien George0288cf02014-04-11 11:53:00 +0000684 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100685 break;
686
687 case PN_atom_paren:
688 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000689 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100690 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100691 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000692 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000693 if (assign_kind != ASSIGN_STORE) {
694 goto bad_aug;
695 }
Damiend99b0522013-12-21 18:17:45 +0000696 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100697 goto testlist_comp;
698 } else {
699 // parenthesis around 1 item, is just that item
700 pn = pns->nodes[0];
701 goto tail_recursion;
702 }
703 break;
704
705 case PN_atom_bracket:
706 // lhs is something in brackets
707 if (assign_kind != ASSIGN_STORE) {
708 goto bad_aug;
709 }
Damiend99b0522013-12-21 18:17:45 +0000710 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100711 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000712 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000713 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
714 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100715 goto testlist_comp;
716 } else {
717 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000718 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100719 }
720 break;
721
722 default:
Damien George9d181f62014-04-27 16:55:27 +0100723 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100724 }
725 return;
726
727 testlist_comp:
728 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000729 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
730 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
731 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100732 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000733 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000734 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000735 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100736 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000737 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
738 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000739 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100740 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100741 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100742 } else {
743 // sequence with 2 items
744 goto sequence_with_2_items;
745 }
746 } else {
747 // sequence with 2 items
748 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000749 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100750 }
751 return;
752 }
753 return;
754
Damien George9d181f62014-04-27 16:55:27 +0100755 cannot_assign:
756 compile_syntax_error(comp, pn, "can't assign to expression");
757 return;
758
Damien429d7192013-10-04 19:53:11 +0100759 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100760 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100761}
762
Damien George65dc9602015-08-14 12:24:11 +0100763// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100764// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
765// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
766// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100767STATIC 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 +0100768 assert(n_pos_defaults >= 0);
769 assert(n_kw_defaults >= 0);
770
Damien429d7192013-10-04 19:53:11 +0100771 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000772 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100773 int nfree = 0;
774 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000775 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
776 id_info_t *id = &comp->scope_cur->id_info[i];
777 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
778 for (int j = 0; j < this_scope->id_info_len; j++) {
779 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100780 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000781 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000782 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000783 nfree += 1;
784 }
785 }
Damien429d7192013-10-04 19:53:11 +0100786 }
787 }
788 }
Damien429d7192013-10-04 19:53:11 +0100789
790 // make the function/closure
791 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100792 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100793 } else {
Damien George3558f622014-04-20 17:50:40 +0100794 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100795 }
796}
797
Damien George6be0b0a2014-08-15 14:30:52 +0100798STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000799 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100800 comp->have_star = true;
801 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000802 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
803 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100804 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100805 } else {
806 // named star
Damien429d7192013-10-04 19:53:11 +0100807 }
Damien George8b19db02014-04-11 23:25:34 +0100808 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000809
810 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100811 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000812 // TODO do we need to do anything with this?
813
814 } else {
815 mp_parse_node_t pn_id;
816 mp_parse_node_t pn_colon;
817 mp_parse_node_t pn_equal;
818 if (MP_PARSE_NODE_IS_ID(pn)) {
819 // this parameter is just an id
820
821 pn_id = pn;
822 pn_colon = MP_PARSE_NODE_NULL;
823 pn_equal = MP_PARSE_NODE_NULL;
824
Damien George0bb97132015-02-27 14:25:47 +0000825 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000826 // this parameter has a colon and/or equal specifier
827
Damien George0bb97132015-02-27 14:25:47 +0000828 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
829
Damien Georgef41fdd02014-03-03 23:19:11 +0000830 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
831 pn_id = pns->nodes[0];
832 pn_colon = pns->nodes[1];
833 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +0000834 }
835
836 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
837 // this parameter does not have a default value
838
839 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100840 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100841 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000842 return;
843 }
844
845 } else {
846 // this parameter has a default value
847 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
848
Damien George8b19db02014-04-11 23:25:34 +0100849 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000850 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000851 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
852 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100853 // in Micro Python we put the default positional parameters into a tuple using the bytecode
854 // we need to do this here before we start building the map for the default keywords
855 if (comp->num_default_params > 0) {
856 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100857 } else {
858 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100859 }
Damien George69b89d22014-04-11 13:38:30 +0000860 // first default dict param, so make the map
861 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000862 }
Damien Georgef0778a72014-06-07 22:01:00 +0100863
864 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000865 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000866 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000867 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000868 } else {
Damien George69b89d22014-04-11 13:38:30 +0000869 comp->num_default_params += 1;
870 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000871 }
872 }
873
874 // TODO pn_colon not implemented
875 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100876 }
877}
878
879// leaves function object on stack
880// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000881STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100882 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100883 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000884 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100885 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000886 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100887 }
888
889 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +0100890 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +0000891 uint old_num_dict_params = comp->num_dict_params;
892 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +0100893
894 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +0100895 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +0000896 comp->num_dict_params = 0;
897 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +0100898 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000899
Damien Georgea91ac202014-10-05 19:01:34 +0100900 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000901 return MP_QSTR_NULL;
902 }
903
Damien Georgee337f1e2014-03-31 15:18:37 +0100904 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +0100905 // the default keywords args may have already made the tuple; if not, do it now
906 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +0000907 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100908 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +0100909 }
Damien Georgee337f1e2014-03-31 15:18:37 +0100910
Damien429d7192013-10-04 19:53:11 +0100911 // get the scope for this function
912 scope_t *fscope = (scope_t*)pns->nodes[4];
913
914 // make the function
Damien George69b89d22014-04-11 13:38:30 +0000915 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100916
917 // restore variables
Damien George8b19db02014-04-11 23:25:34 +0100918 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +0000919 comp->num_dict_params = old_num_dict_params;
920 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +0100921
922 // return its name (the 'f' in "def f(...):")
923 return fscope->simple_name;
924}
925
926// leaves class object on stack
927// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000928STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100929 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100930 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000931 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100932 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000933 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100934 }
935
936 EMIT(load_build_class);
937
938 // scope for this class
939 scope_t *cscope = (scope_t*)pns->nodes[3];
940
941 // compile the class
942 close_over_variables_etc(comp, cscope, 0, 0);
943
944 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000945 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100946
947 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100948 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
949 mp_parse_node_t parents = pns->nodes[1];
950 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
951 parents = MP_PARSE_NODE_NULL;
952 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000953 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100954 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100955
956 // return its name (the 'C' in class C(...):")
957 return cscope->simple_name;
958}
959
Damien6cdd3af2013-10-05 18:08:26 +0100960// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200961STATIC 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 +0000962 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100963 return false;
964 }
965
966 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100967 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100968 return true;
969 }
970
Damiend99b0522013-12-21 18:17:45 +0000971 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100972 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100973 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100974#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000975 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100976 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000977 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100978 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100979#endif
Damien3ef4abb2013-10-12 16:53:13 +0100980#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000981 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +0100982 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100983#endif
Damien6cdd3af2013-10-05 18:08:26 +0100984 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100985 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100986 }
987
988 return true;
989}
990
Damien George969a6b32014-12-10 22:07:04 +0000991STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100992 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000993 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000994 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100995
Damien6cdd3af2013-10-05 18:08:26 +0100996 // inherit emit options for this function/class definition
997 uint emit_options = comp->scope_cur->emit_options;
998
999 // compile each decorator
1000 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001001 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001002 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1003 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001004
1005 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001006 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001007 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001008
1009 // check for built-in decorators
1010 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1011 // this was a built-in
1012 num_built_in_decorators += 1;
1013
1014 } else {
1015 // not a built-in, compile normally
1016
1017 // compile the decorator function
1018 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001019 for (int j = 1; j < name_len; j++) {
1020 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1021 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001022 }
1023
1024 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001025 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001026 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001027 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001028 compile_node(comp, pns_decorator->nodes[1]);
1029 }
Damien429d7192013-10-04 19:53:11 +01001030 }
1031 }
1032
1033 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001034 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001035 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001036 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001037 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001038 } else {
Damien George0bb97132015-02-27 14:25:47 +00001039 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1040 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001041 }
1042
1043 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001044 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001045 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001046 }
1047
1048 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +00001049 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +01001050}
1051
Damien George969a6b32014-12-10 22:07:04 +00001052STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001053 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001054 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +00001055 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +01001056}
1057
Damien George6be0b0a2014-08-15 14:30:52 +01001058STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001059 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00001060 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001061 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1062 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001063
1064 compile_node(comp, pns->nodes[0]); // base of the power node
1065
Damiend99b0522013-12-21 18:17:45 +00001066 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1067 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1068 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1069 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001070 for (int i = 0; i < n - 1; i++) {
1071 compile_node(comp, pns1->nodes[i]);
1072 }
Damiend99b0522013-12-21 18:17:45 +00001073 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1074 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001075 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001076 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001077 compile_node(comp, pns1->nodes[0]);
1078 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001079 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1080 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001081 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001082 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001083 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001084 }
1085 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001086 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001087 }
1088
Damiend99b0522013-12-21 18:17:45 +00001089 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001090 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001091 }
Damiend99b0522013-12-21 18:17:45 +00001092 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1093 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1094 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1095 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001096 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1097
Damiend99b0522013-12-21 18:17:45 +00001098 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1099 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1100 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001101 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001102 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001103 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001104 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001105 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001106 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001107 c_del_stmt(comp, pns->nodes[0]);
1108 for (int i = 0; i < n; i++) {
1109 c_del_stmt(comp, pns1->nodes[i]);
1110 }
Damiend99b0522013-12-21 18:17:45 +00001111 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001112 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001113 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001114 } else {
1115 // sequence with 2 items
1116 goto sequence_with_2_items;
1117 }
1118 } else {
1119 // sequence with 2 items
1120 sequence_with_2_items:
1121 c_del_stmt(comp, pns->nodes[0]);
1122 c_del_stmt(comp, pns->nodes[1]);
1123 }
1124 } else {
1125 // tuple with 1 element
1126 c_del_stmt(comp, pn);
1127 }
1128 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001129 // TODO is there anything else to implement?
1130 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001131 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001132
1133 return;
1134
1135cannot_delete:
1136 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001137}
1138
Damien George969a6b32014-12-10 22:07:04 +00001139STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001140 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1141}
1142
Damien George969a6b32014-12-10 22:07:04 +00001143STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001144 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001145 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001146 }
Damien George090c9232014-10-17 14:08:49 +00001147 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001148 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001149}
1150
Damien George969a6b32014-12-10 22:07:04 +00001151STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001152 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001153 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001154 }
Damien George090c9232014-10-17 14:08:49 +00001155 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001156 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001157}
1158
Damien George969a6b32014-12-10 22:07:04 +00001159STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001160 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001161 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001162 return;
1163 }
Damiend99b0522013-12-21 18:17:45 +00001164 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001165 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001166 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001167 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001168 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001169 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1170 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 +01001171
Damien George6f355fd2014-04-10 14:11:31 +01001172 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001173 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1174 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1175 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001176 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001177 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1178 } else {
1179 compile_node(comp, pns->nodes[0]);
1180 }
1181 EMIT(return_value);
1182}
1183
Damien George969a6b32014-12-10 22:07:04 +00001184STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001185 compile_node(comp, pns->nodes[0]);
1186 EMIT(pop_top);
1187}
1188
Damien George969a6b32014-12-10 22:07:04 +00001189STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001190 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001191 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001192 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001193 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001194 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001195 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001196 compile_node(comp, pns->nodes[0]);
1197 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001198 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001199 } else {
1200 // raise x
1201 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001202 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001203 }
1204}
1205
Damien George635543c2014-04-10 12:56:52 +01001206// q_base holds the base of the name
1207// eg a -> q_base=a
1208// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001209STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001210 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001211 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1212 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001213 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001214 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001215 pn = pns->nodes[0];
1216 is_as = true;
1217 }
Damien George635543c2014-04-10 12:56:52 +01001218 if (MP_PARSE_NODE_IS_NULL(pn)) {
1219 // empty name (eg, from . import x)
1220 *q_base = MP_QSTR_;
1221 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1222 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001223 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001224 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001225 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001226 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001227 }
Damien George635543c2014-04-10 12:56:52 +01001228 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001229 } else {
1230 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001231 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001232 {
Damien429d7192013-10-04 19:53:11 +01001233 // a name of the form a.b.c
1234 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001235 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001236 }
Damiend99b0522013-12-21 18:17:45 +00001237 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001238 int len = n - 1;
1239 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001240 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001241 }
Damien George55baff42014-01-21 21:40:13 +00001242 byte *q_ptr;
1243 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001244 for (int i = 0; i < n; i++) {
1245 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001246 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001247 }
Damien George39dc1452014-10-03 19:52:22 +01001248 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001249 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001250 memcpy(str_dest, str_src, str_src_len);
1251 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001252 }
Damien George635543c2014-04-10 12:56:52 +01001253 qstr q_full = qstr_build_end(q_ptr);
1254 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001255 if (is_as) {
1256 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001257 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001258 }
1259 }
Damien429d7192013-10-04 19:53:11 +01001260 }
Damien429d7192013-10-04 19:53:11 +01001261 }
1262}
1263
Damien George6be0b0a2014-08-15 14:30:52 +01001264STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001265 EMIT_ARG(load_const_small_int, 0); // level 0 import
1266 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1267 qstr q_base;
1268 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001269 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001270}
1271
Damien George969a6b32014-12-10 22:07:04 +00001272STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001273 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1274}
1275
Damien George969a6b32014-12-10 22:07:04 +00001276STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001277 mp_parse_node_t pn_import_source = pns->nodes[0];
1278
1279 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1280 uint import_level = 0;
1281 do {
1282 mp_parse_node_t pn_rel;
1283 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)) {
1284 // This covers relative imports with dots only like "from .. import"
1285 pn_rel = pn_import_source;
1286 pn_import_source = MP_PARSE_NODE_NULL;
1287 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1288 // This covers relative imports starting with dot(s) like "from .foo import"
1289 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1290 pn_rel = pns_2b->nodes[0];
1291 pn_import_source = pns_2b->nodes[1];
1292 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1293 } else {
1294 // Not a relative import
1295 break;
1296 }
1297
1298 // get the list of . and/or ...'s
1299 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001300 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001301
1302 // count the total number of .'s
1303 for (int i = 0; i < n; i++) {
1304 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1305 import_level++;
1306 } else {
1307 // should be an MP_TOKEN_ELLIPSIS
1308 import_level += 3;
1309 }
1310 }
1311 } while (0);
1312
Damiend99b0522013-12-21 18:17:45 +00001313 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001314 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001315
1316 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001317 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001318 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001319
1320 // do the import
Damien George635543c2014-04-10 12:56:52 +01001321 qstr dummy_q;
1322 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001323 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001324
Damien429d7192013-10-04 19:53:11 +01001325 } else {
Damien George635543c2014-04-10 12:56:52 +01001326 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001327
1328 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001329 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001330 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001331 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001332 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1333 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1334 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001335 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001336 }
Damien Georgeb9791222014-01-23 00:34:21 +00001337 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001338
1339 // do the import
Damien George635543c2014-04-10 12:56:52 +01001340 qstr dummy_q;
1341 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001342 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001343 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1344 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1345 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001346 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001347 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001348 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001349 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001350 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001351 }
1352 }
1353 EMIT(pop_top);
1354 }
1355}
1356
Damien George584ba672014-12-21 17:26:45 +00001357STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1358 bool added;
1359 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001360 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1361 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001362 return;
1363 }
1364 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1365
1366 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1367 id_info = scope_find_global(comp->scope_cur, qst);
1368 if (id_info != NULL) {
1369 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1370 }
1371}
1372
Damien George969a6b32014-12-10 22:07:04 +00001373STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001374 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001375 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001376 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001377 for (int i = 0; i < n; i++) {
1378 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001379 }
1380 }
1381}
1382
Damien George584ba672014-12-21 17:26:45 +00001383STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1384 bool added;
1385 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001386 if (!added && id_info->kind != ID_INFO_KIND_FREE) {
1387 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001388 return;
1389 }
1390 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1391 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)) {
1392 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1393 return;
1394 }
1395 id_info->kind = ID_INFO_KIND_FREE;
1396 scope_close_over_in_parents(comp->scope_cur, qst);
1397}
1398
Damien George969a6b32014-12-10 22:07:04 +00001399STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001400 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001401 if (comp->scope_cur->kind == SCOPE_MODULE) {
1402 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1403 return;
1404 }
1405 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001406 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001407 for (int i = 0; i < n; i++) {
1408 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001409 }
1410 }
1411}
1412
Damien George969a6b32014-12-10 22:07:04 +00001413STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001414 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001415 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001416 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001417 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001418 // assertion message
1419 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001420 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001421 }
Damien Georgeb9791222014-01-23 00:34:21 +00001422 EMIT_ARG(raise_varargs, 1);
1423 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001424}
1425
Damien George969a6b32014-12-10 22:07:04 +00001426STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001427 // TODO proper and/or short circuiting
1428
Damien George6f355fd2014-04-10 14:11:31 +01001429 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001430
Damien George65dc9602015-08-14 12:24:11 +01001431 // optimisation: don't emit anything when "if False"
1432 if (!node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001433 uint l_fail = comp_next_label(comp);
1434 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001435
Damien George391db862014-10-17 17:57:33 +00001436 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001437
Damien George65dc9602015-08-14 12:24:11 +01001438 // optimisation: skip everything else when "if True"
1439 if (node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001440 goto done;
1441 }
1442
1443 if (
Damien George65dc9602015-08-14 12:24:11 +01001444 // optimisation: don't jump over non-existent elif/else blocks
1445 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001446 // optimisation: don't jump if last instruction was return
1447 && !EMIT(last_emit_was_return_value)
1448 ) {
1449 // jump over elif/else blocks
1450 EMIT_ARG(jump, l_end);
1451 }
1452
1453 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001454 }
1455
Damien George235f9b32014-10-17 17:30:16 +00001456 // compile elif blocks (if any)
1457 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001458 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 +00001459 for (int i = 0; i < n_elif; i++) {
1460 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1461 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001462
Damien George65dc9602015-08-14 12:24:11 +01001463 // optimisation: don't emit anything when "if False"
1464 if (!node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001465 uint l_fail = comp_next_label(comp);
1466 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001467
Damien George391db862014-10-17 17:57:33 +00001468 compile_node(comp, pns_elif->nodes[1]); // elif block
1469
Damien George65dc9602015-08-14 12:24:11 +01001470 // optimisation: skip everything else when "elif True"
1471 if (node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001472 goto done;
1473 }
1474
1475 // optimisation: don't jump if last instruction was return
1476 if (!EMIT(last_emit_was_return_value)) {
1477 EMIT_ARG(jump, l_end);
1478 }
1479 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001480 }
1481 }
1482
1483 // compile else block
1484 compile_node(comp, pns->nodes[3]); // can be null
1485
Damien George391db862014-10-17 17:57:33 +00001486done:
Damien Georgeb9791222014-01-23 00:34:21 +00001487 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001488}
1489
Damien Georgecbddb272014-02-01 20:08:18 +00001490#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001491 uint16_t old_break_label = comp->break_label; \
1492 uint16_t old_continue_label = comp->continue_label; \
1493 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001494 uint break_label = comp_next_label(comp); \
1495 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001496 comp->break_label = break_label; \
1497 comp->continue_label = continue_label; \
1498 comp->break_continue_except_level = comp->cur_except_level;
1499
1500#define END_BREAK_CONTINUE_BLOCK \
1501 comp->break_label = old_break_label; \
1502 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001503 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001504
Damien George969a6b32014-12-10 22:07:04 +00001505STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001506 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001507
Damien George391db862014-10-17 17:57:33 +00001508 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1509 uint top_label = comp_next_label(comp);
1510 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1511 EMIT_ARG(jump, continue_label);
1512 }
1513 EMIT_ARG(label_assign, top_label);
1514 compile_node(comp, pns->nodes[1]); // body
1515 EMIT_ARG(label_assign, continue_label);
1516 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1517 }
Damience89a212013-10-15 22:25:17 +01001518
1519 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001520 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001521
1522 compile_node(comp, pns->nodes[2]); // else
1523
Damien Georgeb9791222014-01-23 00:34:21 +00001524 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001525}
1526
Damien Georgee181c0d2014-12-12 17:19:56 +00001527// This function compiles an optimised for-loop of the form:
1528// for <var> in range(<start>, <end>, <step>):
1529// <body>
1530// else:
1531// <else>
1532// <var> must be an identifier and <step> must be a small-int.
1533//
1534// Semantics of for-loop require:
1535// - final failing value should not be stored in the loop variable
1536// - if the loop never runs, the loop variable should never be assigned
1537// - assignments to <var>, <end> or <step> in the body do not alter the loop
1538// (<step> is a constant for us, so no need to worry about it changing)
1539//
1540// If <end> is a small-int, then the stack during the for-loop contains just
1541// the current value of <var>. Otherwise, the stack contains <end> then the
1542// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001543STATIC 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 +00001544 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001545
Damien George6f355fd2014-04-10 14:11:31 +01001546 uint top_label = comp_next_label(comp);
1547 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001548
Damien Georgee181c0d2014-12-12 17:19:56 +00001549 // put the end value on the stack if it's not a small-int constant
1550 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1551 if (end_on_stack) {
1552 compile_node(comp, pn_end);
1553 }
1554
1555 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001556 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001557
Damien Georgeb9791222014-01-23 00:34:21 +00001558 EMIT_ARG(jump, entry_label);
1559 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001560
Damien Georgec33ce602014-12-11 17:35:23 +00001561 // duplicate next value and store it to var
1562 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001563 c_assign(comp, pn_var, ASSIGN_STORE);
1564
Damienf3822fc2013-11-09 20:12:03 +00001565 // compile body
1566 compile_node(comp, pn_body);
1567
Damien Georgeb9791222014-01-23 00:34:21 +00001568 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001569
Damien Georgee181c0d2014-12-12 17:19:56 +00001570 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001571 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001572 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001573
Damien Georgeb9791222014-01-23 00:34:21 +00001574 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001575
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001576 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001577 if (end_on_stack) {
1578 EMIT(dup_top_two);
1579 EMIT(rot_two);
1580 } else {
1581 EMIT(dup_top);
1582 compile_node(comp, pn_end);
1583 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001584 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1585 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001586 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001587 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001588 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001589 }
Damien George63f38322015-02-28 15:04:06 +00001590 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001591
1592 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001593 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001594
1595 compile_node(comp, pn_else);
1596
Damien Georgeb9791222014-01-23 00:34:21 +00001597 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001598
1599 // discard final value of var that failed the loop condition
1600 EMIT(pop_top);
1601
1602 // discard <end> value if it's on the stack
1603 if (end_on_stack) {
1604 EMIT(pop_top);
1605 }
Damienf72fd0e2013-11-06 20:20:49 +00001606}
1607
Damien George969a6b32014-12-10 22:07:04 +00001608STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001609 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1610 // this is actually slower, but uses no heap memory
1611 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001612 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 +00001613 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001614 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1615 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1616 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1617 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001618 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1619 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001620 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001621 mp_parse_node_t pn_range_start;
1622 mp_parse_node_t pn_range_end;
1623 mp_parse_node_t pn_range_step;
1624 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001625 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001626 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001627 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001628 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001629 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001630 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001631 } else if (n_args == 2) {
1632 pn_range_start = args[0];
1633 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001634 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001635 } else {
1636 pn_range_start = args[0];
1637 pn_range_end = args[1];
1638 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001639 // We need to know sign of step. This is possible only if it's constant
1640 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1641 optimize = false;
1642 }
Damienf72fd0e2013-11-06 20:20:49 +00001643 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001644 }
1645 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001646 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1647 return;
1648 }
1649 }
1650 }
Damienf72fd0e2013-11-06 20:20:49 +00001651
Damien Georgecbddb272014-02-01 20:08:18 +00001652 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001653 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001654
Damien George6f355fd2014-04-10 14:11:31 +01001655 uint pop_label = comp_next_label(comp);
1656 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001657
Damien429d7192013-10-04 19:53:11 +01001658 compile_node(comp, pns->nodes[1]); // iterator
1659 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001660 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001661 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001662 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1663 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001664 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001665 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001666 }
Damien Georgeb9791222014-01-23 00:34:21 +00001667 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001668 EMIT(for_iter_end);
1669
1670 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001671 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001672
Damien429d7192013-10-04 19:53:11 +01001673 compile_node(comp, pns->nodes[3]); // else (not tested)
1674
Damien Georgeb9791222014-01-23 00:34:21 +00001675 EMIT_ARG(label_assign, break_label);
1676 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001677}
1678
Damien George6be0b0a2014-08-15 14:30:52 +01001679STATIC 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 +01001680 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001681 uint l1 = comp_next_label(comp);
1682 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001683
Damien Georgeb9791222014-01-23 00:34:21 +00001684 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001685 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001686
Damien429d7192013-10-04 19:53:11 +01001687 compile_node(comp, pn_body); // body
1688 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001689 EMIT_ARG(jump, success_label); // jump over exception handler
1690
1691 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001692 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001693
Damien George6f355fd2014-04-10 14:11:31 +01001694 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001695
1696 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001697 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1698 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001699
1700 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001701 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001702
Damiend99b0522013-12-21 18:17:45 +00001703 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001704 // this is a catch all exception handler
1705 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001706 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001707 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001708 return;
1709 }
1710 } else {
1711 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001712 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1713 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1714 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1715 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001716 // handler binds the exception to a local
1717 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001718 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001719 }
1720 }
1721 EMIT(dup_top);
1722 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001723 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001724 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001725 }
1726
1727 EMIT(pop_top);
1728
1729 if (qstr_exception_local == 0) {
1730 EMIT(pop_top);
1731 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001732 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001733 }
1734
1735 EMIT(pop_top);
1736
Damien George6f355fd2014-04-10 14:11:31 +01001737 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001738 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001739 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001740 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001741 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001742 }
1743 compile_node(comp, pns_except->nodes[1]);
1744 if (qstr_exception_local != 0) {
1745 EMIT(pop_block);
1746 }
1747 EMIT(pop_except);
1748 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001749 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1750 EMIT_ARG(label_assign, l3);
1751 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001752 compile_store_id(comp, qstr_exception_local);
1753 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001754
Damien George8dcc0c72014-03-27 10:55:21 +00001755 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001756 EMIT(end_finally);
1757 }
Damien Georgeb9791222014-01-23 00:34:21 +00001758 EMIT_ARG(jump, l2);
1759 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001760 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001761 }
1762
Damien George8dcc0c72014-03-27 10:55:21 +00001763 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001764 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001765 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001766
Damien Georgeb9791222014-01-23 00:34:21 +00001767 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001768 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001769 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001770}
1771
Damien George6be0b0a2014-08-15 14:30:52 +01001772STATIC 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 +01001773 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001774
Damien Georgeb9791222014-01-23 00:34:21 +00001775 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001776 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001777
Damien429d7192013-10-04 19:53:11 +01001778 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001779 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001780 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001781 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001782 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001783 } else {
1784 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1785 }
1786 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001787 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1788 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001789 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001790
Damien George8dcc0c72014-03-27 10:55:21 +00001791 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001792 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001793}
1794
Damien George969a6b32014-12-10 22:07:04 +00001795STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001796 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1797 {
Damiend99b0522013-12-21 18:17:45 +00001798 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1799 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001800 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001801 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1802 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001803 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001804 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001805 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001806 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001807 // no finally
1808 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1809 } else {
1810 // have finally
Damiend99b0522013-12-21 18:17:45 +00001811 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 +01001812 }
1813 } else {
1814 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001815 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001816 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001817 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001818 }
Damien429d7192013-10-04 19:53:11 +01001819 }
1820}
1821
Damien George6be0b0a2014-08-15 14:30:52 +01001822STATIC 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 +01001823 if (n == 0) {
1824 // no more pre-bits, compile the body of the with
1825 compile_node(comp, body);
1826 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001827 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001828 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001829 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001830 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001831 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001832 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001833 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1834 } else {
1835 // this pre-bit is just an expression
1836 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001837 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001838 EMIT(pop_top);
1839 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001840 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001841 // compile additional pre-bits and the body
1842 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1843 // finish this with block
1844 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001845 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1846 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001847 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001848 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001849 EMIT(end_finally);
1850 }
1851}
1852
Damien George969a6b32014-12-10 22:07:04 +00001853STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001854 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001855 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001856 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001857 assert(n > 0);
1858
1859 // compile in a nested fashion
1860 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1861}
1862
Damien George969a6b32014-12-10 22:07:04 +00001863STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001864 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001865 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1866 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001867 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001868 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001869 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001870 EMIT(pop_top);
1871
Damien429d7192013-10-04 19:53:11 +01001872 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001873 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001874 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001875 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001876 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1877 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001878 // do nothing with a lonely constant
1879 } else {
1880 compile_node(comp, pns->nodes[0]); // just an expression
1881 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1882 }
Damien429d7192013-10-04 19:53:11 +01001883 }
1884 } else {
Damiend99b0522013-12-21 18:17:45 +00001885 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1886 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001887 if (kind == PN_expr_stmt_augassign) {
1888 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1889 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001890 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001891 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001892 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001893 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1894 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1895 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1896 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1897 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1898 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1899 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1900 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1901 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1902 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1903 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001904 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001905 }
Damien George7e5fb242014-02-01 22:18:47 +00001906 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001907 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1908 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001909 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1910 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001911 // following CPython, we store left-most first
1912 if (rhs > 0) {
1913 EMIT(dup_top);
1914 }
1915 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1916 for (int i = 0; i < rhs; i++) {
1917 if (i + 1 < rhs) {
1918 EMIT(dup_top);
1919 }
Damiend99b0522013-12-21 18:17:45 +00001920 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001921 }
Damien George0bb97132015-02-27 14:25:47 +00001922 } else {
1923 assert(kind == PN_expr_stmt_assign); // should be
Damien George42e0c592015-03-14 13:11:35 +00001924 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
1925 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001926 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1927 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1928 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001929 // optimisation for a, b = c, d
Damien George4dea9222015-04-09 15:29:54 +00001930 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1931 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001932 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1933 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1934 // can't optimise when it's a star expression on the lhs
1935 goto no_optimisation;
1936 }
Damien429d7192013-10-04 19:53:11 +01001937 compile_node(comp, pns10->nodes[0]); // rhs
1938 compile_node(comp, pns10->nodes[1]); // rhs
1939 EMIT(rot_two);
1940 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1941 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001942 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
1943 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001944 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1945 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1946 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001947 // optimisation for a, b, c = d, e, f
Damien George4dea9222015-04-09 15:29:54 +00001948 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1949 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001950 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1951 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1952 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1953 // can't optimise when it's a star expression on the lhs
1954 goto no_optimisation;
1955 }
Damien429d7192013-10-04 19:53:11 +01001956 compile_node(comp, pns10->nodes[0]); // rhs
1957 compile_node(comp, pns10->nodes[1]); // rhs
1958 compile_node(comp, pns10->nodes[2]); // rhs
1959 EMIT(rot_three);
1960 EMIT(rot_two);
1961 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1962 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1963 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1964 } else {
Damien George495d7812014-04-08 17:51:47 +01001965 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01001966 compile_node(comp, pns1->nodes[0]); // rhs
1967 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1968 }
Damien429d7192013-10-04 19:53:11 +01001969 }
1970 }
1971}
1972
Damien George6be0b0a2014-08-15 14:30:52 +01001973STATIC 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 +00001974 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001975 compile_node(comp, pns->nodes[0]);
1976 for (int i = 1; i < num_nodes; i += 1) {
1977 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001978 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001979 }
1980}
1981
Damien George969a6b32014-12-10 22:07:04 +00001982STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001983 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1984 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001985
Damien George6f355fd2014-04-10 14:11:31 +01001986 uint l_fail = comp_next_label(comp);
1987 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001988 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1989 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001990 EMIT_ARG(jump, l_end);
1991 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001992 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01001993 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001994 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001995}
1996
Damien George969a6b32014-12-10 22:07:04 +00001997STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001998 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001999 //mp_parse_node_t pn_params = pns->nodes[0];
2000 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002001
Damien George36db6bc2014-05-07 17:24:22 +01002002 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002003 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002004 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 +01002005 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002006 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002007 }
2008
2009 // get the scope for this lambda
2010 scope_t *this_scope = (scope_t*)pns->nodes[2];
2011
2012 // make the lambda
2013 close_over_variables_etc(comp, this_scope, 0, 0);
2014}
2015
Damien George7711afb2015-02-28 15:10:18 +00002016STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002017 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002018 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002019 for (int i = 0; i < n; i += 1) {
2020 compile_node(comp, pns->nodes[i]);
2021 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002022 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002023 }
2024 }
Damien Georgeb9791222014-01-23 00:34:21 +00002025 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002026}
2027
Damien George7711afb2015-02-28 15:10:18 +00002028STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2029 compile_or_and_test(comp, pns, true);
2030}
2031
Damien George969a6b32014-12-10 22:07:04 +00002032STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002033 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002034}
2035
Damien George969a6b32014-12-10 22:07:04 +00002036STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002037 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002038 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002039}
2040
Damien George969a6b32014-12-10 22:07:04 +00002041STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002042 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002043 compile_node(comp, pns->nodes[0]);
2044 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002045 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002046 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002047 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002048 }
2049 for (int i = 1; i + 1 < num_nodes; i += 2) {
2050 compile_node(comp, pns->nodes[i + 1]);
2051 if (i + 2 < num_nodes) {
2052 EMIT(dup_top);
2053 EMIT(rot_three);
2054 }
Damien George7e5fb242014-02-01 22:18:47 +00002055 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002056 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002057 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002058 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2059 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2060 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2061 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2062 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2063 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002064 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002065 }
2066 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002067 } else {
2068 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002069 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2070 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002071 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002072 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002073 } else {
2074 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002075 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002076 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002077 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002078 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002079 }
Damien429d7192013-10-04 19:53:11 +01002080 }
Damien429d7192013-10-04 19:53:11 +01002081 }
2082 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002083 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002084 }
2085 }
2086 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002087 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002088 EMIT_ARG(jump, l_end);
2089 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002090 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002091 EMIT(rot_two);
2092 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002093 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002094 }
2095}
2096
Damien George969a6b32014-12-10 22:07:04 +00002097STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002098 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002099}
2100
Damien George969a6b32014-12-10 22:07:04 +00002101STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002102 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002103}
2104
Damien George969a6b32014-12-10 22:07:04 +00002105STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002106 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002107}
2108
Damien George969a6b32014-12-10 22:07:04 +00002109STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002110 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002111}
2112
Damien George969a6b32014-12-10 22:07:04 +00002113STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002114 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002115 compile_node(comp, pns->nodes[0]);
2116 for (int i = 1; i + 1 < num_nodes; i += 2) {
2117 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002118 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002119 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002120 } else {
Damien George0bb97132015-02-27 14:25:47 +00002121 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2122 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002123 }
2124 }
2125}
2126
Damien George969a6b32014-12-10 22:07:04 +00002127STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002128 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002129 compile_node(comp, pns->nodes[0]);
2130 for (int i = 1; i + 1 < num_nodes; i += 2) {
2131 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002132 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002133 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002134 } else {
Damien George0bb97132015-02-27 14:25:47 +00002135 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2136 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002137 }
2138 }
2139}
2140
Damien George969a6b32014-12-10 22:07:04 +00002141STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002142 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002143 compile_node(comp, pns->nodes[0]);
2144 for (int i = 1; i + 1 < num_nodes; i += 2) {
2145 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002146 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002147 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002148 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002149 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002150 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002151 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002152 } else {
Damien George0bb97132015-02-27 14:25:47 +00002153 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2154 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002155 }
2156 }
2157}
2158
Damien George969a6b32014-12-10 22:07:04 +00002159STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002160 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002161 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002162 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002163 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002164 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002165 } else {
Damien George0bb97132015-02-27 14:25:47 +00002166 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2167 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002168 }
2169}
2170
Damien George969a6b32014-12-10 22:07:04 +00002171STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002172 // this is to handle special super() call
2173 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2174
2175 compile_generic_all_nodes(comp, pns);
2176}
2177
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002178STATIC 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 +01002179 // function to call is on top of stack
2180
Damien George35e2a4e2014-02-05 00:51:47 +00002181 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002182 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 +00002183 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002184 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002185 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002186 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002187 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002188 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002189 EMIT_ARG(call_function, 2, 0, 0);
2190 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002191 }
2192 }
Damien George01039b52014-12-21 17:44:27 +00002193 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002194 return;
2195 }
Damien George35e2a4e2014-02-05 00:51:47 +00002196
Damien George2c0842b2014-04-27 16:46:51 +01002197 // get the list of arguments
2198 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002199 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002200
Damien George2c0842b2014-04-27 16:46:51 +01002201 // compile the arguments
2202 // Rather than calling compile_node on the list, we go through the list of args
2203 // explicitly here so that we can count the number of arguments and give sensible
2204 // error messages.
2205 int n_positional = n_positional_extra;
2206 uint n_keyword = 0;
2207 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002208 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002209 for (int i = 0; i < n_args; i++) {
2210 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2211 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2212 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2213 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2214 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2215 return;
2216 }
2217 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002218 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002219 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2220 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2221 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2222 return;
2223 }
2224 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002225 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002226 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2227 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2228 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2229 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2230 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2231 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2232 return;
2233 }
Damien George59fba2d2015-06-25 14:42:13 +00002234 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien George2c0842b2014-04-27 16:46:51 +01002235 compile_node(comp, pns2->nodes[0]);
2236 n_keyword += 1;
2237 } else {
2238 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2239 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2240 n_positional++;
2241 }
2242 } else {
2243 goto normal_argument;
2244 }
2245 } else {
2246 normal_argument:
2247 if (n_keyword > 0) {
2248 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2249 return;
2250 }
2251 compile_node(comp, args[i]);
2252 n_positional++;
2253 }
Damien429d7192013-10-04 19:53:11 +01002254 }
2255
Delio Brignolie6978a42015-09-17 12:07:06 +02002256 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002257 // if we had one but not the other then we load "null" as a place holder
2258 if (star_flags != 0) {
2259 if (star_args_node == NULL) {
2260 EMIT(load_null);
2261 } else {
2262 compile_node(comp, star_args_node->nodes[0]);
2263 }
2264 if (dblstar_args_node == NULL) {
2265 EMIT(load_null);
2266 } else {
2267 compile_node(comp, dblstar_args_node->nodes[0]);
2268 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002269 }
2270
Damien George2c0842b2014-04-27 16:46:51 +01002271 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002272 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002273 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002274 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002275 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002276 }
Damien429d7192013-10-04 19:53:11 +01002277}
2278
Damien George969a6b32014-12-10 22:07:04 +00002279STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002280 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002281 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002282 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 +01002283 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002284 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2285 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002286 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002287 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002288 i += 1;
2289 } else {
2290 compile_node(comp, pns->nodes[i]);
2291 }
Damien George35e2a4e2014-02-05 00:51:47 +00002292 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002293 }
2294}
2295
Damien George969a6b32014-12-10 22:07:04 +00002296STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002297 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002298 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002299}
2300
Damien George969a6b32014-12-10 22:07:04 +00002301STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002302 // a list of strings
Damien63321742013-12-10 17:41:49 +00002303
2304 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002305 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002306 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002307 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002308 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002309 int pn_kind;
2310 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2311 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2312 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2313 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2314 } else {
2315 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2316 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002317 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2318 pn_kind = MP_PARSE_NODE_STRING;
2319 } else {
2320 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2321 pn_kind = MP_PARSE_NODE_BYTES;
2322 }
Damien George40f3c022014-07-03 13:25:24 +01002323 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002324 }
Damien63321742013-12-10 17:41:49 +00002325 if (i == 0) {
2326 string_kind = pn_kind;
2327 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002328 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002329 return;
2330 }
Damien429d7192013-10-04 19:53:11 +01002331 }
Damien63321742013-12-10 17:41:49 +00002332
Damien George65ef6b72015-01-14 21:17:27 +00002333 // if we are not in the last pass, just load a dummy object
2334 if (comp->pass != MP_PASS_EMIT) {
2335 EMIT_ARG(load_const_obj, mp_const_none);
2336 return;
2337 }
2338
Damien63321742013-12-10 17:41:49 +00002339 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002340 vstr_t vstr;
2341 vstr_init_len(&vstr, n_bytes);
2342 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002343 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002344 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002345 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002346 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2347 memcpy(s_dest, s, s_len);
2348 s_dest += s_len;
2349 } else {
2350 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002351 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2352 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002353 }
Damien63321742013-12-10 17:41:49 +00002354 }
Damien George65ef6b72015-01-14 21:17:27 +00002355
2356 // load the object
Damien George05005f62015-01-21 22:48:37 +00002357 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 +01002358}
2359
2360// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002361STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002362 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2363 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2364 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002365
Damien George36db6bc2014-05-07 17:24:22 +01002366 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002367 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002368 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 +01002369 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002370 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002371 }
2372
2373 // get the scope for this comprehension
2374 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2375
2376 // compile the comprehension
2377 close_over_variables_etc(comp, this_scope, 0, 0);
2378
2379 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2380 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002381 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002382}
2383
Damien George969a6b32014-12-10 22:07:04 +00002384STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002385 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002386 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002387 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2388 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2389 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2390 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2391 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2392 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2393 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002394 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002395 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002396 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002397 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002398 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002399 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002400 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002401 // generator expression
2402 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2403 } else {
2404 // tuple with 2 items
2405 goto tuple_with_2_items;
2406 }
2407 } else {
2408 // tuple with 2 items
2409 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002410 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002411 }
2412 } else {
2413 // parenthesis around a single item, is just that item
2414 compile_node(comp, pns->nodes[0]);
2415 }
2416}
2417
Damien George969a6b32014-12-10 22:07:04 +00002418STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002419 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002420 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002421 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002422 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2423 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2424 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2425 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2426 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002427 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002428 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002429 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002430 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002431 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002432 // list of many items
2433 compile_node(comp, pns2->nodes[0]);
2434 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002435 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002436 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002437 // list comprehension
2438 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2439 } else {
2440 // list with 2 items
2441 goto list_with_2_items;
2442 }
2443 } else {
2444 // list with 2 items
2445 list_with_2_items:
2446 compile_node(comp, pns2->nodes[0]);
2447 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002448 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002449 }
2450 } else {
2451 // list with 1 item
2452 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002453 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002454 }
2455}
2456
Damien George969a6b32014-12-10 22:07:04 +00002457STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002458 mp_parse_node_t pn = pns->nodes[0];
2459 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002460 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002461 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002462 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2463 pns = (mp_parse_node_struct_t*)pn;
2464 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002465 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002466 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002467 compile_node(comp, pn);
2468 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002469 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2470 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2471 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2472 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002473 // dict/set with multiple elements
2474
2475 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002476 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002477 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002478
2479 // first element sets whether it's a dict or set
2480 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002481 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002482 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002483 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002484 compile_node(comp, pns->nodes[0]);
2485 EMIT(store_map);
2486 is_dict = true;
2487 } else {
2488 // a set
2489 compile_node(comp, pns->nodes[0]); // 1st value of set
2490 is_dict = false;
2491 }
2492
2493 // process rest of elements
2494 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002495 mp_parse_node_t pn_i = nodes[i];
2496 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2497 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002498 if (is_dict) {
2499 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002500 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002501 return;
2502 }
2503 EMIT(store_map);
2504 } else {
2505 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002506 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002507 return;
2508 }
2509 }
2510 }
2511
Damien Georgee37dcaa2014-12-27 17:07:16 +00002512 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002513 // if it's a set, build it
2514 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002515 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002516 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002517 #endif
Damien George0bb97132015-02-27 14:25:47 +00002518 } else {
2519 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002520 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002521 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002522 // a dictionary comprehension
2523 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2524 } else {
2525 // a set comprehension
2526 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2527 }
Damien429d7192013-10-04 19:53:11 +01002528 }
2529 } else {
2530 // set with one element
2531 goto set_with_one_element;
2532 }
2533 } else {
2534 // set with one element
2535 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002536 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002537 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002538 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002539 #else
2540 assert(0);
2541 #endif
Damien429d7192013-10-04 19:53:11 +01002542 }
2543}
2544
Damien George969a6b32014-12-10 22:07:04 +00002545STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002546 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002547}
2548
Damien George969a6b32014-12-10 22:07:04 +00002549STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002550 // object who's index we want is on top of stack
2551 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002552 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002553}
2554
Damien George969a6b32014-12-10 22:07:04 +00002555STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002556 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002557 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002558}
2559
Damien George83204f32014-12-27 17:20:41 +00002560#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002561STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002562 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2563 mp_parse_node_t pn = pns->nodes[0];
2564 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002565 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002566 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2567 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002568 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2569 pns = (mp_parse_node_struct_t*)pn;
2570 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002571 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002572 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002573 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002574 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002575 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002576 } else {
2577 // [?::x]
2578 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002579 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002580 }
Damiend99b0522013-12-21 18:17:45 +00002581 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002582 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002583 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2584 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2585 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2586 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002587 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002588 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002589 } else {
2590 // [?:x:x]
2591 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002592 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002593 }
2594 } else {
2595 // [?:x]
2596 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002597 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002598 }
2599 } else {
2600 // [?:x]
2601 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002602 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002603 }
2604}
2605
Damien George969a6b32014-12-10 22:07:04 +00002606STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002607 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002608 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2609 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002610}
2611
Damien George969a6b32014-12-10 22:07:04 +00002612STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002613 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002614 compile_subscript_3_helper(comp, pns);
2615}
Damien George83204f32014-12-27 17:20:41 +00002616#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002617
Damien George969a6b32014-12-10 22:07:04 +00002618STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002619 // if this is called then we are compiling a dict key:value pair
2620 compile_node(comp, pns->nodes[1]); // value
2621 compile_node(comp, pns->nodes[0]); // key
2622}
2623
Damien George969a6b32014-12-10 22:07:04 +00002624STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002625 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002626 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002627 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002628}
2629
Damien George969a6b32014-12-10 22:07:04 +00002630STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002631 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002632 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002633 return;
2634 }
Damiend99b0522013-12-21 18:17:45 +00002635 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002636 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002637 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002638 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2639 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002640 compile_node(comp, pns->nodes[0]);
2641 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002642 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002643 EMIT(yield_from);
2644 } else {
2645 compile_node(comp, pns->nodes[0]);
2646 EMIT(yield_value);
2647 }
2648}
2649
Damien George65ef6b72015-01-14 21:17:27 +00002650STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2651 // only create and load the actual str object on the last pass
2652 if (comp->pass != MP_PASS_EMIT) {
2653 EMIT_ARG(load_const_obj, mp_const_none);
2654 } else {
2655 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2656 }
2657}
2658
2659STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2660 // only create and load the actual bytes object on the last pass
2661 if (comp->pass != MP_PASS_EMIT) {
2662 EMIT_ARG(load_const_obj, mp_const_none);
2663 } else {
2664 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2665 }
2666}
2667
Damien George7d414a12015-02-08 01:57:40 +00002668STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2669 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2670}
2671
Damiend99b0522013-12-21 18:17:45 +00002672typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002673STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002674#define nc NULL
2675#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002676#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002677#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002678#undef nc
2679#undef c
2680#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002681 NULL,
2682 compile_string,
2683 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002684 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002685};
2686
Damien George6be0b0a2014-08-15 14:30:52 +01002687STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002688 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002689 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002690 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002691 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002692 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002693 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002694 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002695 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002696 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002697 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2698 case MP_PARSE_NODE_BYTES:
2699 // only create and load the actual bytes object on the last pass
2700 if (comp->pass != MP_PASS_EMIT) {
2701 EMIT_ARG(load_const_obj, mp_const_none);
2702 } else {
2703 mp_uint_t len;
2704 const byte *data = qstr_data(arg, &len);
2705 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2706 }
2707 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002708 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002709 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002710 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002711 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002712 // do nothing
2713 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002714 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002715 }
2716 break;
Damien429d7192013-10-04 19:53:11 +01002717 }
2718 } else {
Damiend99b0522013-12-21 18:17:45 +00002719 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002720 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002721 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2722 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002723#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002724 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2725 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002726#endif
Damien George65ef6b72015-01-14 21:17:27 +00002727 compile_syntax_error(comp, pn, "internal compiler error");
2728 } else {
2729 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002730 }
2731 }
2732}
2733
Damien George2ac4af62014-08-15 16:45:41 +01002734STATIC 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 +01002735 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002736 qstr param_name = MP_QSTR_NULL;
2737 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002738 if (MP_PARSE_NODE_IS_ID(pn)) {
2739 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002740 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002741 // comes after a star, so counts as a keyword-only parameter
2742 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002743 } else {
Damien George2827d622014-04-27 15:50:52 +01002744 // comes before a star, so counts as a positional parameter
2745 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002746 }
Damienb14de212013-10-06 00:28:28 +01002747 } else {
Damiend99b0522013-12-21 18:17:45 +00002748 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2749 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2750 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2751 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002752 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002753 // comes after a star, so counts as a keyword-only parameter
2754 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002755 } else {
Damien George2827d622014-04-27 15:50:52 +01002756 // comes before a star, so counts as a positional parameter
2757 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002758 }
Damiend99b0522013-12-21 18:17:45 +00002759 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002760 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002761 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002762 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002763 // bare star
2764 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002765 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002766 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002767 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002768 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002769 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002770 } else {
2771 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002772 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002773 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002774 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2775 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002776 }
Damien George0bb97132015-02-27 14:25:47 +00002777 } else {
2778 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002779 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002780 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002781 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002782 }
Damien429d7192013-10-04 19:53:11 +01002783 }
2784
Damien George2827d622014-04-27 15:50:52 +01002785 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002786 bool added;
2787 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2788 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002789 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002790 return;
2791 }
Damien429d7192013-10-04 19:53:11 +01002792 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002793 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002794 }
2795}
2796
Damien George2827d622014-04-27 15:50:52 +01002797STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002798 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002799}
2800
Damien George2827d622014-04-27 15:50:52 +01002801STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002802 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2803}
2804
Damien Georgeea5b59b2015-09-04 16:44:14 +01002805#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002806STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2807 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2808 // no annotation
2809 return;
2810 }
2811
2812 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2813 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2814 // named parameter with possible annotation
2815 // fallthrough
2816 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2817 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2818 // named star with possible annotation
2819 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2820 // fallthrough
2821 } else {
2822 // no annotation
2823 return;
2824 }
2825 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
2826 // double star with possible annotation
2827 // fallthrough
2828 } else {
2829 // no annotation
2830 return;
2831 }
2832
2833 mp_parse_node_t pn_annotation = pns->nodes[1];
2834
2835 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002836 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2837 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2838 assert(id_info != NULL);
2839
Damien Georgeea5b59b2015-09-04 16:44:14 +01002840 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2841 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2842 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2843 } else {
2844 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002845 }
Damien George2ac4af62014-08-15 16:45:41 +01002846 }
Damien429d7192013-10-04 19:53:11 +01002847}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002848#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002849
Damien George6be0b0a2014-08-15 14:30:52 +01002850STATIC 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 +01002851 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002852 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002853 // no more nested if/for; compile inner expression
2854 compile_node(comp, pn_inner_expr);
2855 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002857 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002858 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002859 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002860 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002861 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002862 #endif
Damien429d7192013-10-04 19:53:11 +01002863 } else {
2864 EMIT(yield_value);
2865 EMIT(pop_top);
2866 }
Damiend99b0522013-12-21 18:17:45 +00002867 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002868 // if condition
Damiend99b0522013-12-21 18:17:45 +00002869 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002870 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2871 pn_iter = pns_comp_if->nodes[1];
2872 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002873 } else {
2874 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002875 // for loop
Damiend99b0522013-12-21 18:17:45 +00002876 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002877 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002878 uint l_end2 = comp_next_label(comp);
2879 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002880 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002881 EMIT_ARG(label_assign, l_top2);
2882 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002883 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2884 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 +00002885 EMIT_ARG(jump, l_top2);
2886 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002887 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002888 }
2889}
2890
Damien George1463c1f2014-04-25 23:52:57 +01002891STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002892#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002893 // see http://www.python.org/dev/peps/pep-0257/
2894
2895 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002896 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002897 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002898 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002899 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002900 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2901 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002902 for (int i = 0; i < num_nodes; i++) {
2903 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002904 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 +00002905 // not a newline, so this is the first statement; finish search
2906 break;
2907 }
2908 }
2909 // 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 +00002910 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002911 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002912 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002913 } else {
2914 return;
2915 }
2916
2917 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002918 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002919 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002920 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2921 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2922 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2923 // compile the doc string
2924 compile_node(comp, pns->nodes[0]);
2925 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002926 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002927 }
2928 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002929#else
2930 (void)comp;
2931 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002932#endif
Damien429d7192013-10-04 19:53:11 +01002933}
2934
Damien George1463c1f2014-04-25 23:52:57 +01002935STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002936 comp->pass = pass;
2937 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002938 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002939 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002940
Damien George36db6bc2014-05-07 17:24:22 +01002941 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002942 // reset maximum stack sizes in scope
2943 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002944 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002945 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002946 }
2947
Damien429d7192013-10-04 19:53:11 +01002948 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002949 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2950 assert(scope->kind == SCOPE_MODULE);
2951 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2952 compile_node(comp, pns->nodes[0]); // compile the expression
2953 EMIT(return_value);
2954 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002955 if (!comp->is_repl) {
2956 check_for_doc_string(comp, scope->pn);
2957 }
Damien429d7192013-10-04 19:53:11 +01002958 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002959 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002960 EMIT(return_value);
2961 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002962 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2963 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2964 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002965
2966 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002967 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002968 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002969 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002970 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002971 }
2972 #if MICROPY_EMIT_NATIVE
2973 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002974 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002975 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002976
Damien George2ac4af62014-08-15 16:45:41 +01002977 // argument annotations
2978 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2979
2980 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01002981 mp_parse_node_t pn_annotation = pns->nodes[2];
2982 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01002983 // nodes[2] can be null or a test-expr
2984 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2985 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2986 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2987 } else {
2988 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002989 }
2990 }
Damien George2ac4af62014-08-15 16:45:41 +01002991 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01002992 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002993
2994 compile_node(comp, pns->nodes[3]); // 3 is function body
2995 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002996 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002997 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002998 EMIT(return_value);
2999 }
3000 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003001 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3002 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3003 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003004
3005 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003006 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003007 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003008 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003009 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3010 }
3011
3012 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003013
3014 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3015 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3016 EMIT(pop_top);
3017 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3018 }
Damien429d7192013-10-04 19:53:11 +01003019 EMIT(return_value);
3020 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3021 // a bit of a hack at the moment
3022
Damiend99b0522013-12-21 18:17:45 +00003023 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3024 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3025 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3026 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3027 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003028
Damien George708c0732014-04-27 19:23:46 +01003029 // We need a unique name for the comprehension argument (the iterator).
3030 // CPython uses .0, but we should be able to use anything that won't
3031 // clash with a user defined variable. Best to use an existing qstr,
3032 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01003033 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01003034 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003035 bool added;
3036 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3037 assert(added);
3038 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003039 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003040 }
3041
3042 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003043 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003044 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003045 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003046 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003047 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003048 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003049 #endif
Damien429d7192013-10-04 19:53:11 +01003050 }
3051
Damien George6f355fd2014-04-10 14:11:31 +01003052 uint l_end = comp_next_label(comp);
3053 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00003054 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00003055 EMIT_ARG(label_assign, l_top);
3056 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003057 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3058 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003059 EMIT_ARG(jump, l_top);
3060 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003061 EMIT(for_iter_end);
3062
3063 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003064 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003065 }
3066 EMIT(return_value);
3067 } else {
3068 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003069 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3070 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3071 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003072
Damien George36db6bc2014-05-07 17:24:22 +01003073 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003074 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003075 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003076 assert(added);
3077 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003078 }
3079
Damien George542bd6b2015-03-26 14:42:40 +00003080 compile_load_id(comp, MP_QSTR___name__);
3081 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003082 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003083 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003084
3085 check_for_doc_string(comp, pns->nodes[2]);
3086 compile_node(comp, pns->nodes[2]); // 2 is class body
3087
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003088 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003089 assert(id != NULL);
3090 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003091 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003092 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003093 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003094 }
3095 EMIT(return_value);
3096 }
3097
Damien415eb6f2013-10-05 12:19:06 +01003098 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003099
3100 // make sure we match all the exception levels
3101 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003102}
3103
Damien George094d4502014-04-02 17:31:27 +01003104#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003105// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003106STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003107 comp->pass = pass;
3108 comp->scope_cur = scope;
3109 comp->next_label = 1;
3110
3111 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003112 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003113 return;
3114 }
3115
Damien George36db6bc2014-05-07 17:24:22 +01003116 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003117 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003118 }
3119
Damien826005c2013-10-05 23:17:28 +01003120 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003121 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3122 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3123 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003124
Damiend99b0522013-12-21 18:17:45 +00003125 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003126
Damiena2f2f7d2013-10-06 00:14:13 +01003127 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003128 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003129 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003130 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003131 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003132 if (comp->compile_error != MP_OBJ_NULL) {
3133 goto inline_asm_error;
3134 }
Damiena2f2f7d2013-10-06 00:14:13 +01003135 }
3136
Damiend99b0522013-12-21 18:17:45 +00003137 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003138
Damiend99b0522013-12-21 18:17:45 +00003139 mp_parse_node_t pn_body = pns->nodes[3]; // body
3140 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003141 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003142
Damien826005c2013-10-05 23:17:28 +01003143 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003144 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3145 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003146 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3147 // no instructions
3148 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003149 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003150 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003151 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003152 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003153 return;
3154 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003155
3156 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003157 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003158 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3159 goto not_an_instruction;
3160 }
Damiend99b0522013-12-21 18:17:45 +00003161 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003162 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3163 goto not_an_instruction;
3164 }
3165 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3166 goto not_an_instruction;
3167 }
3168 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3169 goto not_an_instruction;
3170 }
Damiend99b0522013-12-21 18:17:45 +00003171 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003172
3173 // parse node looks like an instruction
3174 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003175 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3176 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3177 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003178 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003179
3180 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003181 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003182 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003183 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003184 return;
3185 }
Damien George6f355fd2014-04-10 14:11:31 +01003186 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003187 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003188 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3189 compile_syntax_error(comp, nodes[i], "label redefined");
3190 return;
3191 }
Damien826005c2013-10-05 23:17:28 +01003192 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003193 } else if (op == MP_QSTR_align) {
3194 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003195 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003196 return;
3197 }
Damien George36db6bc2014-05-07 17:24:22 +01003198 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003199 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3200 }
3201 } else if (op == MP_QSTR_data) {
3202 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003203 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003204 return;
3205 }
Damien George36db6bc2014-05-07 17:24:22 +01003206 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003207 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003208 for (uint j = 1; j < n_args; j++) {
3209 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003210 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003211 return;
3212 }
Damien George50912e72015-01-20 11:55:10 +00003213 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003214 }
3215 }
Damien826005c2013-10-05 23:17:28 +01003216 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003217 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003218 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003219 }
3220 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003221
3222 if (comp->compile_error != MP_OBJ_NULL) {
3223 pns = pns2; // this is the parse node that had the error
3224 goto inline_asm_error;
3225 }
Damien826005c2013-10-05 23:17:28 +01003226 }
3227
Damien George36db6bc2014-05-07 17:24:22 +01003228 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003229 EMIT_INLINE_ASM(end_pass);
3230 }
3231
3232 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003233 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003234 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003235 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003236 }
Damien429d7192013-10-04 19:53:11 +01003237}
Damien George094d4502014-04-02 17:31:27 +01003238#endif
Damien429d7192013-10-04 19:53:11 +01003239
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003240STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003241 // in Micro Python we put the *x parameter after all other parameters (except **y)
3242 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3243 id_info_t *id_param = NULL;
3244 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3245 id_info_t *id = &scope->id_info[i];
3246 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3247 if (id_param != NULL) {
3248 // swap star param with last param
3249 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3250 }
3251 break;
3252 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3253 id_param = id;
3254 }
3255 }
3256 }
Damien George2827d622014-04-27 15:50:52 +01003257
Damien429d7192013-10-04 19:53:11 +01003258 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003259 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003260 scope->num_locals = 0;
3261 for (int i = 0; i < scope->id_info_len; i++) {
3262 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003263 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003264 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3265 continue;
3266 }
3267 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3268 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3269 }
Damien George2827d622014-04-27 15:50:52 +01003270 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003271 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003272 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003273 }
3274 }
3275
Damien George65dc9602015-08-14 12:24:11 +01003276 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003277 for (int i = 0; i < scope->id_info_len; i++) {
3278 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003279 // in Micro Python the cells come right after the fast locals
3280 // parameters are not counted here, since they remain at the start
3281 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003282 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003283 id->local_num = scope->num_locals;
3284 scope->num_locals += 1;
3285 }
Damien9ecbcff2013-12-11 00:41:43 +00003286 }
Damien9ecbcff2013-12-11 00:41:43 +00003287
Damien George65dc9602015-08-14 12:24:11 +01003288 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003289 // make sure they are in the order of the parent scope
3290 if (scope->parent != NULL) {
3291 int num_free = 0;
3292 for (int i = 0; i < scope->parent->id_info_len; i++) {
3293 id_info_t *id = &scope->parent->id_info[i];
3294 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3295 for (int j = 0; j < scope->id_info_len; j++) {
3296 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003297 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003298 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003299 // in Micro Python the frees come first, before the params
3300 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003301 num_free += 1;
3302 }
3303 }
3304 }
Damien429d7192013-10-04 19:53:11 +01003305 }
Damien George6baf76e2013-12-30 22:32:17 +00003306 // in Micro Python shift all other locals after the free locals
3307 if (num_free > 0) {
3308 for (int i = 0; i < scope->id_info_len; i++) {
3309 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003310 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003311 id->local_num += num_free;
3312 }
3313 }
Damien George2827d622014-04-27 15:50:52 +01003314 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 +00003315 scope->num_locals += num_free;
3316 }
Damien429d7192013-10-04 19:53:11 +01003317 }
Damien429d7192013-10-04 19:53:11 +01003318}
3319
Damien George58e0f4a2015-09-23 10:50:43 +01003320mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
Damien George9d5e5c02015-09-24 13:15:57 +01003321 // put compiler state on the stack, it's relatively small
3322 compiler_t comp_state = {0};
3323 compiler_t *comp = &comp_state;
3324
Damien Georgecbd2f742014-01-19 11:48:48 +00003325 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003326 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003327
Damien George62a3a282015-03-01 12:04:05 +00003328 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003329 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003330
3331 // optimise constants (scope must be set for error messages to work)
3332 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003333 mp_map_t consts;
3334 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003335 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003336 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003337
Damien Georgea210c772015-03-26 15:49:53 +00003338 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003339 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003340
Damien826005c2013-10-05 23:17:28 +01003341 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003342 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003343 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003344 comp->emit_method_table = &emit_bc_method_table;
3345 #endif
Damien826005c2013-10-05 23:17:28 +01003346 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003347 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003348 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003349#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003350 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003351 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003352#endif
Damien826005c2013-10-05 23:17:28 +01003353 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003354 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003355 }
3356
3357 // update maximim number of labels needed
3358 if (comp->next_label > max_num_labels) {
3359 max_num_labels = comp->next_label;
3360 }
Damien429d7192013-10-04 19:53:11 +01003361 }
3362
Damien826005c2013-10-05 23:17:28 +01003363 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003364 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 +00003365 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003366 }
3367
Damien Georgea210c772015-03-26 15:49:53 +00003368 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003369 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003370
Damien826005c2013-10-05 23:17:28 +01003371 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003372#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003373 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003374#endif
Damien3ef4abb2013-10-12 16:53:13 +01003375#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003376 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003377#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003378 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003379 if (false) {
3380 // dummy
3381
Damien3ef4abb2013-10-12 16:53:13 +01003382#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003383 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003384 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003385 if (emit_inline_thumb == NULL) {
3386 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3387 }
3388 comp->emit = NULL;
3389 comp->emit_method_table = NULL;
3390 comp->emit_inline_asm = emit_inline_thumb;
3391 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003392 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003393 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003394 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003395 }
Damienc025ebb2013-10-12 14:30:21 +01003396#endif
3397
Damien826005c2013-10-05 23:17:28 +01003398 } else {
Damienc025ebb2013-10-12 14:30:21 +01003399
3400 // choose the emit type
3401
Damien826005c2013-10-05 23:17:28 +01003402 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003403
3404#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003405 case MP_EMIT_OPT_NATIVE_PYTHON:
3406 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003407#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003408 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003409 emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003410 }
Damien13ed3a62013-10-08 09:05:10 +01003411 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003412#elif MICROPY_EMIT_X86
3413 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003414 emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels);
Damien Georgec90f59e2014-09-06 23:06:36 +01003415 }
3416 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003417#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003418 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003419 emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels);
Damienc025ebb2013-10-12 14:30:21 +01003420 }
3421 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003422#elif MICROPY_EMIT_ARM
3423 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003424 emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003425 }
3426 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003427#endif
3428 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003429 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003430 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003431#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003432
Damien826005c2013-10-05 23:17:28 +01003433 default:
Damien826005c2013-10-05 23:17:28 +01003434 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003435 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003436 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003437 #endif
Damien826005c2013-10-05 23:17:28 +01003438 break;
3439 }
Damienc025ebb2013-10-12 14:30:21 +01003440
Damien George1e1779e2015-01-14 00:20:28 +00003441 // need a pass to compute stack size
3442 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3443
Damien George36db6bc2014-05-07 17:24:22 +01003444 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003445 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003446 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3447 }
3448
3449 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003450 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003451 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003452 }
Damien6cdd3af2013-10-05 18:08:26 +01003453 }
Damien429d7192013-10-04 19:53:11 +01003454 }
3455
Damien Georgecfc4c332015-07-29 22:16:01 +00003456 if (comp->compile_error != MP_OBJ_NULL) {
3457 // if there is no line number for the error then use the line
3458 // number for the start of this scope
3459 compile_error_set_line(comp, comp->scope_cur->pn);
3460 // add a traceback to the exception using relevant source info
3461 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3462 comp->compile_error_line, comp->scope_cur->simple_name);
3463 }
3464
Damien George41d02b62014-01-24 22:42:28 +00003465 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003466
Damien Georgea210c772015-03-26 15:49:53 +00003467 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003468#if MICROPY_EMIT_NATIVE
3469 if (emit_native != NULL) {
3470#if MICROPY_EMIT_X64
3471 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003472#elif MICROPY_EMIT_X86
3473 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003474#elif MICROPY_EMIT_THUMB
3475 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003476#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003477 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003478#endif
3479 }
3480#endif
3481#if MICROPY_EMIT_INLINE_THUMB
3482 if (emit_inline_thumb != NULL) {
3483 emit_inline_thumb_free(emit_inline_thumb);
3484 }
3485#endif
Damien George41d02b62014-01-24 22:42:28 +00003486
Damien George52b5d762014-09-23 15:31:56 +00003487 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003488 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003489
Damien George41d02b62014-01-24 22:42:28 +00003490 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003491 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003492 for (scope_t *s = module_scope; s;) {
3493 scope_t *next = s->next;
3494 scope_free(s);
3495 s = next;
3496 }
Damien5ac1b2e2013-10-18 19:58:12 +01003497
Damien George9d5e5c02015-09-24 13:15:57 +01003498 if (comp->compile_error != MP_OBJ_NULL) {
3499 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003500 } else {
Damien George1fb03172014-01-03 14:22:03 +00003501 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003502 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003503 }
Damien429d7192013-10-04 19:53:11 +01003504}