blob: 28e51e9c77eedccccb9cc2d886b1e33b3112516f [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
Damien George9d5e5c02015-09-24 13:15:57 +01006 * Copyright (c) 2013-2015 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
36#include "py/smallint.h"
37#include "py/runtime.h"
38#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010039
40// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien George65dc9602015-08-14 12:24:11 +010052#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
Damien George41125902015-03-26 16:44:14 +000053
54#if NEED_METHOD_TABLE
55
56// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000057#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
58#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000059#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
60#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien429d7192013-10-04 19:53:11 +010061
Damien George41125902015-03-26 16:44:14 +000062#else
63
64// if we only have the bytecode emitter enabled then we can do a direct call to the functions
65#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
66#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
67#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
68#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
69
70#endif
71
Damien George0496de22015-10-03 17:07:54 +010072#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
73#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
74
Damien Georgea91ac202014-10-05 19:01:34 +010075// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010076typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000077 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010078
Damien George78035b92014-04-09 12:27:39 +010079 uint8_t is_repl;
80 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010081 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010082 uint8_t have_star;
83
84 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +000085 mp_obj_t compile_error; // set to an exception object if there's an error
86 mp_uint_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +010087
Damien George6f355fd2014-04-10 14:11:31 +010088 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010089
Damien George69b89d22014-04-11 13:38:30 +000090 uint16_t num_dict_params;
91 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010092
Damien Georgea91ac202014-10-05 19:01:34 +010093 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
94 uint16_t continue_label;
95 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000096 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010097
Damien429d7192013-10-04 19:53:11 +010098 scope_t *scope_head;
99 scope_t *scope_cur;
100
Damien6cdd3af2013-10-05 18:08:26 +0100101 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000102 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100103 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000104 #endif
Damien826005c2013-10-05 23:17:28 +0100105
Damien Georgea77ffe62015-03-14 12:59:31 +0000106 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +0100107 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
108 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 +0000109 #endif
Damien429d7192013-10-04 19:53:11 +0100110} compiler_t;
111
Damien Georgecfc4c332015-07-29 22:16:01 +0000112STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
113 // if the line of the error is unknown then try to update it from the pn
114 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
115 comp->compile_error_line = (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100116 }
Damien George84d59c22015-07-27 22:20:00 +0100117}
118
119STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000120 // only register the error if there has been no other error
121 if (comp->compile_error == MP_OBJ_NULL) {
122 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
123 compile_error_set_line(comp, pn);
124 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000125}
126
Damien Georgeddd1e182015-01-10 14:07:24 +0000127#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100128STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300129 #if MICROPY_PY_UCTYPES
130 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
131 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100132 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100133 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100134};
Damien Georgeddd1e182015-01-10 14:07:24 +0000135STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
136#endif
Damien George57e99eb2014-04-10 22:42:11 +0100137
Damien Georgeffae48d2014-05-08 15:58:39 +0000138// this function is essentially a simple preprocessor
139STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
140 if (0) {
141 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100142#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000143 } else if (MP_PARSE_NODE_IS_ID(pn)) {
144 // lookup identifier in table of dynamic constants
145 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
146 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
147 if (elem != NULL) {
148 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
149 }
150#endif
151 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000152 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100153
Damien Georgeffae48d2014-05-08 15:58:39 +0000154 // fold some parse nodes before folding their arguments
155 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100156#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000157 case PN_expr_stmt:
158 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien Georgeb948de32015-10-08 14:26:01 +0100159 if (!(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_expr_stmt_augassign)
160 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_expr_stmt_assign_list))) {
161 // this node is of the form <x> = <y>
Damien Georgeffae48d2014-05-08 15:58:39 +0000162 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
Damien Georgeb948de32015-10-08 14:26:01 +0100163 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)
164 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns->nodes[1])->nodes[0])
165 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns->nodes[1])->nodes[0]) == MP_QSTR_const
166 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns->nodes[1])->nodes[1], PN_trailer_paren)
167 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns->nodes[1])->nodes[2])
Damien Georgeffae48d2014-05-08 15:58:39 +0000168 ) {
169 // code to assign dynamic constants: id = const(value)
170
171 // get the id
172 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
173
174 // get the value
Damien Georgeb948de32015-10-08 14:26:01 +0100175 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns->nodes[1])->nodes[1])->nodes[0];
Damien Georgeffae48d2014-05-08 15:58:39 +0000176 pn_value = fold_constants(comp, pn_value, consts);
177 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
178 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
179 break;
180 }
Damien George40f3c022014-07-03 13:25:24 +0100181 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000182
183 // store the value in the table of dynamic constants
184 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
185 if (elem->value != MP_OBJ_NULL) {
186 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
187 break;
188 }
189 elem->value = MP_OBJ_NEW_SMALL_INT(value);
190
191 // replace const(value) with value
Damien Georgeb948de32015-10-08 14:26:01 +0100192 pns->nodes[1] = pn_value;
Damien Georgeffae48d2014-05-08 15:58:39 +0000193
194 // finished folding this assignment
195 return pn;
196 }
197 }
198 }
199 break;
200#endif
Damien George5042bce2014-05-25 22:06:06 +0100201 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000202 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000203 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100204 return pn;
Damien429d7192013-10-04 19:53:11 +0100205 }
206
Damien Georgeffae48d2014-05-08 15:58:39 +0000207 // fold arguments
208 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
209 for (int i = 0; i < n; i++) {
210 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
211 }
212
213 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000214 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100215 case PN_atom_paren:
216 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
217 // (int)
218 pn = pns->nodes[0];
219 }
220 break;
221
222 case PN_expr:
223 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
224 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100225 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
226 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100227 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
228 }
229 break;
230
231 case PN_and_expr:
232 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
233 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100234 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
235 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100236 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
237 }
238 break;
239
Damien429d7192013-10-04 19:53:11 +0100240 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000241 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 +0100242 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
243 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000244 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100245 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000246 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 +0100247 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
248 }
Damien George0bb97132015-02-27 14:25:47 +0000249 } else {
250 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100251 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000252 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200253 // Shifting to big amounts is underfined behavior
254 // in C and is CPU-dependent; propagate sign bit.
255 arg1 = BITS_PER_WORD - 1;
256 }
Damiend99b0522013-12-21 18:17:45 +0000257 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100258 }
259 }
260 break;
261
262 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100263 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000264 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 +0100265 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
266 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000267 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100268 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000269 arg0 += arg1;
Damien George0bb97132015-02-27 14:25:47 +0000270 } else {
271 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100272 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000273 arg0 -= arg1;
Damien0efb3a12013-10-12 16:16:56 +0100274 }
Damien Georged1e355e2014-05-28 14:51:12 +0100275 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000276 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100277 }
278 }
279 break;
280
281 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000282 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 +0100283 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
284 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000285 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100286 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000287 if (!mp_small_int_mul_overflow(arg0, arg1)) {
288 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100289 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000290 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
291 }
292 }
Damiend99b0522013-12-21 18:17:45 +0000293 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100294 // int / int
295 // pass
Damiend99b0522013-12-21 18:17:45 +0000296 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100297 // int%int
Damien Georgee5635f42015-10-01 22:48:48 +0100298 if (arg1 != 0) {
299 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
300 }
Damien George0bb97132015-02-27 14:25:47 +0000301 } else {
302 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300303 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100304 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000305 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 +0300306 }
Damien429d7192013-10-04 19:53:11 +0100307 }
308 }
309 break;
310
311 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000312 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100313 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000314 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100315 // +int
Damiend99b0522013-12-21 18:17:45 +0000316 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
317 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100318 // -int
Damien George7e12a602015-10-08 13:02:00 +0100319 arg = -arg;
320 if (MP_SMALL_INT_FITS(arg)) {
321 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
322 }
Damien George0bb97132015-02-27 14:25:47 +0000323 } else {
324 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100325 // ~int
Damiend99b0522013-12-21 18:17:45 +0000326 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100327 }
328 }
329 break;
330
331 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100332 if (0) {
Damien Georgeddd1e182015-01-10 14:07:24 +0000333#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100334 } 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])) {
335 // id.id
336 // look it up in constant table, see if it can be replaced with an integer
Damien George4dea9222015-04-09 15:29:54 +0000337 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George57e99eb2014-04-10 22:42:11 +0100338 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
339 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
340 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
341 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
342 if (elem != NULL) {
343 mp_obj_t dest[2];
344 mp_load_method_maybe(elem->value, q_attr, dest);
345 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100346 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000347 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100348 }
349 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000350#endif
Damien429d7192013-10-04 19:53:11 +0100351 }
352 break;
353 }
354 }
355
356 return pn;
357}
358
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200359STATIC 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 +0100360STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000361STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100362
Damien George6f355fd2014-04-10 14:11:31 +0100363STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100364 return comp->next_label++;
365}
366
Damien George8dcc0c72014-03-27 10:55:21 +0000367STATIC void compile_increase_except_level(compiler_t *comp) {
368 comp->cur_except_level += 1;
369 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
370 comp->scope_cur->exc_stack_size = comp->cur_except_level;
371 }
372}
373
374STATIC void compile_decrease_except_level(compiler_t *comp) {
375 assert(comp->cur_except_level > 0);
376 comp->cur_except_level -= 1;
377}
378
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200379STATIC 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 +0100380 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100381 scope->parent = comp->scope_cur;
382 scope->next = NULL;
383 if (comp->scope_head == NULL) {
384 comp->scope_head = scope;
385 } else {
386 scope_t *s = comp->scope_head;
387 while (s->next != NULL) {
388 s = s->next;
389 }
390 s->next = scope;
391 }
392 return scope;
393}
394
Damien George91bc32d2015-04-09 15:31:53 +0000395typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
396
397STATIC 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 +0000398 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000399 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
400 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100401 for (int i = 0; i < num_nodes; i++) {
402 f(comp, pns->nodes[i]);
403 }
Damiend99b0522013-12-21 18:17:45 +0000404 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100405 f(comp, pn);
406 }
407}
408
Damien George969a6b32014-12-10 22:07:04 +0000409STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000410 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100411 for (int i = 0; i < num_nodes; i++) {
412 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000413 if (comp->compile_error != MP_OBJ_NULL) {
414 // add line info for the error in case it didn't have a line number
415 compile_error_set_line(comp, pns->nodes[i]);
416 return;
417 }
Damien429d7192013-10-04 19:53:11 +0100418 }
419}
420
Damien George542bd6b2015-03-26 14:42:40 +0000421STATIC void compile_load_id(compiler_t *comp, qstr qst) {
422 if (comp->pass == MP_PASS_SCOPE) {
423 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
424 } else {
Damien George41125902015-03-26 16:44:14 +0000425 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000426 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000427 #else
428 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
429 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000430 }
431}
432
433STATIC void compile_store_id(compiler_t *comp, qstr qst) {
434 if (comp->pass == MP_PASS_SCOPE) {
435 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
436 } else {
Damien George41125902015-03-26 16:44:14 +0000437 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000438 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000439 #else
440 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
441 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000442 }
443}
444
445STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
446 if (comp->pass == MP_PASS_SCOPE) {
447 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
448 } else {
Damien George41125902015-03-26 16:44:14 +0000449 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000450 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000451 #else
452 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
453 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000454 }
455}
456
Damien George2c0842b2014-04-27 16:46:51 +0100457STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100458 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000459 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100460 compile_node(comp, pn);
461 total += 1;
462 }
463 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000464 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100465 for (int i = 0; i < n; i++) {
466 compile_node(comp, pns_list->nodes[i]);
467 }
468 total += n;
469 }
Damien Georgeb9791222014-01-23 00:34:21 +0000470 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100471}
Damien429d7192013-10-04 19:53:11 +0100472
Damien George969a6b32014-12-10 22:07:04 +0000473STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100474 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000475 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100476}
477
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200478STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000479 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
480 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100481}
482
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200483STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000484 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
485 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100486}
487
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200488STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3a205172013-10-12 15:01:56 +0100489 if (node_is_const_false(pn)) {
490 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000491 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100492 }
493 return;
494 } else if (node_is_const_true(pn)) {
495 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000496 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100497 }
498 return;
Damiend99b0522013-12-21 18:17:45 +0000499 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
500 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
501 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
502 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100503 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000504 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100505 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100506 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000507 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100508 }
Damien George0b2fd912015-02-28 14:37:54 +0000509 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000510 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100511 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000512 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100513 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000514 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100515 }
516 }
517 return;
Damiend99b0522013-12-21 18:17:45 +0000518 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100519 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000520 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100521 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000522 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100523 }
Damiend99b0522013-12-21 18:17:45 +0000524 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100525 c_if_cond(comp, pns->nodes[0], !jump_if, label);
526 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100527 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
528 // cond is something in parenthesis
529 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
530 // empty tuple, acts as false for the condition
531 if (jump_if == false) {
532 EMIT_ARG(jump, label);
533 }
534 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
535 // non-empty tuple, acts as true for the condition
536 if (jump_if == true) {
537 EMIT_ARG(jump, label);
538 }
539 } else {
540 // parenthesis around 1 item, is just that item
541 c_if_cond(comp, pns->nodes[0], jump_if, label);
542 }
543 return;
Damien3a205172013-10-12 15:01:56 +0100544 }
545 }
546
547 // nothing special, fall back to default compiling for node and jump
548 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000549 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100550}
551
552typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100553STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100554
Damien George6be0b0a2014-08-15 14:30:52 +0100555STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100556 if (assign_kind != ASSIGN_AUG_STORE) {
557 compile_node(comp, pns->nodes[0]);
558 }
559
Damiend99b0522013-12-21 18:17:45 +0000560 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
561 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
562 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
563 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100564 if (assign_kind != ASSIGN_AUG_STORE) {
565 for (int i = 0; i < n - 1; i++) {
566 compile_node(comp, pns1->nodes[i]);
567 }
568 }
Damiend99b0522013-12-21 18:17:45 +0000569 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
570 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100571 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000572 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100573 if (assign_kind == ASSIGN_AUG_STORE) {
574 EMIT(rot_three);
575 EMIT(store_subscr);
576 } else {
577 compile_node(comp, pns1->nodes[0]);
578 if (assign_kind == ASSIGN_AUG_LOAD) {
579 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100580 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100581 } else {
582 EMIT(store_subscr);
583 }
584 }
Damiend99b0522013-12-21 18:17:45 +0000585 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
586 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100587 if (assign_kind == ASSIGN_AUG_LOAD) {
588 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000589 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100590 } else {
591 if (assign_kind == ASSIGN_AUG_STORE) {
592 EMIT(rot_two);
593 }
Damien Georgeb9791222014-01-23 00:34:21 +0000594 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100595 }
596 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100597 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100598 }
599 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100600 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100601 }
602
Damiend99b0522013-12-21 18:17:45 +0000603 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100604 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100605 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100606
607 return;
608
609cannot_assign:
610 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100611}
612
Damien George0288cf02014-04-11 11:53:00 +0000613// 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 +0100614STATIC 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 +0000615 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
616
617 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000618 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000619 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
620 EMIT_ARG(unpack_ex, 0, num_tail);
621 have_star_index = 0;
622 }
Damien George963a5a32015-01-16 17:47:07 +0000623 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000624 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000625 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000626 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
627 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100628 } else {
Damien George9d181f62014-04-27 16:55:27 +0100629 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100630 return;
631 }
632 }
633 }
Damien George963a5a32015-01-16 17:47:07 +0000634 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000635 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100636 }
Damien George0288cf02014-04-11 11:53:00 +0000637 if (num_head != 0) {
638 if (0 == have_star_index) {
639 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100640 } else {
Damien George0288cf02014-04-11 11:53:00 +0000641 c_assign(comp, node_head, ASSIGN_STORE);
642 }
643 }
Damien George963a5a32015-01-16 17:47:07 +0000644 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000645 if (num_head + i == have_star_index) {
646 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
647 } else {
648 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100649 }
650 }
651}
652
653// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100654STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100655 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000656 assert(!MP_PARSE_NODE_IS_NULL(pn));
657 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000658 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000659 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100660 switch (assign_kind) {
661 case ASSIGN_STORE:
662 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000663 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100664 break;
665 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000666 default:
Damien George542bd6b2015-03-26 14:42:40 +0000667 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100668 break;
669 }
670 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100671 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100672 return;
673 }
674 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000675 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000676 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
677 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100678 case PN_power:
679 // lhs is an index or attribute
680 c_assign_power(comp, pns, assign_kind);
681 break;
682
683 case PN_testlist_star_expr:
684 case PN_exprlist:
685 // lhs is a tuple
686 if (assign_kind != ASSIGN_STORE) {
687 goto bad_aug;
688 }
Damien George0288cf02014-04-11 11:53:00 +0000689 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100690 break;
691
692 case PN_atom_paren:
693 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000694 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100695 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100696 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000697 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000698 if (assign_kind != ASSIGN_STORE) {
699 goto bad_aug;
700 }
Damiend99b0522013-12-21 18:17:45 +0000701 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100702 goto testlist_comp;
703 } else {
704 // parenthesis around 1 item, is just that item
705 pn = pns->nodes[0];
706 goto tail_recursion;
707 }
708 break;
709
710 case PN_atom_bracket:
711 // lhs is something in brackets
712 if (assign_kind != ASSIGN_STORE) {
713 goto bad_aug;
714 }
Damiend99b0522013-12-21 18:17:45 +0000715 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100716 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000717 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000718 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
719 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100720 goto testlist_comp;
721 } else {
722 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000723 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100724 }
725 break;
726
727 default:
Damien George9d181f62014-04-27 16:55:27 +0100728 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100729 }
730 return;
731
732 testlist_comp:
733 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000734 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
735 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
736 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100737 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000738 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000739 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000740 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100741 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000742 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
743 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000744 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100745 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100746 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100747 } else {
748 // sequence with 2 items
749 goto sequence_with_2_items;
750 }
751 } else {
752 // sequence with 2 items
753 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000754 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100755 }
756 return;
757 }
758 return;
759
Damien George9d181f62014-04-27 16:55:27 +0100760 cannot_assign:
761 compile_syntax_error(comp, pn, "can't assign to expression");
762 return;
763
Damien429d7192013-10-04 19:53:11 +0100764 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100765 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100766}
767
Damien George65dc9602015-08-14 12:24:11 +0100768// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100769// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
770// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
771// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100772STATIC 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 +0100773 assert(n_pos_defaults >= 0);
774 assert(n_kw_defaults >= 0);
775
Damien429d7192013-10-04 19:53:11 +0100776 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000777 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100778 int nfree = 0;
779 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000780 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
781 id_info_t *id = &comp->scope_cur->id_info[i];
782 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
783 for (int j = 0; j < this_scope->id_info_len; j++) {
784 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100785 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000786 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000787 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000788 nfree += 1;
789 }
790 }
Damien429d7192013-10-04 19:53:11 +0100791 }
792 }
793 }
Damien429d7192013-10-04 19:53:11 +0100794
795 // make the function/closure
796 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100797 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100798 } else {
Damien George3558f622014-04-20 17:50:40 +0100799 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100800 }
801}
802
Damien George6be0b0a2014-08-15 14:30:52 +0100803STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000804 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100805 comp->have_star = true;
806 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000807 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
808 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100809 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100810 } else {
811 // named star
Damien429d7192013-10-04 19:53:11 +0100812 }
Damien George8b19db02014-04-11 23:25:34 +0100813 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000814
815 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100816 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000817 // TODO do we need to do anything with this?
818
819 } else {
820 mp_parse_node_t pn_id;
821 mp_parse_node_t pn_colon;
822 mp_parse_node_t pn_equal;
823 if (MP_PARSE_NODE_IS_ID(pn)) {
824 // this parameter is just an id
825
826 pn_id = pn;
827 pn_colon = MP_PARSE_NODE_NULL;
828 pn_equal = MP_PARSE_NODE_NULL;
829
Damien George0bb97132015-02-27 14:25:47 +0000830 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000831 // this parameter has a colon and/or equal specifier
832
Damien George0bb97132015-02-27 14:25:47 +0000833 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
834
Damien Georgef41fdd02014-03-03 23:19:11 +0000835 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
836 pn_id = pns->nodes[0];
837 pn_colon = pns->nodes[1];
838 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +0000839 }
840
841 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
842 // this parameter does not have a default value
843
844 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100845 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100846 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000847 return;
848 }
849
850 } else {
851 // this parameter has a default value
852 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
853
Damien George8b19db02014-04-11 23:25:34 +0100854 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000855 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000856 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
857 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100858 // in Micro Python we put the default positional parameters into a tuple using the bytecode
859 // we need to do this here before we start building the map for the default keywords
860 if (comp->num_default_params > 0) {
861 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100862 } else {
863 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100864 }
Damien George69b89d22014-04-11 13:38:30 +0000865 // first default dict param, so make the map
866 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000867 }
Damien Georgef0778a72014-06-07 22:01:00 +0100868
869 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000870 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000871 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000872 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000873 } else {
Damien George69b89d22014-04-11 13:38:30 +0000874 comp->num_default_params += 1;
875 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000876 }
877 }
878
879 // TODO pn_colon not implemented
880 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100881 }
882}
883
884// leaves function object on stack
885// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000886STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100887 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100888 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000889 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100890 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000891 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100892 }
893
894 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +0100895 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +0000896 uint old_num_dict_params = comp->num_dict_params;
897 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +0100898
899 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +0100900 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +0000901 comp->num_dict_params = 0;
902 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +0100903 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000904
Damien Georgea91ac202014-10-05 19:01:34 +0100905 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000906 return MP_QSTR_NULL;
907 }
908
Damien Georgee337f1e2014-03-31 15:18:37 +0100909 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +0100910 // the default keywords args may have already made the tuple; if not, do it now
911 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +0000912 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100913 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +0100914 }
Damien Georgee337f1e2014-03-31 15:18:37 +0100915
Damien429d7192013-10-04 19:53:11 +0100916 // get the scope for this function
917 scope_t *fscope = (scope_t*)pns->nodes[4];
918
919 // make the function
Damien George69b89d22014-04-11 13:38:30 +0000920 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100921
922 // restore variables
Damien George8b19db02014-04-11 23:25:34 +0100923 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +0000924 comp->num_dict_params = old_num_dict_params;
925 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +0100926
927 // return its name (the 'f' in "def f(...):")
928 return fscope->simple_name;
929}
930
931// leaves class object on stack
932// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000933STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100934 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100935 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000936 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100937 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000938 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100939 }
940
941 EMIT(load_build_class);
942
943 // scope for this class
944 scope_t *cscope = (scope_t*)pns->nodes[3];
945
946 // compile the class
947 close_over_variables_etc(comp, cscope, 0, 0);
948
949 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000950 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100951
952 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100953 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
954 mp_parse_node_t parents = pns->nodes[1];
955 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
956 parents = MP_PARSE_NODE_NULL;
957 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000958 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100959 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100960
961 // return its name (the 'C' in class C(...):")
962 return cscope->simple_name;
963}
964
Damien6cdd3af2013-10-05 18:08:26 +0100965// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200966STATIC 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 +0000967 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100968 return false;
969 }
970
971 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100972 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100973 return true;
974 }
975
Damiend99b0522013-12-21 18:17:45 +0000976 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100977 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100978 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100979#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000980 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100981 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000982 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100983 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100984#endif
Damien3ef4abb2013-10-12 16:53:13 +0100985#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000986 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +0100987 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100988#endif
Damien6cdd3af2013-10-05 18:08:26 +0100989 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100990 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100991 }
992
993 return true;
994}
995
Damien George969a6b32014-12-10 22:07:04 +0000996STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100997 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000998 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000999 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001000
Damien6cdd3af2013-10-05 18:08:26 +01001001 // inherit emit options for this function/class definition
1002 uint emit_options = comp->scope_cur->emit_options;
1003
1004 // compile each decorator
1005 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001006 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001007 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1008 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001009
1010 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001011 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001012 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001013
1014 // check for built-in decorators
1015 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1016 // this was a built-in
1017 num_built_in_decorators += 1;
1018
1019 } else {
1020 // not a built-in, compile normally
1021
1022 // compile the decorator function
1023 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001024 for (int j = 1; j < name_len; j++) {
1025 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1026 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001027 }
1028
1029 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001030 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001031 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001032 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001033 compile_node(comp, pns_decorator->nodes[1]);
1034 }
Damien429d7192013-10-04 19:53:11 +01001035 }
1036 }
1037
1038 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001039 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001040 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001041 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001042 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001043 } else {
Damien George0bb97132015-02-27 14:25:47 +00001044 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1045 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001046 }
1047
1048 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001049 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001050 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001051 }
1052
1053 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +00001054 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +01001055}
1056
Damien George969a6b32014-12-10 22:07:04 +00001057STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001058 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001059 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +00001060 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +01001061}
1062
Damien George6be0b0a2014-08-15 14:30:52 +01001063STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001064 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00001065 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001066 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1067 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001068
1069 compile_node(comp, pns->nodes[0]); // base of the power node
1070
Damiend99b0522013-12-21 18:17:45 +00001071 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1072 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1073 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1074 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001075 for (int i = 0; i < n - 1; i++) {
1076 compile_node(comp, pns1->nodes[i]);
1077 }
Damiend99b0522013-12-21 18:17:45 +00001078 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1079 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001080 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001081 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001082 compile_node(comp, pns1->nodes[0]);
1083 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001084 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1085 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001086 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001087 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001088 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001089 }
1090 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001091 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001092 }
1093
Damiend99b0522013-12-21 18:17:45 +00001094 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001095 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001096 }
Damiend99b0522013-12-21 18:17:45 +00001097 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1098 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1099 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1100 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001101 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1102
Damiend99b0522013-12-21 18:17:45 +00001103 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1104 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1105 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001106 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001107 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001108 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001109 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001110 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001111 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001112 c_del_stmt(comp, pns->nodes[0]);
1113 for (int i = 0; i < n; i++) {
1114 c_del_stmt(comp, pns1->nodes[i]);
1115 }
Damiend99b0522013-12-21 18:17:45 +00001116 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001117 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001118 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001119 } else {
1120 // sequence with 2 items
1121 goto sequence_with_2_items;
1122 }
1123 } else {
1124 // sequence with 2 items
1125 sequence_with_2_items:
1126 c_del_stmt(comp, pns->nodes[0]);
1127 c_del_stmt(comp, pns->nodes[1]);
1128 }
1129 } else {
1130 // tuple with 1 element
1131 c_del_stmt(comp, pn);
1132 }
1133 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001134 // TODO is there anything else to implement?
1135 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001136 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001137
1138 return;
1139
1140cannot_delete:
1141 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001142}
1143
Damien George969a6b32014-12-10 22:07:04 +00001144STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001145 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1146}
1147
Damien George969a6b32014-12-10 22:07:04 +00001148STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001149 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001150 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001151 }
Damien George090c9232014-10-17 14:08:49 +00001152 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001153 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001154}
1155
Damien George969a6b32014-12-10 22:07:04 +00001156STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001157 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001158 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001159 }
Damien George090c9232014-10-17 14:08:49 +00001160 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001161 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001162}
1163
Damien George969a6b32014-12-10 22:07:04 +00001164STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001165 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001166 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001167 return;
1168 }
Damiend99b0522013-12-21 18:17:45 +00001169 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001170 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001171 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001172 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001173 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001174 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1175 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 +01001176
Damien George6f355fd2014-04-10 14:11:31 +01001177 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001178 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1179 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1180 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001181 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001182 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1183 } else {
1184 compile_node(comp, pns->nodes[0]);
1185 }
1186 EMIT(return_value);
1187}
1188
Damien George969a6b32014-12-10 22:07:04 +00001189STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001190 compile_node(comp, pns->nodes[0]);
1191 EMIT(pop_top);
1192}
1193
Damien George969a6b32014-12-10 22:07:04 +00001194STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001195 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001196 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001197 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001198 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001199 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001200 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001201 compile_node(comp, pns->nodes[0]);
1202 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001203 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001204 } else {
1205 // raise x
1206 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001207 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001208 }
1209}
1210
Damien George635543c2014-04-10 12:56:52 +01001211// q_base holds the base of the name
1212// eg a -> q_base=a
1213// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001214STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001215 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001216 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1217 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001218 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001219 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001220 pn = pns->nodes[0];
1221 is_as = true;
1222 }
Damien George635543c2014-04-10 12:56:52 +01001223 if (MP_PARSE_NODE_IS_NULL(pn)) {
1224 // empty name (eg, from . import x)
1225 *q_base = MP_QSTR_;
1226 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1227 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001228 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001229 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001230 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001231 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001232 }
Damien George635543c2014-04-10 12:56:52 +01001233 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001234 } else {
1235 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001236 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001237 {
Damien429d7192013-10-04 19:53:11 +01001238 // a name of the form a.b.c
1239 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001240 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001241 }
Damiend99b0522013-12-21 18:17:45 +00001242 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001243 int len = n - 1;
1244 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001245 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001246 }
Damien George55baff42014-01-21 21:40:13 +00001247 byte *q_ptr;
1248 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001249 for (int i = 0; i < n; i++) {
1250 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001251 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001252 }
Damien George39dc1452014-10-03 19:52:22 +01001253 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001254 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001255 memcpy(str_dest, str_src, str_src_len);
1256 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001257 }
Damien George635543c2014-04-10 12:56:52 +01001258 qstr q_full = qstr_build_end(q_ptr);
1259 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001260 if (is_as) {
1261 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001262 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001263 }
1264 }
Damien429d7192013-10-04 19:53:11 +01001265 }
Damien429d7192013-10-04 19:53:11 +01001266 }
1267}
1268
Damien George6be0b0a2014-08-15 14:30:52 +01001269STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001270 EMIT_ARG(load_const_small_int, 0); // level 0 import
1271 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1272 qstr q_base;
1273 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001274 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001275}
1276
Damien George969a6b32014-12-10 22:07:04 +00001277STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001278 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1279}
1280
Damien George969a6b32014-12-10 22:07:04 +00001281STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001282 mp_parse_node_t pn_import_source = pns->nodes[0];
1283
1284 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1285 uint import_level = 0;
1286 do {
1287 mp_parse_node_t pn_rel;
1288 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)) {
1289 // This covers relative imports with dots only like "from .. import"
1290 pn_rel = pn_import_source;
1291 pn_import_source = MP_PARSE_NODE_NULL;
1292 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1293 // This covers relative imports starting with dot(s) like "from .foo import"
1294 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1295 pn_rel = pns_2b->nodes[0];
1296 pn_import_source = pns_2b->nodes[1];
1297 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1298 } else {
1299 // Not a relative import
1300 break;
1301 }
1302
1303 // get the list of . and/or ...'s
1304 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001305 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001306
1307 // count the total number of .'s
1308 for (int i = 0; i < n; i++) {
1309 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1310 import_level++;
1311 } else {
1312 // should be an MP_TOKEN_ELLIPSIS
1313 import_level += 3;
1314 }
1315 }
1316 } while (0);
1317
Damiend99b0522013-12-21 18:17:45 +00001318 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001319 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001320
1321 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001322 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001323 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001324
1325 // do the import
Damien George635543c2014-04-10 12:56:52 +01001326 qstr dummy_q;
1327 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001328 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001329
Damien429d7192013-10-04 19:53:11 +01001330 } else {
Damien George635543c2014-04-10 12:56:52 +01001331 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001332
1333 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001334 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001335 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001336 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001337 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1338 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1339 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001340 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001341 }
Damien Georgeb9791222014-01-23 00:34:21 +00001342 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001343
1344 // do the import
Damien George635543c2014-04-10 12:56:52 +01001345 qstr dummy_q;
1346 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001347 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001348 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1349 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1350 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001351 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001352 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001353 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001354 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001355 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001356 }
1357 }
1358 EMIT(pop_top);
1359 }
1360}
1361
Damien George584ba672014-12-21 17:26:45 +00001362STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1363 bool added;
1364 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001365 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1366 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001367 return;
1368 }
1369 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1370
1371 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1372 id_info = scope_find_global(comp->scope_cur, qst);
1373 if (id_info != NULL) {
1374 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1375 }
1376}
1377
Damien George969a6b32014-12-10 22:07:04 +00001378STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001379 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001380 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001381 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001382 for (int i = 0; i < n; i++) {
1383 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001384 }
1385 }
1386}
1387
Damien George584ba672014-12-21 17:26:45 +00001388STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1389 bool added;
1390 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001391 if (!added && id_info->kind != ID_INFO_KIND_FREE) {
1392 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001393 return;
1394 }
1395 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1396 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)) {
1397 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1398 return;
1399 }
1400 id_info->kind = ID_INFO_KIND_FREE;
1401 scope_close_over_in_parents(comp->scope_cur, qst);
1402}
1403
Damien George969a6b32014-12-10 22:07:04 +00001404STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001405 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001406 if (comp->scope_cur->kind == SCOPE_MODULE) {
1407 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1408 return;
1409 }
1410 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001411 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001412 for (int i = 0; i < n; i++) {
1413 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001414 }
1415 }
1416}
1417
Damien George969a6b32014-12-10 22:07:04 +00001418STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001419 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001420 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001421 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001422 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001423 // assertion message
1424 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001425 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001426 }
Damien Georgeb9791222014-01-23 00:34:21 +00001427 EMIT_ARG(raise_varargs, 1);
1428 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001429}
1430
Damien George969a6b32014-12-10 22:07:04 +00001431STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001432 // TODO proper and/or short circuiting
1433
Damien George6f355fd2014-04-10 14:11:31 +01001434 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001435
Damien George65dc9602015-08-14 12:24:11 +01001436 // optimisation: don't emit anything when "if False"
1437 if (!node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001438 uint l_fail = comp_next_label(comp);
1439 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001440
Damien George391db862014-10-17 17:57:33 +00001441 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001442
Damien George65dc9602015-08-14 12:24:11 +01001443 // optimisation: skip everything else when "if True"
1444 if (node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001445 goto done;
1446 }
1447
1448 if (
Damien George65dc9602015-08-14 12:24:11 +01001449 // optimisation: don't jump over non-existent elif/else blocks
1450 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001451 // optimisation: don't jump if last instruction was return
1452 && !EMIT(last_emit_was_return_value)
1453 ) {
1454 // jump over elif/else blocks
1455 EMIT_ARG(jump, l_end);
1456 }
1457
1458 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001459 }
1460
Damien George235f9b32014-10-17 17:30:16 +00001461 // compile elif blocks (if any)
1462 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001463 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 +00001464 for (int i = 0; i < n_elif; i++) {
1465 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1466 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001467
Damien George65dc9602015-08-14 12:24:11 +01001468 // optimisation: don't emit anything when "if False"
1469 if (!node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001470 uint l_fail = comp_next_label(comp);
1471 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001472
Damien George391db862014-10-17 17:57:33 +00001473 compile_node(comp, pns_elif->nodes[1]); // elif block
1474
Damien George65dc9602015-08-14 12:24:11 +01001475 // optimisation: skip everything else when "elif True"
1476 if (node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001477 goto done;
1478 }
1479
1480 // optimisation: don't jump if last instruction was return
1481 if (!EMIT(last_emit_was_return_value)) {
1482 EMIT_ARG(jump, l_end);
1483 }
1484 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001485 }
1486 }
1487
1488 // compile else block
1489 compile_node(comp, pns->nodes[3]); // can be null
1490
Damien George391db862014-10-17 17:57:33 +00001491done:
Damien Georgeb9791222014-01-23 00:34:21 +00001492 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001493}
1494
Damien Georgecbddb272014-02-01 20:08:18 +00001495#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001496 uint16_t old_break_label = comp->break_label; \
1497 uint16_t old_continue_label = comp->continue_label; \
1498 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001499 uint break_label = comp_next_label(comp); \
1500 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001501 comp->break_label = break_label; \
1502 comp->continue_label = continue_label; \
1503 comp->break_continue_except_level = comp->cur_except_level;
1504
1505#define END_BREAK_CONTINUE_BLOCK \
1506 comp->break_label = old_break_label; \
1507 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001508 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001509
Damien George969a6b32014-12-10 22:07:04 +00001510STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001511 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001512
Damien George391db862014-10-17 17:57:33 +00001513 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1514 uint top_label = comp_next_label(comp);
1515 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1516 EMIT_ARG(jump, continue_label);
1517 }
1518 EMIT_ARG(label_assign, top_label);
1519 compile_node(comp, pns->nodes[1]); // body
1520 EMIT_ARG(label_assign, continue_label);
1521 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1522 }
Damience89a212013-10-15 22:25:17 +01001523
1524 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001525 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001526
1527 compile_node(comp, pns->nodes[2]); // else
1528
Damien Georgeb9791222014-01-23 00:34:21 +00001529 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001530}
1531
Damien Georgee181c0d2014-12-12 17:19:56 +00001532// This function compiles an optimised for-loop of the form:
1533// for <var> in range(<start>, <end>, <step>):
1534// <body>
1535// else:
1536// <else>
1537// <var> must be an identifier and <step> must be a small-int.
1538//
1539// Semantics of for-loop require:
1540// - final failing value should not be stored in the loop variable
1541// - if the loop never runs, the loop variable should never be assigned
1542// - assignments to <var>, <end> or <step> in the body do not alter the loop
1543// (<step> is a constant for us, so no need to worry about it changing)
1544//
1545// If <end> is a small-int, then the stack during the for-loop contains just
1546// the current value of <var>. Otherwise, the stack contains <end> then the
1547// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001548STATIC 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 +00001549 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001550
Damien George6f355fd2014-04-10 14:11:31 +01001551 uint top_label = comp_next_label(comp);
1552 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001553
Damien Georgee181c0d2014-12-12 17:19:56 +00001554 // put the end value on the stack if it's not a small-int constant
1555 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1556 if (end_on_stack) {
1557 compile_node(comp, pn_end);
1558 }
1559
1560 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001561 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001562
Damien Georgeb9791222014-01-23 00:34:21 +00001563 EMIT_ARG(jump, entry_label);
1564 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001565
Damien Georgec33ce602014-12-11 17:35:23 +00001566 // duplicate next value and store it to var
1567 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001568 c_assign(comp, pn_var, ASSIGN_STORE);
1569
Damienf3822fc2013-11-09 20:12:03 +00001570 // compile body
1571 compile_node(comp, pn_body);
1572
Damien Georgeb9791222014-01-23 00:34:21 +00001573 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001574
Damien Georgee181c0d2014-12-12 17:19:56 +00001575 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001576 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001577 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001578
Damien Georgeb9791222014-01-23 00:34:21 +00001579 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001580
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001581 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001582 if (end_on_stack) {
1583 EMIT(dup_top_two);
1584 EMIT(rot_two);
1585 } else {
1586 EMIT(dup_top);
1587 compile_node(comp, pn_end);
1588 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001589 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1590 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001591 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001592 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001593 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001594 }
Damien George63f38322015-02-28 15:04:06 +00001595 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001596
1597 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001598 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001599
1600 compile_node(comp, pn_else);
1601
Damien Georgeb9791222014-01-23 00:34:21 +00001602 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001603
1604 // discard final value of var that failed the loop condition
1605 EMIT(pop_top);
1606
1607 // discard <end> value if it's on the stack
1608 if (end_on_stack) {
1609 EMIT(pop_top);
1610 }
Damienf72fd0e2013-11-06 20:20:49 +00001611}
1612
Damien George969a6b32014-12-10 22:07:04 +00001613STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001614 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1615 // this is actually slower, but uses no heap memory
1616 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001617 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 +00001618 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001619 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1620 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1621 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1622 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001623 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1624 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001625 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001626 mp_parse_node_t pn_range_start;
1627 mp_parse_node_t pn_range_end;
1628 mp_parse_node_t pn_range_step;
1629 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001630 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001631 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001632 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001633 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001634 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001635 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001636 } else if (n_args == 2) {
1637 pn_range_start = args[0];
1638 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001639 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001640 } else {
1641 pn_range_start = args[0];
1642 pn_range_end = args[1];
1643 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001644 // We need to know sign of step. This is possible only if it's constant
1645 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1646 optimize = false;
1647 }
Damienf72fd0e2013-11-06 20:20:49 +00001648 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001649 }
1650 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001651 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1652 return;
1653 }
1654 }
1655 }
Damienf72fd0e2013-11-06 20:20:49 +00001656
Damien Georgecbddb272014-02-01 20:08:18 +00001657 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001658 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001659
Damien George6f355fd2014-04-10 14:11:31 +01001660 uint pop_label = comp_next_label(comp);
1661 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001662
Damien429d7192013-10-04 19:53:11 +01001663 compile_node(comp, pns->nodes[1]); // iterator
1664 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001665 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001666 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001667 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1668 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001669 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001670 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001671 }
Damien Georgeb9791222014-01-23 00:34:21 +00001672 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001673 EMIT(for_iter_end);
1674
1675 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001676 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001677
Damien429d7192013-10-04 19:53:11 +01001678 compile_node(comp, pns->nodes[3]); // else (not tested)
1679
Damien Georgeb9791222014-01-23 00:34:21 +00001680 EMIT_ARG(label_assign, break_label);
1681 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001682}
1683
Damien George6be0b0a2014-08-15 14:30:52 +01001684STATIC 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 +01001685 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001686 uint l1 = comp_next_label(comp);
1687 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001688
Damien Georgeb9791222014-01-23 00:34:21 +00001689 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001690 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001691
Damien429d7192013-10-04 19:53:11 +01001692 compile_node(comp, pn_body); // body
1693 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001694 EMIT_ARG(jump, success_label); // jump over exception handler
1695
1696 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001697 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001698
Damien George6f355fd2014-04-10 14:11:31 +01001699 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001700
1701 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001702 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1703 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001704
1705 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001706 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001707
Damiend99b0522013-12-21 18:17:45 +00001708 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001709 // this is a catch all exception handler
1710 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001711 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001712 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001713 return;
1714 }
1715 } else {
1716 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001717 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1718 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1719 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1720 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001721 // handler binds the exception to a local
1722 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001723 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001724 }
1725 }
1726 EMIT(dup_top);
1727 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001728 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001729 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001730 }
1731
1732 EMIT(pop_top);
1733
1734 if (qstr_exception_local == 0) {
1735 EMIT(pop_top);
1736 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001737 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001738 }
1739
1740 EMIT(pop_top);
1741
Damien George6f355fd2014-04-10 14:11:31 +01001742 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001743 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001744 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001745 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001746 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001747 }
1748 compile_node(comp, pns_except->nodes[1]);
1749 if (qstr_exception_local != 0) {
1750 EMIT(pop_block);
1751 }
1752 EMIT(pop_except);
1753 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001754 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1755 EMIT_ARG(label_assign, l3);
1756 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001757 compile_store_id(comp, qstr_exception_local);
1758 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001759
Damien George8dcc0c72014-03-27 10:55:21 +00001760 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001761 EMIT(end_finally);
1762 }
Damien Georgeb9791222014-01-23 00:34:21 +00001763 EMIT_ARG(jump, l2);
1764 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001765 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001766 }
1767
Damien George8dcc0c72014-03-27 10:55:21 +00001768 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001769 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001770 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001771
Damien Georgeb9791222014-01-23 00:34:21 +00001772 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001773 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001774 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001775}
1776
Damien George6be0b0a2014-08-15 14:30:52 +01001777STATIC 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 +01001778 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001779
Damien Georgeb9791222014-01-23 00:34:21 +00001780 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001781 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001782
Damien429d7192013-10-04 19:53:11 +01001783 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001784 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001785 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001786 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001787 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001788 } else {
1789 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1790 }
1791 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001792 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1793 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001794 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001795
Damien George8dcc0c72014-03-27 10:55:21 +00001796 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001797 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001798}
1799
Damien George969a6b32014-12-10 22:07:04 +00001800STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001801 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1802 {
Damiend99b0522013-12-21 18:17:45 +00001803 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1804 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001805 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001806 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1807 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001808 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001809 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001810 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001811 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001812 // no finally
1813 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1814 } else {
1815 // have finally
Damiend99b0522013-12-21 18:17:45 +00001816 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 +01001817 }
1818 } else {
1819 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001820 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001821 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001822 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001823 }
Damien429d7192013-10-04 19:53:11 +01001824 }
1825}
1826
Damien George6be0b0a2014-08-15 14:30:52 +01001827STATIC 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 +01001828 if (n == 0) {
1829 // no more pre-bits, compile the body of the with
1830 compile_node(comp, body);
1831 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001832 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001833 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001834 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001835 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001836 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001837 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001838 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1839 } else {
1840 // this pre-bit is just an expression
1841 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001842 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001843 EMIT(pop_top);
1844 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001845 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001846 // compile additional pre-bits and the body
1847 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1848 // finish this with block
1849 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001850 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1851 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001852 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001853 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001854 EMIT(end_finally);
1855 }
1856}
1857
Damien George969a6b32014-12-10 22:07:04 +00001858STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001859 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001860 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001861 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001862 assert(n > 0);
1863
1864 // compile in a nested fashion
1865 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1866}
1867
Damien George969a6b32014-12-10 22:07:04 +00001868STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001869 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001870 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1871 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001872 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001873 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001874 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001875 EMIT(pop_top);
1876
Damien429d7192013-10-04 19:53:11 +01001877 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001878 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001879 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001880 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001881 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1882 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001883 // do nothing with a lonely constant
1884 } else {
1885 compile_node(comp, pns->nodes[0]); // just an expression
1886 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1887 }
Damien429d7192013-10-04 19:53:11 +01001888 }
Damien Georgeb948de32015-10-08 14:26:01 +01001889 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
Damiend99b0522013-12-21 18:17:45 +00001890 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1891 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001892 if (kind == PN_expr_stmt_augassign) {
1893 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1894 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001895 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001896 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001897 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001898 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1899 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1900 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1901 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1902 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1903 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1904 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1905 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1906 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1907 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1908 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001909 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001910 }
Damien George7e5fb242014-02-01 22:18:47 +00001911 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001912 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1913 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001914 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
Damien Georgeb948de32015-10-08 14:26:01 +01001915 compile_node(comp, pns1->nodes[rhs]); // rhs
Damien429d7192013-10-04 19:53:11 +01001916 // following CPython, we store left-most first
1917 if (rhs > 0) {
1918 EMIT(dup_top);
1919 }
1920 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1921 for (int i = 0; i < rhs; i++) {
1922 if (i + 1 < rhs) {
1923 EMIT(dup_top);
1924 }
Damien Georgeb948de32015-10-08 14:26:01 +01001925 c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001926 }
Damien George0bb97132015-02-27 14:25:47 +00001927 } else {
Damien Georgeb948de32015-10-08 14:26:01 +01001928 plain_assign:
Damien George42e0c592015-03-14 13:11:35 +00001929 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001930 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001931 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001932 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2
Damiend99b0522013-12-21 18:17:45 +00001933 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001934 // optimisation for a, b = c, d
Damien Georgeb948de32015-10-08 14:26:01 +01001935 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001936 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001937 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1938 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1939 // can't optimise when it's a star expression on the lhs
1940 goto no_optimisation;
1941 }
Damien429d7192013-10-04 19:53:11 +01001942 compile_node(comp, pns10->nodes[0]); // rhs
1943 compile_node(comp, pns10->nodes[1]); // rhs
1944 EMIT(rot_two);
1945 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1946 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001947 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001948 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001949 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001950 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3
Damiend99b0522013-12-21 18:17:45 +00001951 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001952 // optimisation for a, b, c = d, e, f
Damien Georgeb948de32015-10-08 14:26:01 +01001953 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001954 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001955 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1956 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1957 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1958 // can't optimise when it's a star expression on the lhs
1959 goto no_optimisation;
1960 }
Damien429d7192013-10-04 19:53:11 +01001961 compile_node(comp, pns10->nodes[0]); // rhs
1962 compile_node(comp, pns10->nodes[1]); // rhs
1963 compile_node(comp, pns10->nodes[2]); // rhs
1964 EMIT(rot_three);
1965 EMIT(rot_two);
1966 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1967 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1968 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1969 } else {
Damien George495d7812014-04-08 17:51:47 +01001970 no_optimisation:
Damien Georgeb948de32015-10-08 14:26:01 +01001971 compile_node(comp, pns->nodes[1]); // rhs
Damien429d7192013-10-04 19:53:11 +01001972 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1973 }
Damien429d7192013-10-04 19:53:11 +01001974 }
Damien Georgeb948de32015-10-08 14:26:01 +01001975 } else {
1976 goto plain_assign;
Damien429d7192013-10-04 19:53:11 +01001977 }
1978}
1979
Damien George6be0b0a2014-08-15 14:30:52 +01001980STATIC 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 +00001981 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001982 compile_node(comp, pns->nodes[0]);
1983 for (int i = 1; i < num_nodes; i += 1) {
1984 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001985 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001986 }
1987}
1988
Damien George969a6b32014-12-10 22:07:04 +00001989STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001990 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1991 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001992
Damien George6f355fd2014-04-10 14:11:31 +01001993 uint l_fail = comp_next_label(comp);
1994 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001995 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1996 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001997 EMIT_ARG(jump, l_end);
1998 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001999 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002000 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002001 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002002}
2003
Damien George969a6b32014-12-10 22:07:04 +00002004STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002005 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002006 //mp_parse_node_t pn_params = pns->nodes[0];
2007 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002008
Damien George36db6bc2014-05-07 17:24:22 +01002009 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002010 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002011 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 +01002012 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002013 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002014 }
2015
2016 // get the scope for this lambda
2017 scope_t *this_scope = (scope_t*)pns->nodes[2];
2018
2019 // make the lambda
2020 close_over_variables_etc(comp, this_scope, 0, 0);
2021}
2022
Damien George7711afb2015-02-28 15:10:18 +00002023STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002024 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002025 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002026 for (int i = 0; i < n; i += 1) {
2027 compile_node(comp, pns->nodes[i]);
2028 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002029 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002030 }
2031 }
Damien Georgeb9791222014-01-23 00:34:21 +00002032 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002033}
2034
Damien George7711afb2015-02-28 15:10:18 +00002035STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2036 compile_or_and_test(comp, pns, true);
2037}
2038
Damien George969a6b32014-12-10 22:07:04 +00002039STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002040 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002041}
2042
Damien George969a6b32014-12-10 22:07:04 +00002043STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002044 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002045 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002046}
2047
Damien George969a6b32014-12-10 22:07:04 +00002048STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002049 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002050 compile_node(comp, pns->nodes[0]);
2051 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002052 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002053 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002054 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002055 }
2056 for (int i = 1; i + 1 < num_nodes; i += 2) {
2057 compile_node(comp, pns->nodes[i + 1]);
2058 if (i + 2 < num_nodes) {
2059 EMIT(dup_top);
2060 EMIT(rot_three);
2061 }
Damien George7e5fb242014-02-01 22:18:47 +00002062 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002063 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002064 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002065 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2066 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2067 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2068 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2069 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2070 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002071 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002072 }
2073 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002074 } else {
2075 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002076 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2077 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002078 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002079 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002080 } else {
2081 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002082 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002083 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002084 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002085 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002086 }
Damien429d7192013-10-04 19:53:11 +01002087 }
Damien429d7192013-10-04 19:53:11 +01002088 }
2089 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002090 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002091 }
2092 }
2093 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002094 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002095 EMIT_ARG(jump, l_end);
2096 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002097 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002098 EMIT(rot_two);
2099 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002100 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002101 }
2102}
2103
Damien George969a6b32014-12-10 22:07:04 +00002104STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002105 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002106}
2107
Damien George969a6b32014-12-10 22:07:04 +00002108STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002109 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002110}
2111
Damien George969a6b32014-12-10 22:07:04 +00002112STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002113 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002114}
2115
Damien George969a6b32014-12-10 22:07:04 +00002116STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002117 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002118}
2119
Damien George969a6b32014-12-10 22:07:04 +00002120STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002121 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002122 compile_node(comp, pns->nodes[0]);
2123 for (int i = 1; i + 1 < num_nodes; i += 2) {
2124 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002125 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002126 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002127 } else {
Damien George0bb97132015-02-27 14:25:47 +00002128 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2129 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002130 }
2131 }
2132}
2133
Damien George969a6b32014-12-10 22:07:04 +00002134STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002135 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002136 compile_node(comp, pns->nodes[0]);
2137 for (int i = 1; i + 1 < num_nodes; i += 2) {
2138 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002139 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002140 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002141 } else {
Damien George0bb97132015-02-27 14:25:47 +00002142 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2143 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002144 }
2145 }
2146}
2147
Damien George969a6b32014-12-10 22:07:04 +00002148STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002149 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002150 compile_node(comp, pns->nodes[0]);
2151 for (int i = 1; i + 1 < num_nodes; i += 2) {
2152 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002153 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002154 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002155 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002156 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002157 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002158 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002159 } else {
Damien George0bb97132015-02-27 14:25:47 +00002160 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2161 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002162 }
2163 }
2164}
2165
Damien George969a6b32014-12-10 22:07:04 +00002166STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002167 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002168 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002169 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002170 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002171 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002172 } else {
Damien George0bb97132015-02-27 14:25:47 +00002173 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2174 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002175 }
2176}
2177
Damien George969a6b32014-12-10 22:07:04 +00002178STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002179 // this is to handle special super() call
2180 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2181
2182 compile_generic_all_nodes(comp, pns);
2183}
2184
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002185STATIC 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 +01002186 // function to call is on top of stack
2187
Damien George35e2a4e2014-02-05 00:51:47 +00002188 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002189 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 +00002190 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002191 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002192 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002193 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002194 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002195 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002196 EMIT_ARG(call_function, 2, 0, 0);
2197 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002198 }
2199 }
Damien George01039b52014-12-21 17:44:27 +00002200 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002201 return;
2202 }
Damien George35e2a4e2014-02-05 00:51:47 +00002203
Damien George2c0842b2014-04-27 16:46:51 +01002204 // get the list of arguments
2205 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002206 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002207
Damien George2c0842b2014-04-27 16:46:51 +01002208 // compile the arguments
2209 // Rather than calling compile_node on the list, we go through the list of args
2210 // explicitly here so that we can count the number of arguments and give sensible
2211 // error messages.
2212 int n_positional = n_positional_extra;
2213 uint n_keyword = 0;
2214 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002215 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002216 for (int i = 0; i < n_args; i++) {
2217 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2218 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2219 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2220 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2221 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2222 return;
2223 }
2224 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002225 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002226 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2227 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2228 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2229 return;
2230 }
2231 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002232 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002233 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002234 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002235 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2236 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2237 return;
2238 }
Damien George59fba2d2015-06-25 14:42:13 +00002239 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002240 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002241 n_keyword += 1;
2242 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002243 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2244 n_positional++;
2245 }
2246 } else {
2247 goto normal_argument;
2248 }
2249 } else {
2250 normal_argument:
2251 if (n_keyword > 0) {
2252 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2253 return;
2254 }
2255 compile_node(comp, args[i]);
2256 n_positional++;
2257 }
Damien429d7192013-10-04 19:53:11 +01002258 }
2259
Delio Brignolie6978a42015-09-17 12:07:06 +02002260 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002261 // if we had one but not the other then we load "null" as a place holder
2262 if (star_flags != 0) {
2263 if (star_args_node == NULL) {
2264 EMIT(load_null);
2265 } else {
2266 compile_node(comp, star_args_node->nodes[0]);
2267 }
2268 if (dblstar_args_node == NULL) {
2269 EMIT(load_null);
2270 } else {
2271 compile_node(comp, dblstar_args_node->nodes[0]);
2272 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002273 }
2274
Damien George2c0842b2014-04-27 16:46:51 +01002275 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002276 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002277 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002278 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002279 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002280 }
Damien429d7192013-10-04 19:53:11 +01002281}
2282
Damien George969a6b32014-12-10 22:07:04 +00002283STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002284 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002285 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002286 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 +01002287 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002288 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2289 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002290 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002291 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002292 i += 1;
2293 } else {
2294 compile_node(comp, pns->nodes[i]);
2295 }
Damien George35e2a4e2014-02-05 00:51:47 +00002296 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002297 }
2298}
2299
Damien George969a6b32014-12-10 22:07:04 +00002300STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002301 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002302 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002303}
2304
Damien George969a6b32014-12-10 22:07:04 +00002305STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002306 // a list of strings
Damien63321742013-12-10 17:41:49 +00002307
2308 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002309 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002310 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002311 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002312 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002313 int pn_kind;
2314 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2315 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2316 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2317 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2318 } else {
2319 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2320 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002321 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2322 pn_kind = MP_PARSE_NODE_STRING;
2323 } else {
2324 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2325 pn_kind = MP_PARSE_NODE_BYTES;
2326 }
Damien George40f3c022014-07-03 13:25:24 +01002327 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002328 }
Damien63321742013-12-10 17:41:49 +00002329 if (i == 0) {
2330 string_kind = pn_kind;
2331 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002332 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002333 return;
2334 }
Damien429d7192013-10-04 19:53:11 +01002335 }
Damien63321742013-12-10 17:41:49 +00002336
Damien George65ef6b72015-01-14 21:17:27 +00002337 // if we are not in the last pass, just load a dummy object
2338 if (comp->pass != MP_PASS_EMIT) {
2339 EMIT_ARG(load_const_obj, mp_const_none);
2340 return;
2341 }
2342
Damien63321742013-12-10 17:41:49 +00002343 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002344 vstr_t vstr;
2345 vstr_init_len(&vstr, n_bytes);
2346 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002347 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002348 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002349 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002350 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2351 memcpy(s_dest, s, s_len);
2352 s_dest += s_len;
2353 } else {
2354 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002355 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2356 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002357 }
Damien63321742013-12-10 17:41:49 +00002358 }
Damien George65ef6b72015-01-14 21:17:27 +00002359
2360 // load the object
Damien George05005f62015-01-21 22:48:37 +00002361 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 +01002362}
2363
2364// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002365STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002366 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2367 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2368 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002369
Damien George36db6bc2014-05-07 17:24:22 +01002370 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002371 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002372 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 +01002373 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002374 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002375 }
2376
2377 // get the scope for this comprehension
2378 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2379
2380 // compile the comprehension
2381 close_over_variables_etc(comp, this_scope, 0, 0);
2382
2383 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2384 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002385 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002386}
2387
Damien George969a6b32014-12-10 22:07:04 +00002388STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002389 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002390 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002391 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2392 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2393 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2394 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2395 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2396 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2397 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002398 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002399 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002400 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002401 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002402 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002403 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002404 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002405 // generator expression
2406 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2407 } else {
2408 // tuple with 2 items
2409 goto tuple_with_2_items;
2410 }
2411 } else {
2412 // tuple with 2 items
2413 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002414 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002415 }
2416 } else {
2417 // parenthesis around a single item, is just that item
2418 compile_node(comp, pns->nodes[0]);
2419 }
2420}
2421
Damien George969a6b32014-12-10 22:07:04 +00002422STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002423 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002424 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002425 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002426 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2427 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2428 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2429 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2430 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002431 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002432 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002433 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002434 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002435 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002436 // list of many items
2437 compile_node(comp, pns2->nodes[0]);
2438 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002439 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002440 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002441 // list comprehension
2442 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2443 } else {
2444 // list with 2 items
2445 goto list_with_2_items;
2446 }
2447 } else {
2448 // list with 2 items
2449 list_with_2_items:
2450 compile_node(comp, pns2->nodes[0]);
2451 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002452 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002453 }
2454 } else {
2455 // list with 1 item
2456 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002457 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002458 }
2459}
2460
Damien George969a6b32014-12-10 22:07:04 +00002461STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002462 mp_parse_node_t pn = pns->nodes[0];
2463 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002464 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002465 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002466 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2467 pns = (mp_parse_node_struct_t*)pn;
2468 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002469 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002470 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002471 compile_node(comp, pn);
2472 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002473 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2474 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2475 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2476 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002477 // dict/set with multiple elements
2478
2479 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002480 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002481 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002482
2483 // first element sets whether it's a dict or set
2484 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002485 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002486 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002487 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002488 compile_node(comp, pns->nodes[0]);
2489 EMIT(store_map);
2490 is_dict = true;
2491 } else {
2492 // a set
2493 compile_node(comp, pns->nodes[0]); // 1st value of set
2494 is_dict = false;
2495 }
2496
2497 // process rest of elements
2498 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002499 mp_parse_node_t pn_i = nodes[i];
2500 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2501 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002502 if (is_dict) {
2503 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002504 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002505 return;
2506 }
2507 EMIT(store_map);
2508 } else {
2509 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002510 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002511 return;
2512 }
2513 }
2514 }
2515
Damien Georgee37dcaa2014-12-27 17:07:16 +00002516 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002517 // if it's a set, build it
2518 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002519 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002520 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002521 #endif
Damien George0bb97132015-02-27 14:25:47 +00002522 } else {
2523 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002524 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002525 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002526 // a dictionary comprehension
2527 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2528 } else {
2529 // a set comprehension
2530 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2531 }
Damien429d7192013-10-04 19:53:11 +01002532 }
2533 } else {
2534 // set with one element
2535 goto set_with_one_element;
2536 }
2537 } else {
2538 // set with one element
2539 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002540 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002541 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002542 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002543 #else
2544 assert(0);
2545 #endif
Damien429d7192013-10-04 19:53:11 +01002546 }
2547}
2548
Damien George969a6b32014-12-10 22:07:04 +00002549STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002550 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002551}
2552
Damien George969a6b32014-12-10 22:07:04 +00002553STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002554 // object who's index we want is on top of stack
2555 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002556 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002557}
2558
Damien George969a6b32014-12-10 22:07:04 +00002559STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002560 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002561 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002562}
2563
Damien George83204f32014-12-27 17:20:41 +00002564#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002565STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002566 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2567 mp_parse_node_t pn = pns->nodes[0];
2568 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002569 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002570 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2571 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002572 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2573 pns = (mp_parse_node_struct_t*)pn;
2574 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002575 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002576 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002577 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002578 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002579 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002580 } else {
2581 // [?::x]
2582 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002583 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002584 }
Damiend99b0522013-12-21 18:17:45 +00002585 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002586 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002587 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2588 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2589 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2590 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002591 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002592 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002593 } else {
2594 // [?:x:x]
2595 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002596 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002597 }
2598 } else {
2599 // [?:x]
2600 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002601 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002602 }
2603 } else {
2604 // [?:x]
2605 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002606 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002607 }
2608}
2609
Damien George969a6b32014-12-10 22:07:04 +00002610STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002611 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002612 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2613 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002614}
2615
Damien George969a6b32014-12-10 22:07:04 +00002616STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002617 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002618 compile_subscript_3_helper(comp, pns);
2619}
Damien George83204f32014-12-27 17:20:41 +00002620#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002621
Damien George969a6b32014-12-10 22:07:04 +00002622STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002623 // if this is called then we are compiling a dict key:value pair
2624 compile_node(comp, pns->nodes[1]); // value
2625 compile_node(comp, pns->nodes[0]); // key
2626}
2627
Damien George969a6b32014-12-10 22:07:04 +00002628STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002629 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002630 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002631 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002632}
2633
Damien George969a6b32014-12-10 22:07:04 +00002634STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002635 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002636 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002637 return;
2638 }
Damiend99b0522013-12-21 18:17:45 +00002639 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
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_value);
Damiend99b0522013-12-21 18:17:45 +00002642 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2643 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002644 compile_node(comp, pns->nodes[0]);
2645 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002646 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002647 EMIT(yield_from);
2648 } else {
2649 compile_node(comp, pns->nodes[0]);
2650 EMIT(yield_value);
2651 }
2652}
2653
Damien George65ef6b72015-01-14 21:17:27 +00002654STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2655 // only create and load the actual str object on the last pass
2656 if (comp->pass != MP_PASS_EMIT) {
2657 EMIT_ARG(load_const_obj, mp_const_none);
2658 } else {
2659 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2660 }
2661}
2662
2663STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2664 // only create and load the actual bytes object on the last pass
2665 if (comp->pass != MP_PASS_EMIT) {
2666 EMIT_ARG(load_const_obj, mp_const_none);
2667 } else {
2668 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2669 }
2670}
2671
Damien George7d414a12015-02-08 01:57:40 +00002672STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2673 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2674}
2675
Damiend99b0522013-12-21 18:17:45 +00002676typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002677STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002678#define nc NULL
2679#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002680#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002681#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002682#undef nc
2683#undef c
2684#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002685 NULL,
2686 compile_string,
2687 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002688 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002689};
2690
Damien George6be0b0a2014-08-15 14:30:52 +01002691STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002692 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002693 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002694 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002695 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002696 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002697 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002698 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002699 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002700 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002701 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2702 case MP_PARSE_NODE_BYTES:
2703 // only create and load the actual bytes object on the last pass
2704 if (comp->pass != MP_PASS_EMIT) {
2705 EMIT_ARG(load_const_obj, mp_const_none);
2706 } else {
2707 mp_uint_t len;
2708 const byte *data = qstr_data(arg, &len);
2709 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2710 }
2711 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002712 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002713 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002714 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002715 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002716 // do nothing
2717 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002718 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002719 }
2720 break;
Damien429d7192013-10-04 19:53:11 +01002721 }
2722 } else {
Damiend99b0522013-12-21 18:17:45 +00002723 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002724 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002725 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2726 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002727#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002728 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2729 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002730#endif
Damien George65ef6b72015-01-14 21:17:27 +00002731 compile_syntax_error(comp, pn, "internal compiler error");
2732 } else {
2733 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002734 }
2735 }
2736}
2737
Damien George2ac4af62014-08-15 16:45:41 +01002738STATIC 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 +01002739 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002740 qstr param_name = MP_QSTR_NULL;
2741 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002742 if (MP_PARSE_NODE_IS_ID(pn)) {
2743 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002744 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002745 // comes after a star, so counts as a keyword-only parameter
2746 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002747 } else {
Damien George2827d622014-04-27 15:50:52 +01002748 // comes before a star, so counts as a positional parameter
2749 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002750 }
Damienb14de212013-10-06 00:28:28 +01002751 } else {
Damiend99b0522013-12-21 18:17:45 +00002752 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2753 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2754 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2755 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002756 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002757 // comes after a star, so counts as a keyword-only parameter
2758 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002759 } else {
Damien George2827d622014-04-27 15:50:52 +01002760 // comes before a star, so counts as a positional parameter
2761 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002762 }
Damiend99b0522013-12-21 18:17:45 +00002763 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002764 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002765 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002766 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002767 // bare star
2768 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002769 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002770 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002771 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002772 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002773 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002774 } else {
2775 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002776 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002777 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002778 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2779 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002780 }
Damien George0bb97132015-02-27 14:25:47 +00002781 } else {
2782 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002783 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002784 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002785 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002786 }
Damien429d7192013-10-04 19:53:11 +01002787 }
2788
Damien George2827d622014-04-27 15:50:52 +01002789 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002790 bool added;
2791 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2792 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002793 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002794 return;
2795 }
Damien429d7192013-10-04 19:53:11 +01002796 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002797 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002798 }
2799}
2800
Damien George2827d622014-04-27 15:50:52 +01002801STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002802 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002803}
2804
Damien George2827d622014-04-27 15:50:52 +01002805STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002806 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2807}
2808
Damien Georgeea5b59b2015-09-04 16:44:14 +01002809#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002810STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2811 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2812 // no annotation
2813 return;
2814 }
2815
2816 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2817 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2818 // named parameter with possible annotation
2819 // fallthrough
2820 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2821 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2822 // named star with possible annotation
2823 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2824 // fallthrough
2825 } else {
2826 // no annotation
2827 return;
2828 }
2829 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
2830 // double star with possible annotation
2831 // fallthrough
2832 } else {
2833 // no annotation
2834 return;
2835 }
2836
2837 mp_parse_node_t pn_annotation = pns->nodes[1];
2838
2839 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002840 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2841 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2842 assert(id_info != NULL);
2843
Damien Georgeea5b59b2015-09-04 16:44:14 +01002844 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2845 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2846 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2847 } else {
2848 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002849 }
Damien George2ac4af62014-08-15 16:45:41 +01002850 }
Damien429d7192013-10-04 19:53:11 +01002851}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002852#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002853
Damien George6be0b0a2014-08-15 14:30:52 +01002854STATIC 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 +01002855 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002856 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002857 // no more nested if/for; compile inner expression
2858 compile_node(comp, pn_inner_expr);
2859 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002861 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002862 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002863 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002864 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002865 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002866 #endif
Damien429d7192013-10-04 19:53:11 +01002867 } else {
2868 EMIT(yield_value);
2869 EMIT(pop_top);
2870 }
Damiend99b0522013-12-21 18:17:45 +00002871 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002872 // if condition
Damiend99b0522013-12-21 18:17:45 +00002873 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002874 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2875 pn_iter = pns_comp_if->nodes[1];
2876 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002877 } else {
2878 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002879 // for loop
Damiend99b0522013-12-21 18:17:45 +00002880 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002881 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002882 uint l_end2 = comp_next_label(comp);
2883 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002884 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002885 EMIT_ARG(label_assign, l_top2);
2886 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002887 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2888 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 +00002889 EMIT_ARG(jump, l_top2);
2890 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002891 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002892 }
2893}
2894
Damien George1463c1f2014-04-25 23:52:57 +01002895STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002896#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002897 // see http://www.python.org/dev/peps/pep-0257/
2898
2899 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002900 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002901 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002902 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002903 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002904 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2905 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002906 for (int i = 0; i < num_nodes; i++) {
2907 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002908 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 +00002909 // not a newline, so this is the first statement; finish search
2910 break;
2911 }
2912 }
2913 // 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 +00002914 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002915 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002916 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002917 } else {
2918 return;
2919 }
2920
2921 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002922 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002923 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002924 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2925 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2926 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2927 // compile the doc string
2928 compile_node(comp, pns->nodes[0]);
2929 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002930 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002931 }
2932 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002933#else
2934 (void)comp;
2935 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002936#endif
Damien429d7192013-10-04 19:53:11 +01002937}
2938
Damien George1463c1f2014-04-25 23:52:57 +01002939STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002940 comp->pass = pass;
2941 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002942 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002943 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002944
Damien George36db6bc2014-05-07 17:24:22 +01002945 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002946 // reset maximum stack sizes in scope
2947 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002948 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002949 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002950 }
2951
Damien429d7192013-10-04 19:53:11 +01002952 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002953 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2954 assert(scope->kind == SCOPE_MODULE);
2955 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2956 compile_node(comp, pns->nodes[0]); // compile the expression
2957 EMIT(return_value);
2958 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002959 if (!comp->is_repl) {
2960 check_for_doc_string(comp, scope->pn);
2961 }
Damien429d7192013-10-04 19:53:11 +01002962 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002963 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002964 EMIT(return_value);
2965 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002966 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2967 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2968 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002969
2970 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002971 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002972 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002973 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002974 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002975 }
2976 #if MICROPY_EMIT_NATIVE
2977 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002978 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002979 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002980
Damien George2ac4af62014-08-15 16:45:41 +01002981 // argument annotations
2982 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2983
2984 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01002985 mp_parse_node_t pn_annotation = pns->nodes[2];
2986 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01002987 // nodes[2] can be null or a test-expr
2988 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2989 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2990 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2991 } else {
2992 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002993 }
2994 }
Damien George2ac4af62014-08-15 16:45:41 +01002995 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01002996 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002997
2998 compile_node(comp, pns->nodes[3]); // 3 is function body
2999 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003000 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003001 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003002 EMIT(return_value);
3003 }
3004 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003005 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3006 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3007 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003008
3009 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003010 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003011 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003012 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003013 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3014 }
3015
3016 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003017
3018 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3019 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3020 EMIT(pop_top);
3021 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3022 }
Damien429d7192013-10-04 19:53:11 +01003023 EMIT(return_value);
3024 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3025 // a bit of a hack at the moment
3026
Damiend99b0522013-12-21 18:17:45 +00003027 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3028 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3029 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3030 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3031 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003032
Damien George708c0732014-04-27 19:23:46 +01003033 // We need a unique name for the comprehension argument (the iterator).
3034 // CPython uses .0, but we should be able to use anything that won't
3035 // clash with a user defined variable. Best to use an existing qstr,
3036 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01003037 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01003038 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003039 bool added;
3040 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3041 assert(added);
3042 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003043 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003044 }
3045
3046 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003047 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003048 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003049 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003050 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003051 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003052 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003053 #endif
Damien429d7192013-10-04 19:53:11 +01003054 }
3055
Damien George6f355fd2014-04-10 14:11:31 +01003056 uint l_end = comp_next_label(comp);
3057 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00003058 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00003059 EMIT_ARG(label_assign, l_top);
3060 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003061 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3062 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003063 EMIT_ARG(jump, l_top);
3064 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003065 EMIT(for_iter_end);
3066
3067 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003068 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003069 }
3070 EMIT(return_value);
3071 } else {
3072 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003073 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3074 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3075 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003076
Damien George36db6bc2014-05-07 17:24:22 +01003077 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003078 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003079 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003080 assert(added);
3081 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003082 }
3083
Damien George542bd6b2015-03-26 14:42:40 +00003084 compile_load_id(comp, MP_QSTR___name__);
3085 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003086 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003087 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003088
3089 check_for_doc_string(comp, pns->nodes[2]);
3090 compile_node(comp, pns->nodes[2]); // 2 is class body
3091
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003092 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003093 assert(id != NULL);
3094 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003095 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003096 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003097 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003098 }
3099 EMIT(return_value);
3100 }
3101
Damien415eb6f2013-10-05 12:19:06 +01003102 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003103
3104 // make sure we match all the exception levels
3105 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003106}
3107
Damien George094d4502014-04-02 17:31:27 +01003108#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003109// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003110STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003111 comp->pass = pass;
3112 comp->scope_cur = scope;
3113 comp->next_label = 1;
3114
3115 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003116 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003117 return;
3118 }
3119
Damien George36db6bc2014-05-07 17:24:22 +01003120 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003121 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003122 }
3123
Damien826005c2013-10-05 23:17:28 +01003124 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003125 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3126 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3127 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003128
Damiend99b0522013-12-21 18:17:45 +00003129 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003130
Damiena2f2f7d2013-10-06 00:14:13 +01003131 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003132 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003133 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003134 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003135 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003136 if (comp->compile_error != MP_OBJ_NULL) {
3137 goto inline_asm_error;
3138 }
Damiena2f2f7d2013-10-06 00:14:13 +01003139 }
3140
Damiend99b0522013-12-21 18:17:45 +00003141 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003142
Damiend99b0522013-12-21 18:17:45 +00003143 mp_parse_node_t pn_body = pns->nodes[3]; // body
3144 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003145 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003146
Damien826005c2013-10-05 23:17:28 +01003147 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003148 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3149 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003150 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3151 // no instructions
3152 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003153 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003154 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003155 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003156 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003157 return;
3158 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003159
3160 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003161 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003162 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3163 goto not_an_instruction;
3164 }
Damiend99b0522013-12-21 18:17:45 +00003165 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003166 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3167 goto not_an_instruction;
3168 }
3169 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3170 goto not_an_instruction;
3171 }
3172 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3173 goto not_an_instruction;
3174 }
Damiend99b0522013-12-21 18:17:45 +00003175 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003176
3177 // parse node looks like an instruction
3178 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003179 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3180 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3181 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003182 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003183
3184 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003185 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003186 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003187 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003188 return;
3189 }
Damien George6f355fd2014-04-10 14:11:31 +01003190 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003191 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003192 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3193 compile_syntax_error(comp, nodes[i], "label redefined");
3194 return;
3195 }
Damien826005c2013-10-05 23:17:28 +01003196 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003197 } else if (op == MP_QSTR_align) {
3198 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003199 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003200 return;
3201 }
Damien George36db6bc2014-05-07 17:24:22 +01003202 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003203 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3204 }
3205 } else if (op == MP_QSTR_data) {
3206 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003207 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003208 return;
3209 }
Damien George36db6bc2014-05-07 17:24:22 +01003210 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003211 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003212 for (uint j = 1; j < n_args; j++) {
3213 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003214 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003215 return;
3216 }
Damien George50912e72015-01-20 11:55:10 +00003217 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003218 }
3219 }
Damien826005c2013-10-05 23:17:28 +01003220 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003221 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003222 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003223 }
3224 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003225
3226 if (comp->compile_error != MP_OBJ_NULL) {
3227 pns = pns2; // this is the parse node that had the error
3228 goto inline_asm_error;
3229 }
Damien826005c2013-10-05 23:17:28 +01003230 }
3231
Damien George36db6bc2014-05-07 17:24:22 +01003232 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003233 EMIT_INLINE_ASM(end_pass);
3234 }
3235
3236 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003237 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003238 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003239 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003240 }
Damien429d7192013-10-04 19:53:11 +01003241}
Damien George094d4502014-04-02 17:31:27 +01003242#endif
Damien429d7192013-10-04 19:53:11 +01003243
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003244STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003245 // in Micro Python we put the *x parameter after all other parameters (except **y)
3246 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3247 id_info_t *id_param = NULL;
3248 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3249 id_info_t *id = &scope->id_info[i];
3250 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3251 if (id_param != NULL) {
3252 // swap star param with last param
3253 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3254 }
3255 break;
3256 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3257 id_param = id;
3258 }
3259 }
3260 }
Damien George2827d622014-04-27 15:50:52 +01003261
Damien429d7192013-10-04 19:53:11 +01003262 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003263 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003264 scope->num_locals = 0;
3265 for (int i = 0; i < scope->id_info_len; i++) {
3266 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003267 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003268 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3269 continue;
3270 }
3271 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3272 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3273 }
Damien George2827d622014-04-27 15:50:52 +01003274 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003275 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003276 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003277 }
3278 }
3279
Damien George65dc9602015-08-14 12:24:11 +01003280 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003281 for (int i = 0; i < scope->id_info_len; i++) {
3282 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003283 // in Micro Python the cells come right after the fast locals
3284 // parameters are not counted here, since they remain at the start
3285 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003286 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003287 id->local_num = scope->num_locals;
3288 scope->num_locals += 1;
3289 }
Damien9ecbcff2013-12-11 00:41:43 +00003290 }
Damien9ecbcff2013-12-11 00:41:43 +00003291
Damien George65dc9602015-08-14 12:24:11 +01003292 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003293 // make sure they are in the order of the parent scope
3294 if (scope->parent != NULL) {
3295 int num_free = 0;
3296 for (int i = 0; i < scope->parent->id_info_len; i++) {
3297 id_info_t *id = &scope->parent->id_info[i];
3298 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3299 for (int j = 0; j < scope->id_info_len; j++) {
3300 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003301 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003302 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003303 // in Micro Python the frees come first, before the params
3304 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003305 num_free += 1;
3306 }
3307 }
3308 }
Damien429d7192013-10-04 19:53:11 +01003309 }
Damien George6baf76e2013-12-30 22:32:17 +00003310 // in Micro Python shift all other locals after the free locals
3311 if (num_free > 0) {
3312 for (int i = 0; i < scope->id_info_len; i++) {
3313 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003314 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003315 id->local_num += num_free;
3316 }
3317 }
Damien George2827d622014-04-27 15:50:52 +01003318 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 +00003319 scope->num_locals += num_free;
3320 }
Damien429d7192013-10-04 19:53:11 +01003321 }
Damien429d7192013-10-04 19:53:11 +01003322}
3323
Damien George58e0f4a2015-09-23 10:50:43 +01003324mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
Damien George9d5e5c02015-09-24 13:15:57 +01003325 // put compiler state on the stack, it's relatively small
3326 compiler_t comp_state = {0};
3327 compiler_t *comp = &comp_state;
3328
Damien Georgecbd2f742014-01-19 11:48:48 +00003329 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003330 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003331
Damien George62a3a282015-03-01 12:04:05 +00003332 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003333 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003334
3335 // optimise constants (scope must be set for error messages to work)
3336 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003337 mp_map_t consts;
3338 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003339 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003340 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003341
Damien Georgea210c772015-03-26 15:49:53 +00003342 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003343 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003344
Damien826005c2013-10-05 23:17:28 +01003345 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003346 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003347 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003348 comp->emit_method_table = &emit_bc_method_table;
3349 #endif
Damien826005c2013-10-05 23:17:28 +01003350 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003351 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003352 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003353#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003354 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003355 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003356#endif
Damien826005c2013-10-05 23:17:28 +01003357 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003358 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003359 }
3360
3361 // update maximim number of labels needed
3362 if (comp->next_label > max_num_labels) {
3363 max_num_labels = comp->next_label;
3364 }
Damien429d7192013-10-04 19:53:11 +01003365 }
3366
Damien826005c2013-10-05 23:17:28 +01003367 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003368 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 +00003369 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003370 }
3371
Damien Georgea210c772015-03-26 15:49:53 +00003372 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003373 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003374
Damien826005c2013-10-05 23:17:28 +01003375 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003376#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003377 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003378#endif
Damien3ef4abb2013-10-12 16:53:13 +01003379#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003380 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003381#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003382 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003383 if (false) {
3384 // dummy
3385
Damien3ef4abb2013-10-12 16:53:13 +01003386#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003387 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003388 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003389 if (emit_inline_thumb == NULL) {
3390 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3391 }
3392 comp->emit = NULL;
Damien826005c2013-10-05 23:17:28 +01003393 comp->emit_inline_asm = emit_inline_thumb;
3394 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003395 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003396 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003397 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003398 }
Damienc025ebb2013-10-12 14:30:21 +01003399#endif
3400
Damien826005c2013-10-05 23:17:28 +01003401 } else {
Damienc025ebb2013-10-12 14:30:21 +01003402
3403 // choose the emit type
3404
Damien826005c2013-10-05 23:17:28 +01003405 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003406
3407#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003408 case MP_EMIT_OPT_NATIVE_PYTHON:
3409 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003410#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003411 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003412 emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003413 }
Damien13ed3a62013-10-08 09:05:10 +01003414 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003415#elif MICROPY_EMIT_X86
3416 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003417 emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels);
Damien Georgec90f59e2014-09-06 23:06:36 +01003418 }
3419 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003420#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003421 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003422 emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels);
Damienc025ebb2013-10-12 14:30:21 +01003423 }
3424 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003425#elif MICROPY_EMIT_ARM
3426 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003427 emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003428 }
3429 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003430#endif
3431 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003432 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003433 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003434#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003435
Damien826005c2013-10-05 23:17:28 +01003436 default:
Damien826005c2013-10-05 23:17:28 +01003437 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003438 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003439 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003440 #endif
Damien826005c2013-10-05 23:17:28 +01003441 break;
3442 }
Damienc025ebb2013-10-12 14:30:21 +01003443
Damien George1e1779e2015-01-14 00:20:28 +00003444 // need a pass to compute stack size
3445 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3446
Damien George36db6bc2014-05-07 17:24:22 +01003447 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003448 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003449 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3450 }
3451
3452 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003453 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003454 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003455 }
Damien6cdd3af2013-10-05 18:08:26 +01003456 }
Damien429d7192013-10-04 19:53:11 +01003457 }
3458
Damien Georgecfc4c332015-07-29 22:16:01 +00003459 if (comp->compile_error != MP_OBJ_NULL) {
3460 // if there is no line number for the error then use the line
3461 // number for the start of this scope
3462 compile_error_set_line(comp, comp->scope_cur->pn);
3463 // add a traceback to the exception using relevant source info
3464 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3465 comp->compile_error_line, comp->scope_cur->simple_name);
3466 }
3467
Damien George41d02b62014-01-24 22:42:28 +00003468 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003469
Damien Georgea210c772015-03-26 15:49:53 +00003470 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003471#if MICROPY_EMIT_NATIVE
3472 if (emit_native != NULL) {
3473#if MICROPY_EMIT_X64
3474 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003475#elif MICROPY_EMIT_X86
3476 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003477#elif MICROPY_EMIT_THUMB
3478 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003479#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003480 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003481#endif
3482 }
3483#endif
3484#if MICROPY_EMIT_INLINE_THUMB
3485 if (emit_inline_thumb != NULL) {
3486 emit_inline_thumb_free(emit_inline_thumb);
3487 }
3488#endif
Damien George41d02b62014-01-24 22:42:28 +00003489
Damien George52b5d762014-09-23 15:31:56 +00003490 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003491 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003492
Damien George41d02b62014-01-24 22:42:28 +00003493 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003494 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003495 for (scope_t *s = module_scope; s;) {
3496 scope_t *next = s->next;
3497 scope_free(s);
3498 s = next;
3499 }
Damien5ac1b2e2013-10-18 19:58:12 +01003500
Damien George9d5e5c02015-09-24 13:15:57 +01003501 if (comp->compile_error != MP_OBJ_NULL) {
3502 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003503 } else {
Damien George1fb03172014-01-03 14:22:03 +00003504 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003505 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003506 }
Damien429d7192013-10-04 19:53:11 +01003507}