blob: 82a27bb0b8ec07fdcdbca8644d7b920677952d7a [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
36#include "py/smallint.h"
37#include "py/runtime.h"
38#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010039
40// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien 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 Georgeecf5b772014-04-04 11:13:51 +0000296 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damien George0bb97132015-02-27 14:25:47 +0000297 } else {
298 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300299 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100300 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000301 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300302 }
Damien429d7192013-10-04 19:53:11 +0100303 }
304 }
305 break;
306
307 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000308 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100309 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000310 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100311 // +int
Damiend99b0522013-12-21 18:17:45 +0000312 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
313 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100314 // -int
Damiend99b0522013-12-21 18:17:45 +0000315 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
Damien George0bb97132015-02-27 14:25:47 +0000316 } else {
317 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100318 // ~int
Damiend99b0522013-12-21 18:17:45 +0000319 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100320 }
321 }
322 break;
323
324 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100325 if (0) {
Damien Georgeddd1e182015-01-10 14:07:24 +0000326#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100327 } 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])) {
328 // id.id
329 // look it up in constant table, see if it can be replaced with an integer
Damien George4dea9222015-04-09 15:29:54 +0000330 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George57e99eb2014-04-10 22:42:11 +0100331 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
332 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
333 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
334 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
335 if (elem != NULL) {
336 mp_obj_t dest[2];
337 mp_load_method_maybe(elem->value, q_attr, dest);
338 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100339 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000340 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100341 }
342 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000343#endif
Damien429d7192013-10-04 19:53:11 +0100344 }
345 break;
346 }
347 }
348
349 return pn;
350}
351
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200352STATIC 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 +0100353STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000354STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100355
Damien George6f355fd2014-04-10 14:11:31 +0100356STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100357 return comp->next_label++;
358}
359
Damien George8dcc0c72014-03-27 10:55:21 +0000360STATIC void compile_increase_except_level(compiler_t *comp) {
361 comp->cur_except_level += 1;
362 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
363 comp->scope_cur->exc_stack_size = comp->cur_except_level;
364 }
365}
366
367STATIC void compile_decrease_except_level(compiler_t *comp) {
368 assert(comp->cur_except_level > 0);
369 comp->cur_except_level -= 1;
370}
371
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200372STATIC 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 +0100373 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100374 scope->parent = comp->scope_cur;
375 scope->next = NULL;
376 if (comp->scope_head == NULL) {
377 comp->scope_head = scope;
378 } else {
379 scope_t *s = comp->scope_head;
380 while (s->next != NULL) {
381 s = s->next;
382 }
383 s->next = scope;
384 }
385 return scope;
386}
387
Damien George91bc32d2015-04-09 15:31:53 +0000388typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
389
390STATIC 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 +0000391 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000392 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
393 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100394 for (int i = 0; i < num_nodes; i++) {
395 f(comp, pns->nodes[i]);
396 }
Damiend99b0522013-12-21 18:17:45 +0000397 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100398 f(comp, pn);
399 }
400}
401
Damien George969a6b32014-12-10 22:07:04 +0000402STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000403 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100404 for (int i = 0; i < num_nodes; i++) {
405 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000406 if (comp->compile_error != MP_OBJ_NULL) {
407 // add line info for the error in case it didn't have a line number
408 compile_error_set_line(comp, pns->nodes[i]);
409 return;
410 }
Damien429d7192013-10-04 19:53:11 +0100411 }
412}
413
Damien George542bd6b2015-03-26 14:42:40 +0000414STATIC void compile_load_id(compiler_t *comp, qstr qst) {
415 if (comp->pass == MP_PASS_SCOPE) {
416 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
417 } else {
Damien George41125902015-03-26 16:44:14 +0000418 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000419 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000420 #else
421 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
422 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000423 }
424}
425
426STATIC void compile_store_id(compiler_t *comp, qstr qst) {
427 if (comp->pass == MP_PASS_SCOPE) {
428 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
429 } else {
Damien George41125902015-03-26 16:44:14 +0000430 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000431 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000432 #else
433 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
434 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000435 }
436}
437
438STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
439 if (comp->pass == MP_PASS_SCOPE) {
440 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
441 } else {
Damien George41125902015-03-26 16:44:14 +0000442 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000443 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000444 #else
445 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
446 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000447 }
448}
449
Damien George2c0842b2014-04-27 16:46:51 +0100450STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100451 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000452 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100453 compile_node(comp, pn);
454 total += 1;
455 }
456 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000457 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100458 for (int i = 0; i < n; i++) {
459 compile_node(comp, pns_list->nodes[i]);
460 }
461 total += n;
462 }
Damien Georgeb9791222014-01-23 00:34:21 +0000463 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100464}
Damien429d7192013-10-04 19:53:11 +0100465
Damien George969a6b32014-12-10 22:07:04 +0000466STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100467 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000468 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100469}
470
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200471STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000472 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
473 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100474}
475
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200476STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000477 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
478 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100479}
480
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200481STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3a205172013-10-12 15:01:56 +0100482 if (node_is_const_false(pn)) {
483 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000484 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100485 }
486 return;
487 } else if (node_is_const_true(pn)) {
488 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000489 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100490 }
491 return;
Damiend99b0522013-12-21 18:17:45 +0000492 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
493 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
494 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
495 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100496 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000497 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100498 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100499 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000500 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100501 }
Damien George0b2fd912015-02-28 14:37:54 +0000502 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000503 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100504 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000505 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100506 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000507 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100508 }
509 }
510 return;
Damiend99b0522013-12-21 18:17:45 +0000511 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100512 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000513 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100514 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000515 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100516 }
Damiend99b0522013-12-21 18:17:45 +0000517 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100518 c_if_cond(comp, pns->nodes[0], !jump_if, label);
519 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100520 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
521 // cond is something in parenthesis
522 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
523 // empty tuple, acts as false for the condition
524 if (jump_if == false) {
525 EMIT_ARG(jump, label);
526 }
527 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
528 // non-empty tuple, acts as true for the condition
529 if (jump_if == true) {
530 EMIT_ARG(jump, label);
531 }
532 } else {
533 // parenthesis around 1 item, is just that item
534 c_if_cond(comp, pns->nodes[0], jump_if, label);
535 }
536 return;
Damien3a205172013-10-12 15:01:56 +0100537 }
538 }
539
540 // nothing special, fall back to default compiling for node and jump
541 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000542 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100543}
544
545typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100546STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100547
Damien George6be0b0a2014-08-15 14:30:52 +0100548STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100549 if (assign_kind != ASSIGN_AUG_STORE) {
550 compile_node(comp, pns->nodes[0]);
551 }
552
Damiend99b0522013-12-21 18:17:45 +0000553 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
554 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
555 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
556 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100557 if (assign_kind != ASSIGN_AUG_STORE) {
558 for (int i = 0; i < n - 1; i++) {
559 compile_node(comp, pns1->nodes[i]);
560 }
561 }
Damiend99b0522013-12-21 18:17:45 +0000562 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
563 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100564 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000565 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100566 if (assign_kind == ASSIGN_AUG_STORE) {
567 EMIT(rot_three);
568 EMIT(store_subscr);
569 } else {
570 compile_node(comp, pns1->nodes[0]);
571 if (assign_kind == ASSIGN_AUG_LOAD) {
572 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100573 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100574 } else {
575 EMIT(store_subscr);
576 }
577 }
Damiend99b0522013-12-21 18:17:45 +0000578 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
579 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100580 if (assign_kind == ASSIGN_AUG_LOAD) {
581 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000582 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100583 } else {
584 if (assign_kind == ASSIGN_AUG_STORE) {
585 EMIT(rot_two);
586 }
Damien Georgeb9791222014-01-23 00:34:21 +0000587 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100588 }
589 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100590 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100591 }
592 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100593 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100594 }
595
Damiend99b0522013-12-21 18:17:45 +0000596 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100597 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100598 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100599
600 return;
601
602cannot_assign:
603 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100604}
605
Damien George0288cf02014-04-11 11:53:00 +0000606// 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 +0100607STATIC 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 +0000608 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
609
610 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000611 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000612 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
613 EMIT_ARG(unpack_ex, 0, num_tail);
614 have_star_index = 0;
615 }
Damien George963a5a32015-01-16 17:47:07 +0000616 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000617 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000618 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000619 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
620 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100621 } else {
Damien George9d181f62014-04-27 16:55:27 +0100622 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100623 return;
624 }
625 }
626 }
Damien George963a5a32015-01-16 17:47:07 +0000627 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000628 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100629 }
Damien George0288cf02014-04-11 11:53:00 +0000630 if (num_head != 0) {
631 if (0 == have_star_index) {
632 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100633 } else {
Damien George0288cf02014-04-11 11:53:00 +0000634 c_assign(comp, node_head, ASSIGN_STORE);
635 }
636 }
Damien George963a5a32015-01-16 17:47:07 +0000637 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000638 if (num_head + i == have_star_index) {
639 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
640 } else {
641 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100642 }
643 }
644}
645
646// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100647STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100648 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000649 assert(!MP_PARSE_NODE_IS_NULL(pn));
650 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000651 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000652 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100653 switch (assign_kind) {
654 case ASSIGN_STORE:
655 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000656 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100657 break;
658 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000659 default:
Damien George542bd6b2015-03-26 14:42:40 +0000660 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100661 break;
662 }
663 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100664 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100665 return;
666 }
667 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000668 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000669 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
670 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100671 case PN_power:
672 // lhs is an index or attribute
673 c_assign_power(comp, pns, assign_kind);
674 break;
675
676 case PN_testlist_star_expr:
677 case PN_exprlist:
678 // lhs is a tuple
679 if (assign_kind != ASSIGN_STORE) {
680 goto bad_aug;
681 }
Damien George0288cf02014-04-11 11:53:00 +0000682 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100683 break;
684
685 case PN_atom_paren:
686 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000687 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100688 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100689 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000690 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000691 if (assign_kind != ASSIGN_STORE) {
692 goto bad_aug;
693 }
Damiend99b0522013-12-21 18:17:45 +0000694 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100695 goto testlist_comp;
696 } else {
697 // parenthesis around 1 item, is just that item
698 pn = pns->nodes[0];
699 goto tail_recursion;
700 }
701 break;
702
703 case PN_atom_bracket:
704 // lhs is something in brackets
705 if (assign_kind != ASSIGN_STORE) {
706 goto bad_aug;
707 }
Damiend99b0522013-12-21 18:17:45 +0000708 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100709 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000710 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000711 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
712 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100713 goto testlist_comp;
714 } else {
715 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000716 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100717 }
718 break;
719
720 default:
Damien George9d181f62014-04-27 16:55:27 +0100721 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100722 }
723 return;
724
725 testlist_comp:
726 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000727 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
728 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
729 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100730 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000731 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000732 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000733 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100734 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000735 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
736 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000737 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100738 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100739 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100740 } else {
741 // sequence with 2 items
742 goto sequence_with_2_items;
743 }
744 } else {
745 // sequence with 2 items
746 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000747 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100748 }
749 return;
750 }
751 return;
752
Damien George9d181f62014-04-27 16:55:27 +0100753 cannot_assign:
754 compile_syntax_error(comp, pn, "can't assign to expression");
755 return;
756
Damien429d7192013-10-04 19:53:11 +0100757 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100758 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100759}
760
Damien George65dc9602015-08-14 12:24:11 +0100761// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100762// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
763// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
764// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100765STATIC 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 +0100766 assert(n_pos_defaults >= 0);
767 assert(n_kw_defaults >= 0);
768
Damien429d7192013-10-04 19:53:11 +0100769 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000770 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100771 int nfree = 0;
772 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000773 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
774 id_info_t *id = &comp->scope_cur->id_info[i];
775 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
776 for (int j = 0; j < this_scope->id_info_len; j++) {
777 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100778 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000779 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000780 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000781 nfree += 1;
782 }
783 }
Damien429d7192013-10-04 19:53:11 +0100784 }
785 }
786 }
Damien429d7192013-10-04 19:53:11 +0100787
788 // make the function/closure
789 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100790 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100791 } else {
Damien George3558f622014-04-20 17:50:40 +0100792 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100793 }
794}
795
Damien George6be0b0a2014-08-15 14:30:52 +0100796STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000797 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100798 comp->have_star = true;
799 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000800 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
801 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100802 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100803 } else {
804 // named star
Damien429d7192013-10-04 19:53:11 +0100805 }
Damien George8b19db02014-04-11 23:25:34 +0100806 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000807
808 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100809 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000810 // TODO do we need to do anything with this?
811
812 } else {
813 mp_parse_node_t pn_id;
814 mp_parse_node_t pn_colon;
815 mp_parse_node_t pn_equal;
816 if (MP_PARSE_NODE_IS_ID(pn)) {
817 // this parameter is just an id
818
819 pn_id = pn;
820 pn_colon = MP_PARSE_NODE_NULL;
821 pn_equal = MP_PARSE_NODE_NULL;
822
Damien George0bb97132015-02-27 14:25:47 +0000823 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000824 // this parameter has a colon and/or equal specifier
825
Damien George0bb97132015-02-27 14:25:47 +0000826 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
827
Damien Georgef41fdd02014-03-03 23:19:11 +0000828 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
829 pn_id = pns->nodes[0];
830 pn_colon = pns->nodes[1];
831 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +0000832 }
833
834 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
835 // this parameter does not have a default value
836
837 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100838 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100839 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000840 return;
841 }
842
843 } else {
844 // this parameter has a default value
845 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
846
Damien George8b19db02014-04-11 23:25:34 +0100847 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000848 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000849 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
850 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100851 // in Micro Python we put the default positional parameters into a tuple using the bytecode
852 // we need to do this here before we start building the map for the default keywords
853 if (comp->num_default_params > 0) {
854 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100855 } else {
856 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100857 }
Damien George69b89d22014-04-11 13:38:30 +0000858 // first default dict param, so make the map
859 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000860 }
Damien Georgef0778a72014-06-07 22:01:00 +0100861
862 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000863 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000864 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000865 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000866 } else {
Damien George69b89d22014-04-11 13:38:30 +0000867 comp->num_default_params += 1;
868 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000869 }
870 }
871
872 // TODO pn_colon not implemented
873 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100874 }
875}
876
877// leaves function object on stack
878// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000879STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100880 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100881 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000882 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100883 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000884 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100885 }
886
887 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +0100888 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +0000889 uint old_num_dict_params = comp->num_dict_params;
890 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +0100891
892 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +0100893 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +0000894 comp->num_dict_params = 0;
895 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +0100896 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000897
Damien Georgea91ac202014-10-05 19:01:34 +0100898 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000899 return MP_QSTR_NULL;
900 }
901
Damien Georgee337f1e2014-03-31 15:18:37 +0100902 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +0100903 // the default keywords args may have already made the tuple; if not, do it now
904 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +0000905 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100906 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +0100907 }
Damien Georgee337f1e2014-03-31 15:18:37 +0100908
Damien429d7192013-10-04 19:53:11 +0100909 // get the scope for this function
910 scope_t *fscope = (scope_t*)pns->nodes[4];
911
912 // make the function
Damien George69b89d22014-04-11 13:38:30 +0000913 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100914
915 // restore variables
Damien George8b19db02014-04-11 23:25:34 +0100916 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +0000917 comp->num_dict_params = old_num_dict_params;
918 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +0100919
920 // return its name (the 'f' in "def f(...):")
921 return fscope->simple_name;
922}
923
924// leaves class object on stack
925// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000926STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100927 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100928 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000929 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100930 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000931 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100932 }
933
934 EMIT(load_build_class);
935
936 // scope for this class
937 scope_t *cscope = (scope_t*)pns->nodes[3];
938
939 // compile the class
940 close_over_variables_etc(comp, cscope, 0, 0);
941
942 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000943 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100944
945 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100946 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
947 mp_parse_node_t parents = pns->nodes[1];
948 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
949 parents = MP_PARSE_NODE_NULL;
950 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000951 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100952 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100953
954 // return its name (the 'C' in class C(...):")
955 return cscope->simple_name;
956}
957
Damien6cdd3af2013-10-05 18:08:26 +0100958// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200959STATIC 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 +0000960 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100961 return false;
962 }
963
964 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100965 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100966 return true;
967 }
968
Damiend99b0522013-12-21 18:17:45 +0000969 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100970 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100971 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100972#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000973 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100974 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000975 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100976 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100977#endif
Damien3ef4abb2013-10-12 16:53:13 +0100978#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000979 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +0100980 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100981#endif
Damien6cdd3af2013-10-05 18:08:26 +0100982 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100983 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100984 }
985
986 return true;
987}
988
Damien George969a6b32014-12-10 22:07:04 +0000989STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100990 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000991 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000992 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100993
Damien6cdd3af2013-10-05 18:08:26 +0100994 // inherit emit options for this function/class definition
995 uint emit_options = comp->scope_cur->emit_options;
996
997 // compile each decorator
998 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100999 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001000 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1001 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001002
1003 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001004 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001005 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001006
1007 // check for built-in decorators
1008 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1009 // this was a built-in
1010 num_built_in_decorators += 1;
1011
1012 } else {
1013 // not a built-in, compile normally
1014
1015 // compile the decorator function
1016 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001017 for (int j = 1; j < name_len; j++) {
1018 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1019 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001020 }
1021
1022 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001023 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001024 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001025 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001026 compile_node(comp, pns_decorator->nodes[1]);
1027 }
Damien429d7192013-10-04 19:53:11 +01001028 }
1029 }
1030
1031 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001032 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001033 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001034 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001035 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001036 } else {
Damien George0bb97132015-02-27 14:25:47 +00001037 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1038 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001039 }
1040
1041 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001042 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001043 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001044 }
1045
1046 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +00001047 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +01001048}
1049
Damien George969a6b32014-12-10 22:07:04 +00001050STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001051 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001052 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +00001053 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +01001054}
1055
Damien George6be0b0a2014-08-15 14:30:52 +01001056STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001057 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00001058 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001059 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1060 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001061
1062 compile_node(comp, pns->nodes[0]); // base of the power node
1063
Damiend99b0522013-12-21 18:17:45 +00001064 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1065 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1066 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1067 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001068 for (int i = 0; i < n - 1; i++) {
1069 compile_node(comp, pns1->nodes[i]);
1070 }
Damiend99b0522013-12-21 18:17:45 +00001071 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1072 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001073 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001074 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001075 compile_node(comp, pns1->nodes[0]);
1076 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001077 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1078 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001079 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001080 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001081 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001082 }
1083 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001084 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001085 }
1086
Damiend99b0522013-12-21 18:17:45 +00001087 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001088 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001089 }
Damiend99b0522013-12-21 18:17:45 +00001090 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1091 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1092 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1093 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001094 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1095
Damiend99b0522013-12-21 18:17:45 +00001096 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1097 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1098 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001099 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001100 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001101 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001102 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001103 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001104 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001105 c_del_stmt(comp, pns->nodes[0]);
1106 for (int i = 0; i < n; i++) {
1107 c_del_stmt(comp, pns1->nodes[i]);
1108 }
Damiend99b0522013-12-21 18:17:45 +00001109 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001110 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001111 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001112 } else {
1113 // sequence with 2 items
1114 goto sequence_with_2_items;
1115 }
1116 } else {
1117 // sequence with 2 items
1118 sequence_with_2_items:
1119 c_del_stmt(comp, pns->nodes[0]);
1120 c_del_stmt(comp, pns->nodes[1]);
1121 }
1122 } else {
1123 // tuple with 1 element
1124 c_del_stmt(comp, pn);
1125 }
1126 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001127 // TODO is there anything else to implement?
1128 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001129 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001130
1131 return;
1132
1133cannot_delete:
1134 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001135}
1136
Damien George969a6b32014-12-10 22:07:04 +00001137STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001138 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1139}
1140
Damien George969a6b32014-12-10 22:07:04 +00001141STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001142 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001143 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001144 }
Damien George090c9232014-10-17 14:08:49 +00001145 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001146 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001147}
1148
Damien George969a6b32014-12-10 22:07:04 +00001149STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001150 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001151 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001152 }
Damien George090c9232014-10-17 14:08:49 +00001153 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001154 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001155}
1156
Damien George969a6b32014-12-10 22:07:04 +00001157STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001158 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001159 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001160 return;
1161 }
Damiend99b0522013-12-21 18:17:45 +00001162 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001163 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001164 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001165 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001166 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001167 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1168 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 +01001169
Damien George6f355fd2014-04-10 14:11:31 +01001170 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001171 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1172 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1173 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001174 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001175 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1176 } else {
1177 compile_node(comp, pns->nodes[0]);
1178 }
1179 EMIT(return_value);
1180}
1181
Damien George969a6b32014-12-10 22:07:04 +00001182STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001183 compile_node(comp, pns->nodes[0]);
1184 EMIT(pop_top);
1185}
1186
Damien George969a6b32014-12-10 22:07:04 +00001187STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001188 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001189 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001190 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001191 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001192 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001193 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001194 compile_node(comp, pns->nodes[0]);
1195 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001196 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001197 } else {
1198 // raise x
1199 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001200 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001201 }
1202}
1203
Damien George635543c2014-04-10 12:56:52 +01001204// q_base holds the base of the name
1205// eg a -> q_base=a
1206// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001207STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001208 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001209 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1210 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001211 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001212 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001213 pn = pns->nodes[0];
1214 is_as = true;
1215 }
Damien George635543c2014-04-10 12:56:52 +01001216 if (MP_PARSE_NODE_IS_NULL(pn)) {
1217 // empty name (eg, from . import x)
1218 *q_base = MP_QSTR_;
1219 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1220 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001221 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001222 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001223 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001224 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001225 }
Damien George635543c2014-04-10 12:56:52 +01001226 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001227 } else {
1228 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001229 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001230 {
Damien429d7192013-10-04 19:53:11 +01001231 // a name of the form a.b.c
1232 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001233 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001234 }
Damiend99b0522013-12-21 18:17:45 +00001235 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001236 int len = n - 1;
1237 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001238 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001239 }
Damien George55baff42014-01-21 21:40:13 +00001240 byte *q_ptr;
1241 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001242 for (int i = 0; i < n; i++) {
1243 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001244 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001245 }
Damien George39dc1452014-10-03 19:52:22 +01001246 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001247 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001248 memcpy(str_dest, str_src, str_src_len);
1249 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001250 }
Damien George635543c2014-04-10 12:56:52 +01001251 qstr q_full = qstr_build_end(q_ptr);
1252 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001253 if (is_as) {
1254 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001255 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001256 }
1257 }
Damien429d7192013-10-04 19:53:11 +01001258 }
Damien429d7192013-10-04 19:53:11 +01001259 }
1260}
1261
Damien George6be0b0a2014-08-15 14:30:52 +01001262STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001263 EMIT_ARG(load_const_small_int, 0); // level 0 import
1264 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1265 qstr q_base;
1266 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001267 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001268}
1269
Damien George969a6b32014-12-10 22:07:04 +00001270STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001271 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1272}
1273
Damien George969a6b32014-12-10 22:07:04 +00001274STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001275 mp_parse_node_t pn_import_source = pns->nodes[0];
1276
1277 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1278 uint import_level = 0;
1279 do {
1280 mp_parse_node_t pn_rel;
1281 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)) {
1282 // This covers relative imports with dots only like "from .. import"
1283 pn_rel = pn_import_source;
1284 pn_import_source = MP_PARSE_NODE_NULL;
1285 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1286 // This covers relative imports starting with dot(s) like "from .foo import"
1287 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1288 pn_rel = pns_2b->nodes[0];
1289 pn_import_source = pns_2b->nodes[1];
1290 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1291 } else {
1292 // Not a relative import
1293 break;
1294 }
1295
1296 // get the list of . and/or ...'s
1297 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001298 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001299
1300 // count the total number of .'s
1301 for (int i = 0; i < n; i++) {
1302 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1303 import_level++;
1304 } else {
1305 // should be an MP_TOKEN_ELLIPSIS
1306 import_level += 3;
1307 }
1308 }
1309 } while (0);
1310
Damiend99b0522013-12-21 18:17:45 +00001311 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001312 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001313
1314 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001315 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001316 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001317
1318 // do the import
Damien George635543c2014-04-10 12:56:52 +01001319 qstr dummy_q;
1320 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001321 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001322
Damien429d7192013-10-04 19:53:11 +01001323 } else {
Damien George635543c2014-04-10 12:56:52 +01001324 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001325
1326 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001327 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001328 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001329 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001330 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1331 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1332 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001333 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001334 }
Damien Georgeb9791222014-01-23 00:34:21 +00001335 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001336
1337 // do the import
Damien George635543c2014-04-10 12:56:52 +01001338 qstr dummy_q;
1339 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001340 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001341 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1342 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1343 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001344 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001345 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001346 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001347 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001348 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001349 }
1350 }
1351 EMIT(pop_top);
1352 }
1353}
1354
Damien George584ba672014-12-21 17:26:45 +00001355STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1356 bool added;
1357 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001358 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1359 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001360 return;
1361 }
1362 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1363
1364 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1365 id_info = scope_find_global(comp->scope_cur, qst);
1366 if (id_info != NULL) {
1367 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1368 }
1369}
1370
Damien George969a6b32014-12-10 22:07:04 +00001371STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001372 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001373 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001374 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001375 for (int i = 0; i < n; i++) {
1376 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001377 }
1378 }
1379}
1380
Damien George584ba672014-12-21 17:26:45 +00001381STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1382 bool added;
1383 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001384 if (!added && id_info->kind != ID_INFO_KIND_FREE) {
1385 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001386 return;
1387 }
1388 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1389 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)) {
1390 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1391 return;
1392 }
1393 id_info->kind = ID_INFO_KIND_FREE;
1394 scope_close_over_in_parents(comp->scope_cur, qst);
1395}
1396
Damien George969a6b32014-12-10 22:07:04 +00001397STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001398 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001399 if (comp->scope_cur->kind == SCOPE_MODULE) {
1400 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1401 return;
1402 }
1403 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001404 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001405 for (int i = 0; i < n; i++) {
1406 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001407 }
1408 }
1409}
1410
Damien George969a6b32014-12-10 22:07:04 +00001411STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001412 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001413 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001414 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001415 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001416 // assertion message
1417 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001418 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001419 }
Damien Georgeb9791222014-01-23 00:34:21 +00001420 EMIT_ARG(raise_varargs, 1);
1421 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001422}
1423
Damien George969a6b32014-12-10 22:07:04 +00001424STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001425 // TODO proper and/or short circuiting
1426
Damien George6f355fd2014-04-10 14:11:31 +01001427 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001428
Damien George65dc9602015-08-14 12:24:11 +01001429 // optimisation: don't emit anything when "if False"
1430 if (!node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001431 uint l_fail = comp_next_label(comp);
1432 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001433
Damien George391db862014-10-17 17:57:33 +00001434 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001435
Damien George65dc9602015-08-14 12:24:11 +01001436 // optimisation: skip everything else when "if True"
1437 if (node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001438 goto done;
1439 }
1440
1441 if (
Damien George65dc9602015-08-14 12:24:11 +01001442 // optimisation: don't jump over non-existent elif/else blocks
1443 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001444 // optimisation: don't jump if last instruction was return
1445 && !EMIT(last_emit_was_return_value)
1446 ) {
1447 // jump over elif/else blocks
1448 EMIT_ARG(jump, l_end);
1449 }
1450
1451 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001452 }
1453
Damien George235f9b32014-10-17 17:30:16 +00001454 // compile elif blocks (if any)
1455 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001456 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 +00001457 for (int i = 0; i < n_elif; i++) {
1458 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1459 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001460
Damien George65dc9602015-08-14 12:24:11 +01001461 // optimisation: don't emit anything when "if False"
1462 if (!node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001463 uint l_fail = comp_next_label(comp);
1464 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001465
Damien George391db862014-10-17 17:57:33 +00001466 compile_node(comp, pns_elif->nodes[1]); // elif block
1467
Damien George65dc9602015-08-14 12:24:11 +01001468 // optimisation: skip everything else when "elif True"
1469 if (node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001470 goto done;
1471 }
1472
1473 // optimisation: don't jump if last instruction was return
1474 if (!EMIT(last_emit_was_return_value)) {
1475 EMIT_ARG(jump, l_end);
1476 }
1477 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001478 }
1479 }
1480
1481 // compile else block
1482 compile_node(comp, pns->nodes[3]); // can be null
1483
Damien George391db862014-10-17 17:57:33 +00001484done:
Damien Georgeb9791222014-01-23 00:34:21 +00001485 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001486}
1487
Damien Georgecbddb272014-02-01 20:08:18 +00001488#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001489 uint16_t old_break_label = comp->break_label; \
1490 uint16_t old_continue_label = comp->continue_label; \
1491 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001492 uint break_label = comp_next_label(comp); \
1493 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001494 comp->break_label = break_label; \
1495 comp->continue_label = continue_label; \
1496 comp->break_continue_except_level = comp->cur_except_level;
1497
1498#define END_BREAK_CONTINUE_BLOCK \
1499 comp->break_label = old_break_label; \
1500 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001501 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001502
Damien George969a6b32014-12-10 22:07:04 +00001503STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001504 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001505
Damien George391db862014-10-17 17:57:33 +00001506 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1507 uint top_label = comp_next_label(comp);
1508 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1509 EMIT_ARG(jump, continue_label);
1510 }
1511 EMIT_ARG(label_assign, top_label);
1512 compile_node(comp, pns->nodes[1]); // body
1513 EMIT_ARG(label_assign, continue_label);
1514 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1515 }
Damience89a212013-10-15 22:25:17 +01001516
1517 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001518 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001519
1520 compile_node(comp, pns->nodes[2]); // else
1521
Damien Georgeb9791222014-01-23 00:34:21 +00001522 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001523}
1524
Damien Georgee181c0d2014-12-12 17:19:56 +00001525// This function compiles an optimised for-loop of the form:
1526// for <var> in range(<start>, <end>, <step>):
1527// <body>
1528// else:
1529// <else>
1530// <var> must be an identifier and <step> must be a small-int.
1531//
1532// Semantics of for-loop require:
1533// - final failing value should not be stored in the loop variable
1534// - if the loop never runs, the loop variable should never be assigned
1535// - assignments to <var>, <end> or <step> in the body do not alter the loop
1536// (<step> is a constant for us, so no need to worry about it changing)
1537//
1538// If <end> is a small-int, then the stack during the for-loop contains just
1539// the current value of <var>. Otherwise, the stack contains <end> then the
1540// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001541STATIC 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 +00001542 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001543
Damien George6f355fd2014-04-10 14:11:31 +01001544 uint top_label = comp_next_label(comp);
1545 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001546
Damien Georgee181c0d2014-12-12 17:19:56 +00001547 // put the end value on the stack if it's not a small-int constant
1548 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1549 if (end_on_stack) {
1550 compile_node(comp, pn_end);
1551 }
1552
1553 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001554 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001555
Damien Georgeb9791222014-01-23 00:34:21 +00001556 EMIT_ARG(jump, entry_label);
1557 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001558
Damien Georgec33ce602014-12-11 17:35:23 +00001559 // duplicate next value and store it to var
1560 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001561 c_assign(comp, pn_var, ASSIGN_STORE);
1562
Damienf3822fc2013-11-09 20:12:03 +00001563 // compile body
1564 compile_node(comp, pn_body);
1565
Damien Georgeb9791222014-01-23 00:34:21 +00001566 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001567
Damien Georgee181c0d2014-12-12 17:19:56 +00001568 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001569 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001570 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001571
Damien Georgeb9791222014-01-23 00:34:21 +00001572 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001573
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001574 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001575 if (end_on_stack) {
1576 EMIT(dup_top_two);
1577 EMIT(rot_two);
1578 } else {
1579 EMIT(dup_top);
1580 compile_node(comp, pn_end);
1581 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001582 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1583 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001584 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001585 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001586 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001587 }
Damien George63f38322015-02-28 15:04:06 +00001588 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001589
1590 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001591 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001592
1593 compile_node(comp, pn_else);
1594
Damien Georgeb9791222014-01-23 00:34:21 +00001595 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001596
1597 // discard final value of var that failed the loop condition
1598 EMIT(pop_top);
1599
1600 // discard <end> value if it's on the stack
1601 if (end_on_stack) {
1602 EMIT(pop_top);
1603 }
Damienf72fd0e2013-11-06 20:20:49 +00001604}
1605
Damien George969a6b32014-12-10 22:07:04 +00001606STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001607 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1608 // this is actually slower, but uses no heap memory
1609 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001610 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 +00001611 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001612 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1613 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1614 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1615 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001616 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1617 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001618 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001619 mp_parse_node_t pn_range_start;
1620 mp_parse_node_t pn_range_end;
1621 mp_parse_node_t pn_range_step;
1622 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001623 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001624 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001625 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001626 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001627 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001628 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001629 } else if (n_args == 2) {
1630 pn_range_start = args[0];
1631 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001632 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001633 } else {
1634 pn_range_start = args[0];
1635 pn_range_end = args[1];
1636 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001637 // We need to know sign of step. This is possible only if it's constant
1638 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1639 optimize = false;
1640 }
Damienf72fd0e2013-11-06 20:20:49 +00001641 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001642 }
1643 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001644 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1645 return;
1646 }
1647 }
1648 }
Damienf72fd0e2013-11-06 20:20:49 +00001649
Damien Georgecbddb272014-02-01 20:08:18 +00001650 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001651 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001652
Damien George6f355fd2014-04-10 14:11:31 +01001653 uint pop_label = comp_next_label(comp);
1654 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001655
Damien429d7192013-10-04 19:53:11 +01001656 compile_node(comp, pns->nodes[1]); // iterator
1657 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001658 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001659 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001660 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1661 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001662 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001663 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001664 }
Damien Georgeb9791222014-01-23 00:34:21 +00001665 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001666 EMIT(for_iter_end);
1667
1668 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001669 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001670
Damien429d7192013-10-04 19:53:11 +01001671 compile_node(comp, pns->nodes[3]); // else (not tested)
1672
Damien Georgeb9791222014-01-23 00:34:21 +00001673 EMIT_ARG(label_assign, break_label);
1674 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001675}
1676
Damien George6be0b0a2014-08-15 14:30:52 +01001677STATIC 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 +01001678 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001679 uint l1 = comp_next_label(comp);
1680 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001681
Damien Georgeb9791222014-01-23 00:34:21 +00001682 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001683 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001684
Damien429d7192013-10-04 19:53:11 +01001685 compile_node(comp, pn_body); // body
1686 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001687 EMIT_ARG(jump, success_label); // jump over exception handler
1688
1689 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001690 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001691
Damien George6f355fd2014-04-10 14:11:31 +01001692 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001693
1694 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001695 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1696 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001697
1698 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001699 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001700
Damiend99b0522013-12-21 18:17:45 +00001701 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001702 // this is a catch all exception handler
1703 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001704 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001705 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001706 return;
1707 }
1708 } else {
1709 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001710 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1711 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1712 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1713 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001714 // handler binds the exception to a local
1715 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001716 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001717 }
1718 }
1719 EMIT(dup_top);
1720 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001721 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001722 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001723 }
1724
1725 EMIT(pop_top);
1726
1727 if (qstr_exception_local == 0) {
1728 EMIT(pop_top);
1729 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001730 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001731 }
1732
1733 EMIT(pop_top);
1734
Damien George6f355fd2014-04-10 14:11:31 +01001735 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001736 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001737 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001738 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001739 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001740 }
1741 compile_node(comp, pns_except->nodes[1]);
1742 if (qstr_exception_local != 0) {
1743 EMIT(pop_block);
1744 }
1745 EMIT(pop_except);
1746 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001747 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1748 EMIT_ARG(label_assign, l3);
1749 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001750 compile_store_id(comp, qstr_exception_local);
1751 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001752
Damien George8dcc0c72014-03-27 10:55:21 +00001753 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001754 EMIT(end_finally);
1755 }
Damien Georgeb9791222014-01-23 00:34:21 +00001756 EMIT_ARG(jump, l2);
1757 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001758 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001759 }
1760
Damien George8dcc0c72014-03-27 10:55:21 +00001761 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001762 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001763 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001764
Damien Georgeb9791222014-01-23 00:34:21 +00001765 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001766 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001767 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001768}
1769
Damien George6be0b0a2014-08-15 14:30:52 +01001770STATIC 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 +01001771 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001772
Damien Georgeb9791222014-01-23 00:34:21 +00001773 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001774 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001775
Damien429d7192013-10-04 19:53:11 +01001776 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001777 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001778 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001779 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001780 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001781 } else {
1782 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1783 }
1784 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001785 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1786 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001787 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001788
Damien George8dcc0c72014-03-27 10:55:21 +00001789 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001790 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001791}
1792
Damien George969a6b32014-12-10 22:07:04 +00001793STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001794 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1795 {
Damiend99b0522013-12-21 18:17:45 +00001796 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1797 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001798 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001799 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1800 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001801 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001802 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001803 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001804 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001805 // no finally
1806 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1807 } else {
1808 // have finally
Damiend99b0522013-12-21 18:17:45 +00001809 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 +01001810 }
1811 } else {
1812 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001813 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001814 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001815 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001816 }
Damien429d7192013-10-04 19:53:11 +01001817 }
1818}
1819
Damien George6be0b0a2014-08-15 14:30:52 +01001820STATIC 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 +01001821 if (n == 0) {
1822 // no more pre-bits, compile the body of the with
1823 compile_node(comp, body);
1824 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001825 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001826 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001827 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001828 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001829 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001830 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001831 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1832 } else {
1833 // this pre-bit is just an expression
1834 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001835 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001836 EMIT(pop_top);
1837 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001838 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001839 // compile additional pre-bits and the body
1840 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1841 // finish this with block
1842 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001843 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1844 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001845 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001846 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001847 EMIT(end_finally);
1848 }
1849}
1850
Damien George969a6b32014-12-10 22:07:04 +00001851STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001852 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001853 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001854 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001855 assert(n > 0);
1856
1857 // compile in a nested fashion
1858 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1859}
1860
Damien George969a6b32014-12-10 22:07:04 +00001861STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001862 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001863 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1864 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001865 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001866 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001867 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001868 EMIT(pop_top);
1869
Damien429d7192013-10-04 19:53:11 +01001870 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001871 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001872 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001873 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001874 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1875 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001876 // do nothing with a lonely constant
1877 } else {
1878 compile_node(comp, pns->nodes[0]); // just an expression
1879 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1880 }
Damien429d7192013-10-04 19:53:11 +01001881 }
1882 } else {
Damiend99b0522013-12-21 18:17:45 +00001883 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1884 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001885 if (kind == PN_expr_stmt_augassign) {
1886 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1887 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001888 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001889 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001890 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001891 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1892 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1893 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1894 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1895 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1896 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1897 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1898 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1899 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1900 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1901 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001902 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001903 }
Damien George7e5fb242014-02-01 22:18:47 +00001904 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001905 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1906 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001907 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1908 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001909 // following CPython, we store left-most first
1910 if (rhs > 0) {
1911 EMIT(dup_top);
1912 }
1913 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1914 for (int i = 0; i < rhs; i++) {
1915 if (i + 1 < rhs) {
1916 EMIT(dup_top);
1917 }
Damiend99b0522013-12-21 18:17:45 +00001918 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001919 }
Damien George0bb97132015-02-27 14:25:47 +00001920 } else {
1921 assert(kind == PN_expr_stmt_assign); // should be
Damien George42e0c592015-03-14 13:11:35 +00001922 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
1923 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001924 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1925 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1926 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001927 // optimisation for a, b = c, d
Damien George4dea9222015-04-09 15:29:54 +00001928 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1929 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001930 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1931 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1932 // can't optimise when it's a star expression on the lhs
1933 goto no_optimisation;
1934 }
Damien429d7192013-10-04 19:53:11 +01001935 compile_node(comp, pns10->nodes[0]); // rhs
1936 compile_node(comp, pns10->nodes[1]); // rhs
1937 EMIT(rot_two);
1938 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1939 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001940 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
1941 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001942 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1943 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1944 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001945 // optimisation for a, b, c = d, e, f
Damien George4dea9222015-04-09 15:29:54 +00001946 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1947 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001948 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1949 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1950 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1951 // can't optimise when it's a star expression on the lhs
1952 goto no_optimisation;
1953 }
Damien429d7192013-10-04 19:53:11 +01001954 compile_node(comp, pns10->nodes[0]); // rhs
1955 compile_node(comp, pns10->nodes[1]); // rhs
1956 compile_node(comp, pns10->nodes[2]); // rhs
1957 EMIT(rot_three);
1958 EMIT(rot_two);
1959 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1960 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1961 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1962 } else {
Damien George495d7812014-04-08 17:51:47 +01001963 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01001964 compile_node(comp, pns1->nodes[0]); // rhs
1965 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1966 }
Damien429d7192013-10-04 19:53:11 +01001967 }
1968 }
1969}
1970
Damien George6be0b0a2014-08-15 14:30:52 +01001971STATIC 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 +00001972 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001973 compile_node(comp, pns->nodes[0]);
1974 for (int i = 1; i < num_nodes; i += 1) {
1975 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001976 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001977 }
1978}
1979
Damien George969a6b32014-12-10 22:07:04 +00001980STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001981 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1982 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001983
Damien George6f355fd2014-04-10 14:11:31 +01001984 uint l_fail = comp_next_label(comp);
1985 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001986 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1987 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001988 EMIT_ARG(jump, l_end);
1989 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001990 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01001991 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001992 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001993}
1994
Damien George969a6b32014-12-10 22:07:04 +00001995STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001996 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001997 //mp_parse_node_t pn_params = pns->nodes[0];
1998 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001999
Damien George36db6bc2014-05-07 17:24:22 +01002000 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002001 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002002 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 +01002003 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002004 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002005 }
2006
2007 // get the scope for this lambda
2008 scope_t *this_scope = (scope_t*)pns->nodes[2];
2009
2010 // make the lambda
2011 close_over_variables_etc(comp, this_scope, 0, 0);
2012}
2013
Damien George7711afb2015-02-28 15:10:18 +00002014STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002015 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002016 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002017 for (int i = 0; i < n; i += 1) {
2018 compile_node(comp, pns->nodes[i]);
2019 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002020 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002021 }
2022 }
Damien Georgeb9791222014-01-23 00:34:21 +00002023 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002024}
2025
Damien George7711afb2015-02-28 15:10:18 +00002026STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2027 compile_or_and_test(comp, pns, true);
2028}
2029
Damien George969a6b32014-12-10 22:07:04 +00002030STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002031 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002032}
2033
Damien George969a6b32014-12-10 22:07:04 +00002034STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002035 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002036 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002037}
2038
Damien George969a6b32014-12-10 22:07:04 +00002039STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002040 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002041 compile_node(comp, pns->nodes[0]);
2042 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002043 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002044 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002045 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002046 }
2047 for (int i = 1; i + 1 < num_nodes; i += 2) {
2048 compile_node(comp, pns->nodes[i + 1]);
2049 if (i + 2 < num_nodes) {
2050 EMIT(dup_top);
2051 EMIT(rot_three);
2052 }
Damien George7e5fb242014-02-01 22:18:47 +00002053 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002054 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002055 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002056 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2057 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2058 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2059 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2060 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2061 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002062 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002063 }
2064 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002065 } else {
2066 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002067 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2068 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002069 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002070 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002071 } else {
2072 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002073 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002074 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002075 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002076 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002077 }
Damien429d7192013-10-04 19:53:11 +01002078 }
Damien429d7192013-10-04 19:53:11 +01002079 }
2080 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002081 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002082 }
2083 }
2084 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002085 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002086 EMIT_ARG(jump, l_end);
2087 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002088 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002089 EMIT(rot_two);
2090 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002091 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002092 }
2093}
2094
Damien George969a6b32014-12-10 22:07:04 +00002095STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002096 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002097}
2098
Damien George969a6b32014-12-10 22:07:04 +00002099STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002100 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002101}
2102
Damien George969a6b32014-12-10 22:07:04 +00002103STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002104 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002105}
2106
Damien George969a6b32014-12-10 22:07:04 +00002107STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002108 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002109}
2110
Damien George969a6b32014-12-10 22:07:04 +00002111STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002112 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002113 compile_node(comp, pns->nodes[0]);
2114 for (int i = 1; i + 1 < num_nodes; i += 2) {
2115 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002116 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002117 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002118 } else {
Damien George0bb97132015-02-27 14:25:47 +00002119 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2120 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002121 }
2122 }
2123}
2124
Damien George969a6b32014-12-10 22:07:04 +00002125STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002126 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002127 compile_node(comp, pns->nodes[0]);
2128 for (int i = 1; i + 1 < num_nodes; i += 2) {
2129 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002130 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002131 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002132 } else {
Damien George0bb97132015-02-27 14:25:47 +00002133 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2134 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002135 }
2136 }
2137}
2138
Damien George969a6b32014-12-10 22:07:04 +00002139STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002140 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002141 compile_node(comp, pns->nodes[0]);
2142 for (int i = 1; i + 1 < num_nodes; i += 2) {
2143 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002144 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002145 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002146 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002147 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002148 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002149 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002150 } else {
Damien George0bb97132015-02-27 14:25:47 +00002151 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2152 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002153 }
2154 }
2155}
2156
Damien George969a6b32014-12-10 22:07:04 +00002157STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002158 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002159 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002160 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002161 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002162 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002163 } else {
Damien George0bb97132015-02-27 14:25:47 +00002164 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2165 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002166 }
2167}
2168
Damien George969a6b32014-12-10 22:07:04 +00002169STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002170 // this is to handle special super() call
2171 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2172
2173 compile_generic_all_nodes(comp, pns);
2174}
2175
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002176STATIC 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 +01002177 // function to call is on top of stack
2178
Damien George35e2a4e2014-02-05 00:51:47 +00002179 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002180 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 +00002181 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002182 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002183 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002184 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002185 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002186 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002187 EMIT_ARG(call_function, 2, 0, 0);
2188 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002189 }
2190 }
Damien George01039b52014-12-21 17:44:27 +00002191 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002192 return;
2193 }
Damien George35e2a4e2014-02-05 00:51:47 +00002194
Damien George2c0842b2014-04-27 16:46:51 +01002195 // get the list of arguments
2196 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002197 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002198
Damien George2c0842b2014-04-27 16:46:51 +01002199 // compile the arguments
2200 // Rather than calling compile_node on the list, we go through the list of args
2201 // explicitly here so that we can count the number of arguments and give sensible
2202 // error messages.
2203 int n_positional = n_positional_extra;
2204 uint n_keyword = 0;
2205 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002206 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002207 for (int i = 0; i < n_args; i++) {
2208 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2209 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2210 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2211 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2212 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2213 return;
2214 }
2215 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002216 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002217 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2218 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2219 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2220 return;
2221 }
2222 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002223 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002224 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2225 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2226 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2227 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2228 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2229 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2230 return;
2231 }
Damien George59fba2d2015-06-25 14:42:13 +00002232 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien George2c0842b2014-04-27 16:46:51 +01002233 compile_node(comp, pns2->nodes[0]);
2234 n_keyword += 1;
2235 } else {
2236 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2237 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2238 n_positional++;
2239 }
2240 } else {
2241 goto normal_argument;
2242 }
2243 } else {
2244 normal_argument:
2245 if (n_keyword > 0) {
2246 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2247 return;
2248 }
2249 compile_node(comp, args[i]);
2250 n_positional++;
2251 }
Damien429d7192013-10-04 19:53:11 +01002252 }
2253
Delio Brignolie6978a42015-09-17 12:07:06 +02002254 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002255 // if we had one but not the other then we load "null" as a place holder
2256 if (star_flags != 0) {
2257 if (star_args_node == NULL) {
2258 EMIT(load_null);
2259 } else {
2260 compile_node(comp, star_args_node->nodes[0]);
2261 }
2262 if (dblstar_args_node == NULL) {
2263 EMIT(load_null);
2264 } else {
2265 compile_node(comp, dblstar_args_node->nodes[0]);
2266 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002267 }
2268
Damien George2c0842b2014-04-27 16:46:51 +01002269 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002270 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002271 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002272 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002273 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002274 }
Damien429d7192013-10-04 19:53:11 +01002275}
2276
Damien George969a6b32014-12-10 22:07:04 +00002277STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002278 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002279 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002280 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 +01002281 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002282 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2283 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002284 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002285 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002286 i += 1;
2287 } else {
2288 compile_node(comp, pns->nodes[i]);
2289 }
Damien George35e2a4e2014-02-05 00:51:47 +00002290 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002291 }
2292}
2293
Damien George969a6b32014-12-10 22:07:04 +00002294STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002295 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002296 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002297}
2298
Damien George969a6b32014-12-10 22:07:04 +00002299STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002300 // a list of strings
Damien63321742013-12-10 17:41:49 +00002301
2302 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002303 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002304 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002305 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002306 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002307 int pn_kind;
2308 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2309 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2310 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2311 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2312 } else {
2313 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2314 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002315 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2316 pn_kind = MP_PARSE_NODE_STRING;
2317 } else {
2318 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2319 pn_kind = MP_PARSE_NODE_BYTES;
2320 }
Damien George40f3c022014-07-03 13:25:24 +01002321 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002322 }
Damien63321742013-12-10 17:41:49 +00002323 if (i == 0) {
2324 string_kind = pn_kind;
2325 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002326 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002327 return;
2328 }
Damien429d7192013-10-04 19:53:11 +01002329 }
Damien63321742013-12-10 17:41:49 +00002330
Damien George65ef6b72015-01-14 21:17:27 +00002331 // if we are not in the last pass, just load a dummy object
2332 if (comp->pass != MP_PASS_EMIT) {
2333 EMIT_ARG(load_const_obj, mp_const_none);
2334 return;
2335 }
2336
Damien63321742013-12-10 17:41:49 +00002337 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002338 vstr_t vstr;
2339 vstr_init_len(&vstr, n_bytes);
2340 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002341 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002342 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002343 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002344 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2345 memcpy(s_dest, s, s_len);
2346 s_dest += s_len;
2347 } else {
2348 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002349 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2350 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002351 }
Damien63321742013-12-10 17:41:49 +00002352 }
Damien George65ef6b72015-01-14 21:17:27 +00002353
2354 // load the object
Damien George05005f62015-01-21 22:48:37 +00002355 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 +01002356}
2357
2358// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002359STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002360 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2361 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2362 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002363
Damien George36db6bc2014-05-07 17:24:22 +01002364 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002365 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002366 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 +01002367 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002368 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002369 }
2370
2371 // get the scope for this comprehension
2372 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2373
2374 // compile the comprehension
2375 close_over_variables_etc(comp, this_scope, 0, 0);
2376
2377 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2378 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002379 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002380}
2381
Damien George969a6b32014-12-10 22:07:04 +00002382STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002383 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002384 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002385 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2386 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2387 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2388 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2389 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2390 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2391 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002392 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002393 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002394 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002395 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002396 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002397 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002398 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002399 // generator expression
2400 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2401 } else {
2402 // tuple with 2 items
2403 goto tuple_with_2_items;
2404 }
2405 } else {
2406 // tuple with 2 items
2407 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002408 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002409 }
2410 } else {
2411 // parenthesis around a single item, is just that item
2412 compile_node(comp, pns->nodes[0]);
2413 }
2414}
2415
Damien George969a6b32014-12-10 22:07:04 +00002416STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002417 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002418 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002419 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002420 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2421 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2422 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2423 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2424 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002425 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002426 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002427 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002428 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002429 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002430 // list of many items
2431 compile_node(comp, pns2->nodes[0]);
2432 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002433 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002434 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002435 // list comprehension
2436 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2437 } else {
2438 // list with 2 items
2439 goto list_with_2_items;
2440 }
2441 } else {
2442 // list with 2 items
2443 list_with_2_items:
2444 compile_node(comp, pns2->nodes[0]);
2445 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002446 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002447 }
2448 } else {
2449 // list with 1 item
2450 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002451 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002452 }
2453}
2454
Damien George969a6b32014-12-10 22:07:04 +00002455STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002456 mp_parse_node_t pn = pns->nodes[0];
2457 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002458 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002459 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002460 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2461 pns = (mp_parse_node_struct_t*)pn;
2462 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002463 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002464 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002465 compile_node(comp, pn);
2466 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002467 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2468 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2469 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2470 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002471 // dict/set with multiple elements
2472
2473 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002474 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002475 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002476
2477 // first element sets whether it's a dict or set
2478 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002479 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002480 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002481 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002482 compile_node(comp, pns->nodes[0]);
2483 EMIT(store_map);
2484 is_dict = true;
2485 } else {
2486 // a set
2487 compile_node(comp, pns->nodes[0]); // 1st value of set
2488 is_dict = false;
2489 }
2490
2491 // process rest of elements
2492 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002493 mp_parse_node_t pn_i = nodes[i];
2494 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2495 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002496 if (is_dict) {
2497 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002498 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002499 return;
2500 }
2501 EMIT(store_map);
2502 } else {
2503 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002504 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002505 return;
2506 }
2507 }
2508 }
2509
Damien Georgee37dcaa2014-12-27 17:07:16 +00002510 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002511 // if it's a set, build it
2512 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002513 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002514 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002515 #endif
Damien George0bb97132015-02-27 14:25:47 +00002516 } else {
2517 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002518 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002519 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002520 // a dictionary comprehension
2521 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2522 } else {
2523 // a set comprehension
2524 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2525 }
Damien429d7192013-10-04 19:53:11 +01002526 }
2527 } else {
2528 // set with one element
2529 goto set_with_one_element;
2530 }
2531 } else {
2532 // set with one element
2533 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002534 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002535 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002536 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002537 #else
2538 assert(0);
2539 #endif
Damien429d7192013-10-04 19:53:11 +01002540 }
2541}
2542
Damien George969a6b32014-12-10 22:07:04 +00002543STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002544 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002545}
2546
Damien George969a6b32014-12-10 22:07:04 +00002547STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002548 // object who's index we want is on top of stack
2549 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002550 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002551}
2552
Damien George969a6b32014-12-10 22:07:04 +00002553STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002554 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002555 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002556}
2557
Damien George83204f32014-12-27 17:20:41 +00002558#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002559STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002560 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2561 mp_parse_node_t pn = pns->nodes[0];
2562 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002563 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002564 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2565 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002566 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2567 pns = (mp_parse_node_struct_t*)pn;
2568 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002569 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002570 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002571 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002572 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002573 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002574 } else {
2575 // [?::x]
2576 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002577 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002578 }
Damiend99b0522013-12-21 18:17:45 +00002579 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002580 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002581 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2582 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2583 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2584 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002585 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002586 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002587 } else {
2588 // [?:x:x]
2589 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002590 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002591 }
2592 } else {
2593 // [?:x]
2594 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002595 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002596 }
2597 } else {
2598 // [?:x]
2599 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002600 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002601 }
2602}
2603
Damien George969a6b32014-12-10 22:07:04 +00002604STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002605 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002606 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2607 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002608}
2609
Damien George969a6b32014-12-10 22:07:04 +00002610STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002611 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002612 compile_subscript_3_helper(comp, pns);
2613}
Damien George83204f32014-12-27 17:20:41 +00002614#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002615
Damien George969a6b32014-12-10 22:07:04 +00002616STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002617 // if this is called then we are compiling a dict key:value pair
2618 compile_node(comp, pns->nodes[1]); // value
2619 compile_node(comp, pns->nodes[0]); // key
2620}
2621
Damien George969a6b32014-12-10 22:07:04 +00002622STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002623 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002624 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002625 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002626}
2627
Damien George969a6b32014-12-10 22:07:04 +00002628STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002629 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002630 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002631 return;
2632 }
Damiend99b0522013-12-21 18:17:45 +00002633 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002634 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002635 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002636 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2637 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002638 compile_node(comp, pns->nodes[0]);
2639 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002640 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002641 EMIT(yield_from);
2642 } else {
2643 compile_node(comp, pns->nodes[0]);
2644 EMIT(yield_value);
2645 }
2646}
2647
Damien George65ef6b72015-01-14 21:17:27 +00002648STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2649 // only create and load the actual str object on the last pass
2650 if (comp->pass != MP_PASS_EMIT) {
2651 EMIT_ARG(load_const_obj, mp_const_none);
2652 } else {
2653 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2654 }
2655}
2656
2657STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2658 // only create and load the actual bytes object on the last pass
2659 if (comp->pass != MP_PASS_EMIT) {
2660 EMIT_ARG(load_const_obj, mp_const_none);
2661 } else {
2662 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2663 }
2664}
2665
Damien George7d414a12015-02-08 01:57:40 +00002666STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2667 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2668}
2669
Damiend99b0522013-12-21 18:17:45 +00002670typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002671STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002672#define nc NULL
2673#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002674#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002675#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002676#undef nc
2677#undef c
2678#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002679 NULL,
2680 compile_string,
2681 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002682 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002683};
2684
Damien George6be0b0a2014-08-15 14:30:52 +01002685STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002686 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002687 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002688 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002689 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002690 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002691 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002692 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002693 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002694 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002695 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2696 case MP_PARSE_NODE_BYTES:
2697 // only create and load the actual bytes object on the last pass
2698 if (comp->pass != MP_PASS_EMIT) {
2699 EMIT_ARG(load_const_obj, mp_const_none);
2700 } else {
2701 mp_uint_t len;
2702 const byte *data = qstr_data(arg, &len);
2703 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2704 }
2705 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002706 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002707 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002708 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002709 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002710 // do nothing
2711 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002712 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002713 }
2714 break;
Damien429d7192013-10-04 19:53:11 +01002715 }
2716 } else {
Damiend99b0522013-12-21 18:17:45 +00002717 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002718 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002719 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2720 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002721#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002722 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2723 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002724#endif
Damien George65ef6b72015-01-14 21:17:27 +00002725 compile_syntax_error(comp, pn, "internal compiler error");
2726 } else {
2727 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002728 }
2729 }
2730}
2731
Damien George2ac4af62014-08-15 16:45:41 +01002732STATIC 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 +01002733 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002734 qstr param_name = MP_QSTR_NULL;
2735 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002736 if (MP_PARSE_NODE_IS_ID(pn)) {
2737 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002738 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002739 // comes after a star, so counts as a keyword-only parameter
2740 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002741 } else {
Damien George2827d622014-04-27 15:50:52 +01002742 // comes before a star, so counts as a positional parameter
2743 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002744 }
Damienb14de212013-10-06 00:28:28 +01002745 } else {
Damiend99b0522013-12-21 18:17:45 +00002746 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2747 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2748 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2749 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002750 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002751 // comes after a star, so counts as a keyword-only parameter
2752 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002753 } else {
Damien George2827d622014-04-27 15:50:52 +01002754 // comes before a star, so counts as a positional parameter
2755 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002756 }
Damiend99b0522013-12-21 18:17:45 +00002757 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002758 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002759 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002760 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002761 // bare star
2762 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002763 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002764 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002765 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002766 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002767 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002768 } else {
2769 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002770 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002771 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002772 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2773 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002774 }
Damien George0bb97132015-02-27 14:25:47 +00002775 } else {
2776 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002777 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002778 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002779 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002780 }
Damien429d7192013-10-04 19:53:11 +01002781 }
2782
Damien George2827d622014-04-27 15:50:52 +01002783 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002784 bool added;
2785 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2786 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002787 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002788 return;
2789 }
Damien429d7192013-10-04 19:53:11 +01002790 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002791 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002792 }
2793}
2794
Damien George2827d622014-04-27 15:50:52 +01002795STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002796 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002797}
2798
Damien George2827d622014-04-27 15:50:52 +01002799STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002800 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2801}
2802
Damien Georgeea5b59b2015-09-04 16:44:14 +01002803#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002804STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2805 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2806 // no annotation
2807 return;
2808 }
2809
2810 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2811 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2812 // named parameter with possible annotation
2813 // fallthrough
2814 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2815 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2816 // named star with possible annotation
2817 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2818 // fallthrough
2819 } else {
2820 // no annotation
2821 return;
2822 }
2823 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
2824 // double star with possible annotation
2825 // fallthrough
2826 } else {
2827 // no annotation
2828 return;
2829 }
2830
2831 mp_parse_node_t pn_annotation = pns->nodes[1];
2832
2833 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002834 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2835 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2836 assert(id_info != NULL);
2837
Damien Georgeea5b59b2015-09-04 16:44:14 +01002838 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2839 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2840 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2841 } else {
2842 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002843 }
Damien George2ac4af62014-08-15 16:45:41 +01002844 }
Damien429d7192013-10-04 19:53:11 +01002845}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002846#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002847
Damien George6be0b0a2014-08-15 14:30:52 +01002848STATIC 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 +01002849 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002850 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002851 // no more nested if/for; compile inner expression
2852 compile_node(comp, pn_inner_expr);
2853 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002854 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002855 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002857 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002858 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002859 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002860 #endif
Damien429d7192013-10-04 19:53:11 +01002861 } else {
2862 EMIT(yield_value);
2863 EMIT(pop_top);
2864 }
Damiend99b0522013-12-21 18:17:45 +00002865 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002866 // if condition
Damiend99b0522013-12-21 18:17:45 +00002867 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002868 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2869 pn_iter = pns_comp_if->nodes[1];
2870 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002871 } else {
2872 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002873 // for loop
Damiend99b0522013-12-21 18:17:45 +00002874 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002875 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002876 uint l_end2 = comp_next_label(comp);
2877 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002878 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002879 EMIT_ARG(label_assign, l_top2);
2880 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002881 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2882 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 +00002883 EMIT_ARG(jump, l_top2);
2884 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002885 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002886 }
2887}
2888
Damien George1463c1f2014-04-25 23:52:57 +01002889STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002890#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002891 // see http://www.python.org/dev/peps/pep-0257/
2892
2893 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002894 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002895 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002896 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002897 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002898 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2899 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002900 for (int i = 0; i < num_nodes; i++) {
2901 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002902 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 +00002903 // not a newline, so this is the first statement; finish search
2904 break;
2905 }
2906 }
2907 // 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 +00002908 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002909 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002910 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002911 } else {
2912 return;
2913 }
2914
2915 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002916 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002917 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002918 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2919 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2920 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2921 // compile the doc string
2922 compile_node(comp, pns->nodes[0]);
2923 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002924 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002925 }
2926 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002927#else
2928 (void)comp;
2929 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002930#endif
Damien429d7192013-10-04 19:53:11 +01002931}
2932
Damien George1463c1f2014-04-25 23:52:57 +01002933STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002934 comp->pass = pass;
2935 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002936 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002937 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002938
Damien George36db6bc2014-05-07 17:24:22 +01002939 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002940 // reset maximum stack sizes in scope
2941 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002942 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002943 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002944 }
2945
Damien429d7192013-10-04 19:53:11 +01002946 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002947 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2948 assert(scope->kind == SCOPE_MODULE);
2949 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2950 compile_node(comp, pns->nodes[0]); // compile the expression
2951 EMIT(return_value);
2952 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002953 if (!comp->is_repl) {
2954 check_for_doc_string(comp, scope->pn);
2955 }
Damien429d7192013-10-04 19:53:11 +01002956 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002957 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002958 EMIT(return_value);
2959 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002960 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2961 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2962 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002963
2964 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002965 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002966 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002967 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002968 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002969 }
2970 #if MICROPY_EMIT_NATIVE
2971 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002972 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002973 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002974
Damien George2ac4af62014-08-15 16:45:41 +01002975 // argument annotations
2976 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2977
2978 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01002979 mp_parse_node_t pn_annotation = pns->nodes[2];
2980 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01002981 // nodes[2] can be null or a test-expr
2982 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2983 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2984 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2985 } else {
2986 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002987 }
2988 }
Damien George2ac4af62014-08-15 16:45:41 +01002989 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01002990 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002991
2992 compile_node(comp, pns->nodes[3]); // 3 is function body
2993 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002994 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002995 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002996 EMIT(return_value);
2997 }
2998 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002999 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3000 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3001 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003002
3003 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003004 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003005 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003006 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003007 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3008 }
3009
3010 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003011
3012 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3013 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3014 EMIT(pop_top);
3015 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3016 }
Damien429d7192013-10-04 19:53:11 +01003017 EMIT(return_value);
3018 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3019 // a bit of a hack at the moment
3020
Damiend99b0522013-12-21 18:17:45 +00003021 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3022 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3023 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3024 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3025 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003026
Damien George708c0732014-04-27 19:23:46 +01003027 // We need a unique name for the comprehension argument (the iterator).
3028 // CPython uses .0, but we should be able to use anything that won't
3029 // clash with a user defined variable. Best to use an existing qstr,
3030 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01003031 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01003032 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003033 bool added;
3034 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3035 assert(added);
3036 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003037 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003038 }
3039
3040 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003041 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003042 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003043 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003044 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003045 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003046 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003047 #endif
Damien429d7192013-10-04 19:53:11 +01003048 }
3049
Damien George6f355fd2014-04-10 14:11:31 +01003050 uint l_end = comp_next_label(comp);
3051 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00003052 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00003053 EMIT_ARG(label_assign, l_top);
3054 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003055 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3056 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003057 EMIT_ARG(jump, l_top);
3058 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003059 EMIT(for_iter_end);
3060
3061 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003062 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003063 }
3064 EMIT(return_value);
3065 } else {
3066 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003067 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3068 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3069 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003070
Damien George36db6bc2014-05-07 17:24:22 +01003071 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003072 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003073 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003074 assert(added);
3075 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003076 }
3077
Damien George542bd6b2015-03-26 14:42:40 +00003078 compile_load_id(comp, MP_QSTR___name__);
3079 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003080 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003081 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003082
3083 check_for_doc_string(comp, pns->nodes[2]);
3084 compile_node(comp, pns->nodes[2]); // 2 is class body
3085
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003086 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003087 assert(id != NULL);
3088 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003089 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003090 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003091 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003092 }
3093 EMIT(return_value);
3094 }
3095
Damien415eb6f2013-10-05 12:19:06 +01003096 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003097
3098 // make sure we match all the exception levels
3099 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003100}
3101
Damien George094d4502014-04-02 17:31:27 +01003102#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003103// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003104STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003105 comp->pass = pass;
3106 comp->scope_cur = scope;
3107 comp->next_label = 1;
3108
3109 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003110 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003111 return;
3112 }
3113
Damien George36db6bc2014-05-07 17:24:22 +01003114 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003115 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003116 }
3117
Damien826005c2013-10-05 23:17:28 +01003118 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003119 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3120 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3121 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003122
Damiend99b0522013-12-21 18:17:45 +00003123 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003124
Damiena2f2f7d2013-10-06 00:14:13 +01003125 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003126 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003127 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003128 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003129 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003130 if (comp->compile_error != MP_OBJ_NULL) {
3131 goto inline_asm_error;
3132 }
Damiena2f2f7d2013-10-06 00:14:13 +01003133 }
3134
Damiend99b0522013-12-21 18:17:45 +00003135 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003136
Damiend99b0522013-12-21 18:17:45 +00003137 mp_parse_node_t pn_body = pns->nodes[3]; // body
3138 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003139 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003140
Damien826005c2013-10-05 23:17:28 +01003141 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003142 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3143 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003144 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3145 // no instructions
3146 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003147 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003148 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003149 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003150 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003151 return;
3152 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003153
3154 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003155 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003156 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3157 goto not_an_instruction;
3158 }
Damiend99b0522013-12-21 18:17:45 +00003159 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003160 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3161 goto not_an_instruction;
3162 }
3163 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3164 goto not_an_instruction;
3165 }
3166 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3167 goto not_an_instruction;
3168 }
Damiend99b0522013-12-21 18:17:45 +00003169 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003170
3171 // parse node looks like an instruction
3172 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003173 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3174 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3175 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003176 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003177
3178 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003179 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003180 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003181 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003182 return;
3183 }
Damien George6f355fd2014-04-10 14:11:31 +01003184 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003185 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003186 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3187 compile_syntax_error(comp, nodes[i], "label redefined");
3188 return;
3189 }
Damien826005c2013-10-05 23:17:28 +01003190 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003191 } else if (op == MP_QSTR_align) {
3192 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003193 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003194 return;
3195 }
Damien George36db6bc2014-05-07 17:24:22 +01003196 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003197 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3198 }
3199 } else if (op == MP_QSTR_data) {
3200 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003201 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003202 return;
3203 }
Damien George36db6bc2014-05-07 17:24:22 +01003204 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003205 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003206 for (uint j = 1; j < n_args; j++) {
3207 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003208 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003209 return;
3210 }
Damien George50912e72015-01-20 11:55:10 +00003211 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003212 }
3213 }
Damien826005c2013-10-05 23:17:28 +01003214 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003215 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003216 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003217 }
3218 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003219
3220 if (comp->compile_error != MP_OBJ_NULL) {
3221 pns = pns2; // this is the parse node that had the error
3222 goto inline_asm_error;
3223 }
Damien826005c2013-10-05 23:17:28 +01003224 }
3225
Damien George36db6bc2014-05-07 17:24:22 +01003226 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003227 EMIT_INLINE_ASM(end_pass);
3228 }
3229
3230 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003231 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003232 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003233 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003234 }
Damien429d7192013-10-04 19:53:11 +01003235}
Damien George094d4502014-04-02 17:31:27 +01003236#endif
Damien429d7192013-10-04 19:53:11 +01003237
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003238STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003239 // in Micro Python we put the *x parameter after all other parameters (except **y)
3240 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3241 id_info_t *id_param = NULL;
3242 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3243 id_info_t *id = &scope->id_info[i];
3244 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3245 if (id_param != NULL) {
3246 // swap star param with last param
3247 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3248 }
3249 break;
3250 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3251 id_param = id;
3252 }
3253 }
3254 }
Damien George2827d622014-04-27 15:50:52 +01003255
Damien429d7192013-10-04 19:53:11 +01003256 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003257 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003258 scope->num_locals = 0;
3259 for (int i = 0; i < scope->id_info_len; i++) {
3260 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003261 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003262 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3263 continue;
3264 }
3265 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3266 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3267 }
Damien George2827d622014-04-27 15:50:52 +01003268 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003269 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003270 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003271 }
3272 }
3273
Damien George65dc9602015-08-14 12:24:11 +01003274 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003275 for (int i = 0; i < scope->id_info_len; i++) {
3276 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003277 // in Micro Python the cells come right after the fast locals
3278 // parameters are not counted here, since they remain at the start
3279 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003280 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003281 id->local_num = scope->num_locals;
3282 scope->num_locals += 1;
3283 }
Damien9ecbcff2013-12-11 00:41:43 +00003284 }
Damien9ecbcff2013-12-11 00:41:43 +00003285
Damien George65dc9602015-08-14 12:24:11 +01003286 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003287 // make sure they are in the order of the parent scope
3288 if (scope->parent != NULL) {
3289 int num_free = 0;
3290 for (int i = 0; i < scope->parent->id_info_len; i++) {
3291 id_info_t *id = &scope->parent->id_info[i];
3292 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3293 for (int j = 0; j < scope->id_info_len; j++) {
3294 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003295 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003296 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003297 // in Micro Python the frees come first, before the params
3298 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003299 num_free += 1;
3300 }
3301 }
3302 }
Damien429d7192013-10-04 19:53:11 +01003303 }
Damien George6baf76e2013-12-30 22:32:17 +00003304 // in Micro Python shift all other locals after the free locals
3305 if (num_free > 0) {
3306 for (int i = 0; i < scope->id_info_len; i++) {
3307 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003308 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003309 id->local_num += num_free;
3310 }
3311 }
Damien George2827d622014-04-27 15:50:52 +01003312 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 +00003313 scope->num_locals += num_free;
3314 }
Damien429d7192013-10-04 19:53:11 +01003315 }
Damien429d7192013-10-04 19:53:11 +01003316}
3317
Damien George65cad122014-04-06 11:48:15 +01003318mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
Damien Georgeb140bff2014-04-09 12:54:21 +01003319 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003320 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003321 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003322 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003323
Damien George62a3a282015-03-01 12:04:05 +00003324 // create the module scope
3325 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3326
3327 // optimise constants (scope must be set for error messages to work)
3328 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003329 mp_map_t consts;
3330 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003331 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003332 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003333
Damien Georgea210c772015-03-26 15:49:53 +00003334 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003335 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003336
Damien826005c2013-10-05 23:17:28 +01003337 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003338 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003339 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003340 comp->emit_method_table = &emit_bc_method_table;
3341 #endif
Damien Georgea77ffe62015-03-14 12:59:31 +00003342 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003343 comp->emit_inline_asm = NULL;
3344 comp->emit_inline_asm_method_table = NULL;
Damien Georgea77ffe62015-03-14 12:59:31 +00003345 #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 George62a3a282015-03-01 12:04:05 +00003488 mp_parse_node_free(module_scope->pn);
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 George41d02b62014-01-24 22:42:28 +00003498 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003499 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003500 m_del_obj(compiler_t, comp);
3501
Damien Georgea91ac202014-10-05 19:01:34 +01003502 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003503 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003504 } else {
Damien George1fb03172014-01-03 14:22:03 +00003505 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003506 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003507 }
Damien429d7192013-10-04 19:53:11 +01003508}