blob: 12134d4a9d0c747de0389811c8da152addbf56fd [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
36#include "py/smallint.h"
37#include "py/runtime.h"
38#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010039
40// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien George41125902015-03-26 16:44:14 +000052#define NEED_METHOD_TABLE (MICROPY_EMIT_CPYTHON || MICROPY_EMIT_NATIVE)
53
54#if NEED_METHOD_TABLE
55
56// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000057#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
58#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000059#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
60#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien Georgeb9791222014-01-23 00:34:21 +000061#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
62#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010063
Damien George41125902015-03-26 16:44:14 +000064#else
65
66// if we only have the bytecode emitter enabled then we can do a direct call to the functions
67#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
68#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
69#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
70#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
71
72#endif
73
Damien Georgea91ac202014-10-05 19:01:34 +010074// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010075typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000076 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010077
Damien George78035b92014-04-09 12:27:39 +010078 uint8_t is_repl;
79 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010080 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010081 uint8_t have_star;
82
83 // try to keep compiler clean from nlr
84 // this is set to an exception object if we have a compile error
85 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010086
Damien George6f355fd2014-04-10 14:11:31 +010087 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010088
Damien George69b89d22014-04-11 13:38:30 +000089 uint16_t num_dict_params;
90 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010091
Damien Georgea91ac202014-10-05 19:01:34 +010092 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
93 uint16_t continue_label;
94 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000095 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010096
Damien429d7192013-10-04 19:53:11 +010097 scope_t *scope_head;
98 scope_t *scope_cur;
99
Damien6cdd3af2013-10-05 18:08:26 +0100100 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000101 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100102 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000103 #endif
Damien826005c2013-10-05 23:17:28 +0100104
Damien Georgea77ffe62015-03-14 12:59:31 +0000105 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +0100106 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
107 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien Georgea77ffe62015-03-14 12:59:31 +0000108 #endif
Damien429d7192013-10-04 19:53:11 +0100109} compiler_t;
110
Damien George84d59c22015-07-27 22:20:00 +0100111STATIC void compile_error_add_traceback(compiler_t *comp, mp_parse_node_t pn) {
112 mp_uint_t line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100113 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George84d59c22015-07-27 22:20:00 +0100114 line = (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100115 } else {
Damien Georgea91ac202014-10-05 19:01:34 +0100116 // we don't have a line number, so just pass 0
Damien George84d59c22015-07-27 22:20:00 +0100117 line = 0;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100118 }
Damien George84d59c22015-07-27 22:20:00 +0100119 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, line, comp->scope_cur->simple_name);
120}
121
122STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
123 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
124 compile_error_add_traceback(comp, pn);
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])) {
159 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
160 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
161 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
162 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
163 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
164 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
165 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
166 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
167 ) {
168 // code to assign dynamic constants: id = const(value)
169
170 // get the id
171 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
172
173 // get the value
174 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
175 pn_value = fold_constants(comp, pn_value, consts);
176 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
177 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
178 break;
179 }
Damien George40f3c022014-07-03 13:25:24 +0100180 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000181
182 // store the value in the table of dynamic constants
183 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
184 if (elem->value != MP_OBJ_NULL) {
185 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
186 break;
187 }
188 elem->value = MP_OBJ_NEW_SMALL_INT(value);
189
190 // replace const(value) with value
191 pns1->nodes[0] = pn_value;
192
193 // finished folding this assignment
194 return pn;
195 }
196 }
197 }
198 break;
199#endif
Damien George5042bce2014-05-25 22:06:06 +0100200 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000201 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000202 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100203 return pn;
Damien429d7192013-10-04 19:53:11 +0100204 }
205
Damien Georgeffae48d2014-05-08 15:58:39 +0000206 // fold arguments
207 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
208 for (int i = 0; i < n; i++) {
209 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
210 }
211
212 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000213 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100214 case PN_atom_paren:
215 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
216 // (int)
217 pn = pns->nodes[0];
218 }
219 break;
220
221 case PN_expr:
222 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
223 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100224 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
225 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100226 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
227 }
228 break;
229
230 case PN_and_expr:
231 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
232 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100233 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
234 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100235 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
236 }
237 break;
238
Damien429d7192013-10-04 19:53:11 +0100239 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000240 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 +0100241 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
242 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000243 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100244 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000245 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 +0100246 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
247 }
Damien George0bb97132015-02-27 14:25:47 +0000248 } else {
249 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100250 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000251 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200252 // Shifting to big amounts is underfined behavior
253 // in C and is CPU-dependent; propagate sign bit.
254 arg1 = BITS_PER_WORD - 1;
255 }
Damiend99b0522013-12-21 18:17:45 +0000256 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100257 }
258 }
259 break;
260
261 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100262 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000263 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 +0100264 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
265 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000266 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100267 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000268 arg0 += arg1;
Damien George0bb97132015-02-27 14:25:47 +0000269 } else {
270 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100271 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000272 arg0 -= arg1;
Damien0efb3a12013-10-12 16:16:56 +0100273 }
Damien Georged1e355e2014-05-28 14:51:12 +0100274 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000275 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100276 }
277 }
278 break;
279
280 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000281 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 +0100282 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
283 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000284 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100285 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000286 if (!mp_small_int_mul_overflow(arg0, arg1)) {
287 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100288 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000289 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
290 }
291 }
Damiend99b0522013-12-21 18:17:45 +0000292 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100293 // int / int
294 // pass
Damiend99b0522013-12-21 18:17:45 +0000295 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100296 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000297 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damien George0bb97132015-02-27 14:25:47 +0000298 } else {
299 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300300 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100301 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000302 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 +0300303 }
Damien429d7192013-10-04 19:53:11 +0100304 }
305 }
306 break;
307
308 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000309 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100310 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000311 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100312 // +int
Damiend99b0522013-12-21 18:17:45 +0000313 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
314 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
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);
Damien George0bb97132015-02-27 14:25:47 +0000317 } else {
318 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100319 // ~int
Damiend99b0522013-12-21 18:17:45 +0000320 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100321 }
322 }
323 break;
324
325 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100326 if (0) {
327#if MICROPY_EMIT_CPYTHON
328 } else if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100329 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100330 // can overflow; enabled only to compare with CPython
Damien George4dea9222015-04-09 15:29:54 +0000331 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
Damiend99b0522013-12-21 18:17:45 +0000332 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200333 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100334 if (power >= 0) {
335 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200336 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100337 for (; power > 0; power--) {
338 ans *= base;
339 }
Damiend99b0522013-12-21 18:17:45 +0000340 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100341 }
342 }
Damien George57e99eb2014-04-10 22:42:11 +0100343#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000344#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100345 } 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])) {
346 // id.id
347 // look it up in constant table, see if it can be replaced with an integer
Damien George4dea9222015-04-09 15:29:54 +0000348 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George57e99eb2014-04-10 22:42:11 +0100349 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
350 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
351 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
352 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
353 if (elem != NULL) {
354 mp_obj_t dest[2];
355 mp_load_method_maybe(elem->value, q_attr, dest);
356 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100357 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000358 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100359 }
360 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000361#endif
Damien429d7192013-10-04 19:53:11 +0100362 }
363 break;
364 }
365 }
366
367 return pn;
368}
369
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200370STATIC 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 +0100371STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000372STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100373
Damien George6f355fd2014-04-10 14:11:31 +0100374STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100375 return comp->next_label++;
376}
377
Damien George8dcc0c72014-03-27 10:55:21 +0000378STATIC void compile_increase_except_level(compiler_t *comp) {
379 comp->cur_except_level += 1;
380 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
381 comp->scope_cur->exc_stack_size = comp->cur_except_level;
382 }
383}
384
385STATIC void compile_decrease_except_level(compiler_t *comp) {
386 assert(comp->cur_except_level > 0);
387 comp->cur_except_level -= 1;
388}
389
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200390STATIC 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 +0100391 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100392 scope->parent = comp->scope_cur;
393 scope->next = NULL;
394 if (comp->scope_head == NULL) {
395 comp->scope_head = scope;
396 } else {
397 scope_t *s = comp->scope_head;
398 while (s->next != NULL) {
399 s = s->next;
400 }
401 s->next = scope;
402 }
403 return scope;
404}
405
Damien George91bc32d2015-04-09 15:31:53 +0000406typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
407
408STATIC 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 +0000409 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000410 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
411 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100412 for (int i = 0; i < num_nodes; i++) {
413 f(comp, pns->nodes[i]);
414 }
Damiend99b0522013-12-21 18:17:45 +0000415 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100416 f(comp, pn);
417 }
418}
419
Damien George969a6b32014-12-10 22:07:04 +0000420STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000421 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100422 for (int i = 0; i < num_nodes; i++) {
423 compile_node(comp, pns->nodes[i]);
424 }
425}
426
Damien George542bd6b2015-03-26 14:42:40 +0000427STATIC void compile_load_id(compiler_t *comp, qstr qst) {
428 if (comp->pass == MP_PASS_SCOPE) {
429 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
430 } else {
Damien George41125902015-03-26 16:44:14 +0000431 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000432 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000433 #else
434 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
435 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000436 }
437}
438
439STATIC void compile_store_id(compiler_t *comp, qstr qst) {
440 if (comp->pass == MP_PASS_SCOPE) {
441 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
442 } else {
Damien George41125902015-03-26 16:44:14 +0000443 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000444 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000445 #else
446 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
447 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000448 }
449}
450
451STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
452 if (comp->pass == MP_PASS_SCOPE) {
453 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
454 } else {
Damien George41125902015-03-26 16:44:14 +0000455 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000456 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000457 #else
458 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
459 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000460 }
461}
462
Damien3ef4abb2013-10-12 16:53:13 +0100463#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200464STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100465 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
466 return true;
467 }
Damien George4c81ba82015-01-13 16:21:23 +0000468 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
469 return true;
470 }
Damien George7d414a12015-02-08 01:57:40 +0000471 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
472 return true;
473 }
Damiend99b0522013-12-21 18:17:45 +0000474 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100475 return false;
476 }
Damiend99b0522013-12-21 18:17:45 +0000477 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100478 return false;
479 }
480 return true;
481}
482
Damien George5042bce2014-05-25 22:06:06 +0100483STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000484 bool has_single_quote = false;
485 bool has_double_quote = false;
486 for (int i = 0; i < len; i++) {
487 if (str[i] == '\'') {
488 has_single_quote = true;
489 } else if (str[i] == '"') {
490 has_double_quote = true;
491 }
492 }
493 if (bytes) {
494 vstr_printf(vstr, "b");
495 }
496 bool quote_single = false;
497 if (has_single_quote && !has_double_quote) {
498 vstr_printf(vstr, "\"");
499 } else {
500 quote_single = true;
501 vstr_printf(vstr, "'");
502 }
503 for (int i = 0; i < len; i++) {
504 if (str[i] == '\n') {
505 vstr_printf(vstr, "\\n");
506 } else if (str[i] == '\\') {
507 vstr_printf(vstr, "\\\\");
508 } else if (str[i] == '\'' && quote_single) {
509 vstr_printf(vstr, "\\'");
510 } else {
511 vstr_printf(vstr, "%c", str[i]);
512 }
513 }
514 if (has_single_quote && !has_double_quote) {
515 vstr_printf(vstr, "\"");
516 } else {
517 vstr_printf(vstr, "'");
518 }
519}
520
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200521STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000522 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string) || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
Damien George5042bce2014-05-25 22:06:06 +0100523 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000524 cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes));
Damien George5042bce2014-05-25 22:06:06 +0100525 return;
526 }
527
Damien George7d414a12015-02-08 01:57:40 +0000528 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
529 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
530 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
531 return;
532 }
533
Damiend99b0522013-12-21 18:17:45 +0000534 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200535 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
536 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
537 return;
538 }
539
Damien George42f3de92014-10-03 17:44:14 +0000540 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000541 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
542 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100543 case MP_PARSE_NODE_STRING:
544 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100545 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100546 const byte *str = qstr_data(arg, &len);
547 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
548 break;
549 }
Damiend99b0522013-12-21 18:17:45 +0000550 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100551 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000552 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
553 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
554 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100555 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100556 }
557 break;
558 default: assert(0);
559 }
560}
561
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200562STATIC void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100563 int n = 0;
564 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000565 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100566 }
567 int total = n;
568 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000569 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100570 total += 1;
Damien3a205172013-10-12 15:01:56 +0100571 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100572 is_const = false;
573 }
574 }
575 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100576 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100577 is_const = false;
578 break;
579 }
580 }
581 if (total > 0 && is_const) {
582 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000583 vstr_t *vstr = vstr_new();
584 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000585 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000586 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100587 need_comma = true;
588 }
589 for (int i = 0; i < n; i++) {
590 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000591 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100592 }
Damien02f89412013-12-12 15:13:36 +0000593 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100594 need_comma = true;
595 }
596 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000597 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100598 } else {
Damien02f89412013-12-12 15:13:36 +0000599 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100600 }
Damien George0d3cb672015-01-28 23:43:01 +0000601 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000602 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100603 } else {
Damiend99b0522013-12-21 18:17:45 +0000604 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100605 compile_node(comp, pn);
606 }
607 for (int i = 0; i < n; i++) {
608 compile_node(comp, pns_list->nodes[i]);
609 }
Damien Georgeb9791222014-01-23 00:34:21 +0000610 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100611 }
612}
Damien3a205172013-10-12 15:01:56 +0100613#endif
614
615// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100616STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100617#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100618 cpython_c_tuple(comp, pn, pns_list);
619#else
620 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000621 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100622 compile_node(comp, pn);
623 total += 1;
624 }
625 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000626 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100627 for (int i = 0; i < n; i++) {
628 compile_node(comp, pns_list->nodes[i]);
629 }
630 total += n;
631 }
Damien Georgeb9791222014-01-23 00:34:21 +0000632 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100633#endif
634}
Damien429d7192013-10-04 19:53:11 +0100635
Damien George969a6b32014-12-10 22:07:04 +0000636STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100637 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000638 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100639}
640
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200641STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000642 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
643 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100644}
645
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200646STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000647 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
648 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100649}
650
Damien3ef4abb2013-10-12 16:53:13 +0100651#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100652// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200653STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100654 if (node_is_const_false(pn)) {
655 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000656 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100657 }
658 return;
659 } else if (node_is_const_true(pn)) {
660 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000661 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100662 }
663 return;
Damiend99b0522013-12-21 18:17:45 +0000664 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
665 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
666 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
667 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100668 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100669 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100670 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100671 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100672 }
Damien3a205172013-10-12 15:01:56 +0100673 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000674 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100675 } else {
676 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100677 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100678 }
679 }
680 return;
Damiend99b0522013-12-21 18:17:45 +0000681 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100682 if (jump_if == false) {
683 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100684 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100685 }
686 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100687 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100688 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100689 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100690 }
Damien3a205172013-10-12 15:01:56 +0100691 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000692 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100693 }
694 return;
Damiend99b0522013-12-21 18:17:45 +0000695 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100696 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100697 return;
698 }
699 }
700
701 // nothing special, fall back to default compiling for node and jump
702 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000703 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100704}
Damien3a205172013-10-12 15:01:56 +0100705#endif
Damien429d7192013-10-04 19:53:11 +0100706
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200707STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100708#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100709 cpython_c_if_cond(comp, pn, jump_if, label, false);
710#else
711 if (node_is_const_false(pn)) {
712 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000713 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100714 }
715 return;
716 } else if (node_is_const_true(pn)) {
717 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000718 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100719 }
720 return;
Damiend99b0522013-12-21 18:17:45 +0000721 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
722 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
723 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
724 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100725 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000726 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100727 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100728 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000729 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100730 }
Damien George0b2fd912015-02-28 14:37:54 +0000731 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000732 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100733 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000734 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100735 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000736 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100737 }
738 }
739 return;
Damiend99b0522013-12-21 18:17:45 +0000740 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100741 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000742 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100743 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000744 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100745 }
Damiend99b0522013-12-21 18:17:45 +0000746 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100747 c_if_cond(comp, pns->nodes[0], !jump_if, label);
748 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100749 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
750 // cond is something in parenthesis
751 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
752 // empty tuple, acts as false for the condition
753 if (jump_if == false) {
754 EMIT_ARG(jump, label);
755 }
756 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
757 // non-empty tuple, acts as true for the condition
758 if (jump_if == true) {
759 EMIT_ARG(jump, label);
760 }
761 } else {
762 // parenthesis around 1 item, is just that item
763 c_if_cond(comp, pns->nodes[0], jump_if, label);
764 }
765 return;
Damien3a205172013-10-12 15:01:56 +0100766 }
767 }
768
769 // nothing special, fall back to default compiling for node and jump
770 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000771 EMIT_ARG(pop_jump_if, jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100772#endif
Damien429d7192013-10-04 19:53:11 +0100773}
774
775typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100776STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100777
Damien George6be0b0a2014-08-15 14:30:52 +0100778STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100779 if (assign_kind != ASSIGN_AUG_STORE) {
780 compile_node(comp, pns->nodes[0]);
781 }
782
Damiend99b0522013-12-21 18:17:45 +0000783 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
784 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
785 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
786 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100787 if (assign_kind != ASSIGN_AUG_STORE) {
788 for (int i = 0; i < n - 1; i++) {
789 compile_node(comp, pns1->nodes[i]);
790 }
791 }
Damiend99b0522013-12-21 18:17:45 +0000792 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
793 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100794 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000795 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100796 if (assign_kind == ASSIGN_AUG_STORE) {
797 EMIT(rot_three);
798 EMIT(store_subscr);
799 } else {
800 compile_node(comp, pns1->nodes[0]);
801 if (assign_kind == ASSIGN_AUG_LOAD) {
802 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100803 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100804 } else {
805 EMIT(store_subscr);
806 }
807 }
Damiend99b0522013-12-21 18:17:45 +0000808 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
809 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100810 if (assign_kind == ASSIGN_AUG_LOAD) {
811 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000812 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100813 } else {
814 if (assign_kind == ASSIGN_AUG_STORE) {
815 EMIT(rot_two);
816 }
Damien Georgeb9791222014-01-23 00:34:21 +0000817 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100818 }
819 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100820 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100821 }
822 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100823 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100824 }
825
Damiend99b0522013-12-21 18:17:45 +0000826 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100827 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100828 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100829
830 return;
831
832cannot_assign:
833 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100834}
835
Damien George0288cf02014-04-11 11:53:00 +0000836// 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 +0100837STATIC 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 +0000838 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
839
840 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000841 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000842 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
843 EMIT_ARG(unpack_ex, 0, num_tail);
844 have_star_index = 0;
845 }
Damien George963a5a32015-01-16 17:47:07 +0000846 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000847 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000848 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000849 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
850 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100851 } else {
Damien George9d181f62014-04-27 16:55:27 +0100852 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100853 return;
854 }
855 }
856 }
Damien George963a5a32015-01-16 17:47:07 +0000857 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000858 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100859 }
Damien George0288cf02014-04-11 11:53:00 +0000860 if (num_head != 0) {
861 if (0 == have_star_index) {
862 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100863 } else {
Damien George0288cf02014-04-11 11:53:00 +0000864 c_assign(comp, node_head, ASSIGN_STORE);
865 }
866 }
Damien George963a5a32015-01-16 17:47:07 +0000867 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000868 if (num_head + i == have_star_index) {
869 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
870 } else {
871 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100872 }
873 }
874}
875
876// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100877STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100878 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000879 assert(!MP_PARSE_NODE_IS_NULL(pn));
880 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000881 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000882 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100883 switch (assign_kind) {
884 case ASSIGN_STORE:
885 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000886 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100887 break;
888 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000889 default:
Damien George542bd6b2015-03-26 14:42:40 +0000890 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100891 break;
892 }
893 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100894 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100895 return;
896 }
897 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000898 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000899 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
900 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100901 case PN_power:
902 // lhs is an index or attribute
903 c_assign_power(comp, pns, assign_kind);
904 break;
905
906 case PN_testlist_star_expr:
907 case PN_exprlist:
908 // lhs is a tuple
909 if (assign_kind != ASSIGN_STORE) {
910 goto bad_aug;
911 }
Damien George0288cf02014-04-11 11:53:00 +0000912 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100913 break;
914
915 case PN_atom_paren:
916 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000917 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100918 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100919 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000920 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000921 if (assign_kind != ASSIGN_STORE) {
922 goto bad_aug;
923 }
Damiend99b0522013-12-21 18:17:45 +0000924 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100925 goto testlist_comp;
926 } else {
927 // parenthesis around 1 item, is just that item
928 pn = pns->nodes[0];
929 goto tail_recursion;
930 }
931 break;
932
933 case PN_atom_bracket:
934 // lhs is something in brackets
935 if (assign_kind != ASSIGN_STORE) {
936 goto bad_aug;
937 }
Damiend99b0522013-12-21 18:17:45 +0000938 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100939 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000940 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000941 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
942 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100943 goto testlist_comp;
944 } else {
945 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000946 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100947 }
948 break;
949
950 default:
Damien George9d181f62014-04-27 16:55:27 +0100951 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100952 }
953 return;
954
955 testlist_comp:
956 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000957 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
958 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
959 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100960 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000961 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000962 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000963 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100964 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000965 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
966 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000967 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100968 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100969 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100970 } else {
971 // sequence with 2 items
972 goto sequence_with_2_items;
973 }
974 } else {
975 // sequence with 2 items
976 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000977 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100978 }
979 return;
980 }
981 return;
982
Damien George9d181f62014-04-27 16:55:27 +0100983 cannot_assign:
984 compile_syntax_error(comp, pn, "can't assign to expression");
985 return;
986
Damien429d7192013-10-04 19:53:11 +0100987 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100988 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100989}
990
991// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100992// if we are not in CPython compatibility mode then:
993// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
994// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
995// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100996STATIC 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 +0100997 assert(n_pos_defaults >= 0);
998 assert(n_kw_defaults >= 0);
999
Damien429d7192013-10-04 19:53:11 +01001000 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +00001001 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +01001002 int nfree = 0;
1003 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +00001004 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
1005 id_info_t *id = &comp->scope_cur->id_info[i];
1006 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
1007 for (int j = 0; j < this_scope->id_info_len; j++) {
1008 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01001009 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +00001010#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +01001011 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001012#else
1013 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +00001014 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00001015#endif
Damien318aec62013-12-10 18:28:17 +00001016 nfree += 1;
1017 }
1018 }
Damien429d7192013-10-04 19:53:11 +01001019 }
1020 }
1021 }
Damien429d7192013-10-04 19:53:11 +01001022
1023 // make the function/closure
1024 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001025 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001026 } else {
Damien George3558f622014-04-20 17:50:40 +01001027 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001028 }
1029}
1030
Damien George6be0b0a2014-08-15 14:30:52 +01001031STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001032 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001033 comp->have_star = true;
1034 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001035 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1036 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001037 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001038 } else {
1039 // named star
Damien429d7192013-10-04 19:53:11 +01001040 }
Damien George8b19db02014-04-11 23:25:34 +01001041 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001042
1043 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001044 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001045 // TODO do we need to do anything with this?
1046
1047 } else {
1048 mp_parse_node_t pn_id;
1049 mp_parse_node_t pn_colon;
1050 mp_parse_node_t pn_equal;
1051 if (MP_PARSE_NODE_IS_ID(pn)) {
1052 // this parameter is just an id
1053
1054 pn_id = pn;
1055 pn_colon = MP_PARSE_NODE_NULL;
1056 pn_equal = MP_PARSE_NODE_NULL;
1057
Damien George0bb97132015-02-27 14:25:47 +00001058 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001059 // this parameter has a colon and/or equal specifier
1060
Damien George0bb97132015-02-27 14:25:47 +00001061 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1062
Damien Georgef41fdd02014-03-03 23:19:11 +00001063 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1064 pn_id = pns->nodes[0];
1065 pn_colon = pns->nodes[1];
1066 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +00001067 }
1068
1069 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1070 // this parameter does not have a default value
1071
1072 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001073 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001074 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001075 return;
1076 }
1077
1078 } else {
1079 // this parameter has a default value
1080 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1081
Damien George8b19db02014-04-11 23:25:34 +01001082 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001083 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001084#if MICROPY_EMIT_CPYTHON
Damien George59fba2d2015-06-25 14:42:13 +00001085 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien Georgef0778a72014-06-07 22:01:00 +01001086 compile_node(comp, pn_equal);
1087#else
Damien George69b89d22014-04-11 13:38:30 +00001088 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1089 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001090 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1091 // we need to do this here before we start building the map for the default keywords
1092 if (comp->num_default_params > 0) {
1093 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001094 } else {
1095 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001096 }
Damien George69b89d22014-04-11 13:38:30 +00001097 // first default dict param, so make the map
1098 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001099 }
Damien Georgef0778a72014-06-07 22:01:00 +01001100
1101 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001102 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +00001103 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +00001104 EMIT(store_map);
1105#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001106 } else {
Damien George69b89d22014-04-11 13:38:30 +00001107 comp->num_default_params += 1;
1108 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001109 }
1110 }
1111
1112 // TODO pn_colon not implemented
1113 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001114 }
1115}
1116
1117// leaves function object on stack
1118// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001119STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001120 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001121 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001122 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001123 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001124 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001125 }
1126
1127 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001128 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001129 uint old_num_dict_params = comp->num_dict_params;
1130 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001131
1132 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001133 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001134 comp->num_dict_params = 0;
1135 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001136 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001137
Damien Georgea91ac202014-10-05 19:01:34 +01001138 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001139 return MP_QSTR_NULL;
1140 }
1141
Damien Georgee337f1e2014-03-31 15:18:37 +01001142#if !MICROPY_EMIT_CPYTHON
1143 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001144 // the default keywords args may have already made the tuple; if not, do it now
1145 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001146 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001147 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001148 }
1149#endif
1150
Damien429d7192013-10-04 19:53:11 +01001151 // get the scope for this function
1152 scope_t *fscope = (scope_t*)pns->nodes[4];
1153
1154 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001155 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001156
1157 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001158 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001159 comp->num_dict_params = old_num_dict_params;
1160 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001161
1162 // return its name (the 'f' in "def f(...):")
1163 return fscope->simple_name;
1164}
1165
1166// leaves class object on stack
1167// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001168STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001169 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001170 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001171 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001172 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001173 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001174 }
1175
1176 EMIT(load_build_class);
1177
1178 // scope for this class
1179 scope_t *cscope = (scope_t*)pns->nodes[3];
1180
1181 // compile the class
1182 close_over_variables_etc(comp, cscope, 0, 0);
1183
1184 // get its name
Damien George59fba2d2015-06-25 14:42:13 +00001185 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001186
1187 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001188 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1189 mp_parse_node_t parents = pns->nodes[1];
1190 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1191 parents = MP_PARSE_NODE_NULL;
1192 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001193 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001194 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001195
1196 // return its name (the 'C' in class C(...):")
1197 return cscope->simple_name;
1198}
1199
Damien6cdd3af2013-10-05 18:08:26 +01001200// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001201STATIC 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 +00001202 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001203 return false;
1204 }
1205
1206 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001207 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001208 return true;
1209 }
1210
Damiend99b0522013-12-21 18:17:45 +00001211 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001212 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001213 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001214#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001215 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001216 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001217 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001218 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001219#endif
Damien3ef4abb2013-10-12 16:53:13 +01001220#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001221 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001222 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001223#endif
Damien6cdd3af2013-10-05 18:08:26 +01001224 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001225 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001226 }
1227
1228 return true;
1229}
1230
Damien George969a6b32014-12-10 22:07:04 +00001231STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001232 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001233 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001234 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001235
Damien6cdd3af2013-10-05 18:08:26 +01001236 // inherit emit options for this function/class definition
1237 uint emit_options = comp->scope_cur->emit_options;
1238
1239 // compile each decorator
1240 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001241 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001242 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1243 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001244
1245 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001246 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001247 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001248
1249 // check for built-in decorators
1250 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1251 // this was a built-in
1252 num_built_in_decorators += 1;
1253
1254 } else {
1255 // not a built-in, compile normally
1256
1257 // compile the decorator function
1258 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001259 for (int j = 1; j < name_len; j++) {
1260 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1261 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001262 }
1263
1264 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001265 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001266 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001267 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001268 compile_node(comp, pns_decorator->nodes[1]);
1269 }
Damien429d7192013-10-04 19:53:11 +01001270 }
1271 }
1272
1273 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001274 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001275 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001276 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001277 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001278 } else {
Damien George0bb97132015-02-27 14:25:47 +00001279 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1280 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001281 }
1282
1283 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001284 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001285 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001286 }
1287
1288 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +00001289 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +01001290}
1291
Damien George969a6b32014-12-10 22:07:04 +00001292STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001293 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001294 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +00001295 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +01001296}
1297
Damien George6be0b0a2014-08-15 14:30:52 +01001298STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001299 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00001300 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001301 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1302 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001303
1304 compile_node(comp, pns->nodes[0]); // base of the power node
1305
Damiend99b0522013-12-21 18:17:45 +00001306 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1307 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1308 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1309 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001310 for (int i = 0; i < n - 1; i++) {
1311 compile_node(comp, pns1->nodes[i]);
1312 }
Damiend99b0522013-12-21 18:17:45 +00001313 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1314 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001315 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001316 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001317 compile_node(comp, pns1->nodes[0]);
1318 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001319 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1320 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001321 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001322 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001323 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001324 }
1325 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001326 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001327 }
1328
Damiend99b0522013-12-21 18:17:45 +00001329 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001330 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001331 }
Damiend99b0522013-12-21 18:17:45 +00001332 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1333 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1334 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1335 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001336 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1337
Damiend99b0522013-12-21 18:17:45 +00001338 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1339 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1340 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001341 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001342 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001343 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001344 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001345 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001346 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001347 c_del_stmt(comp, pns->nodes[0]);
1348 for (int i = 0; i < n; i++) {
1349 c_del_stmt(comp, pns1->nodes[i]);
1350 }
Damiend99b0522013-12-21 18:17:45 +00001351 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001352 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001353 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001354 } else {
1355 // sequence with 2 items
1356 goto sequence_with_2_items;
1357 }
1358 } else {
1359 // sequence with 2 items
1360 sequence_with_2_items:
1361 c_del_stmt(comp, pns->nodes[0]);
1362 c_del_stmt(comp, pns->nodes[1]);
1363 }
1364 } else {
1365 // tuple with 1 element
1366 c_del_stmt(comp, pn);
1367 }
1368 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001369 // TODO is there anything else to implement?
1370 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001371 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001372
1373 return;
1374
1375cannot_delete:
1376 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001377}
1378
Damien George969a6b32014-12-10 22:07:04 +00001379STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001380 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1381}
1382
Damien George969a6b32014-12-10 22:07:04 +00001383STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001384 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001385 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001386 }
Damien George090c9232014-10-17 14:08:49 +00001387 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001388 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001389}
1390
Damien George969a6b32014-12-10 22:07:04 +00001391STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001392 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001393 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001394 }
Damien George090c9232014-10-17 14:08:49 +00001395 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001396 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001397}
1398
Damien George969a6b32014-12-10 22:07:04 +00001399STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001400 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001401 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001402 return;
1403 }
Damiend99b0522013-12-21 18:17:45 +00001404 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001405 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001406 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001407 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001408 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001409 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1410 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 +01001411
Damien George6f355fd2014-04-10 14:11:31 +01001412 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001413 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1414 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1415 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001416 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001417 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1418 } else {
1419 compile_node(comp, pns->nodes[0]);
1420 }
1421 EMIT(return_value);
1422}
1423
Damien George969a6b32014-12-10 22:07:04 +00001424STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001425 compile_node(comp, pns->nodes[0]);
1426 EMIT(pop_top);
1427}
1428
Damien George969a6b32014-12-10 22:07:04 +00001429STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001430 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001431 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001432 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001433 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001434 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001435 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001436 compile_node(comp, pns->nodes[0]);
1437 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001438 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001439 } else {
1440 // raise x
1441 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001442 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001443 }
1444}
1445
Damien George635543c2014-04-10 12:56:52 +01001446// q_base holds the base of the name
1447// eg a -> q_base=a
1448// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001449STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001450 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001451 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1452 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001453 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001454 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001455 pn = pns->nodes[0];
1456 is_as = true;
1457 }
Damien George635543c2014-04-10 12:56:52 +01001458 if (MP_PARSE_NODE_IS_NULL(pn)) {
1459 // empty name (eg, from . import x)
1460 *q_base = MP_QSTR_;
1461 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1462 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001463 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001464 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001465 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001466 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001467 }
Damien George635543c2014-04-10 12:56:52 +01001468 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001469 } else {
1470 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001471 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001472 {
Damien429d7192013-10-04 19:53:11 +01001473 // a name of the form a.b.c
1474 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001475 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001476 }
Damiend99b0522013-12-21 18:17:45 +00001477 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001478 int len = n - 1;
1479 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001480 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001481 }
Damien George55baff42014-01-21 21:40:13 +00001482 byte *q_ptr;
1483 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001484 for (int i = 0; i < n; i++) {
1485 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001486 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001487 }
Damien George39dc1452014-10-03 19:52:22 +01001488 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001489 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001490 memcpy(str_dest, str_src, str_src_len);
1491 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001492 }
Damien George635543c2014-04-10 12:56:52 +01001493 qstr q_full = qstr_build_end(q_ptr);
1494 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001495 if (is_as) {
1496 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001497 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001498 }
1499 }
Damien429d7192013-10-04 19:53:11 +01001500 }
Damien429d7192013-10-04 19:53:11 +01001501 }
1502}
1503
Damien George6be0b0a2014-08-15 14:30:52 +01001504STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001505 EMIT_ARG(load_const_small_int, 0); // level 0 import
1506 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1507 qstr q_base;
1508 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001509 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001510}
1511
Damien George969a6b32014-12-10 22:07:04 +00001512STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001513 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1514}
1515
Damien George969a6b32014-12-10 22:07:04 +00001516STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001517 mp_parse_node_t pn_import_source = pns->nodes[0];
1518
1519 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1520 uint import_level = 0;
1521 do {
1522 mp_parse_node_t pn_rel;
1523 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)) {
1524 // This covers relative imports with dots only like "from .. import"
1525 pn_rel = pn_import_source;
1526 pn_import_source = MP_PARSE_NODE_NULL;
1527 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1528 // This covers relative imports starting with dot(s) like "from .foo import"
1529 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1530 pn_rel = pns_2b->nodes[0];
1531 pn_import_source = pns_2b->nodes[1];
1532 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1533 } else {
1534 // Not a relative import
1535 break;
1536 }
1537
1538 // get the list of . and/or ...'s
1539 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001540 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001541
1542 // count the total number of .'s
1543 for (int i = 0; i < n; i++) {
1544 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1545 import_level++;
1546 } else {
1547 // should be an MP_TOKEN_ELLIPSIS
1548 import_level += 3;
1549 }
1550 }
1551 } while (0);
1552
Damiend99b0522013-12-21 18:17:45 +00001553 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001554 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001555
1556 // build the "fromlist" tuple
1557#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001558 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001559#else
Damien George59fba2d2015-06-25 14:42:13 +00001560 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001562#endif
1563
1564 // do the import
Damien George635543c2014-04-10 12:56:52 +01001565 qstr dummy_q;
1566 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001567 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001568
Damien429d7192013-10-04 19:53:11 +01001569 } else {
Damien George635543c2014-04-10 12:56:52 +01001570 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001571
1572 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001573 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001574 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001575#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001576 {
1577 vstr_t *vstr = vstr_new();
1578 vstr_printf(vstr, "(");
1579 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001580 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1581 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1582 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001583 if (i > 0) {
1584 vstr_printf(vstr, ", ");
1585 }
1586 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001587 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001588 const byte *str = qstr_data(id2, &len);
1589 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001590 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001591 }
Damien02f89412013-12-12 15:13:36 +00001592 if (n == 1) {
1593 vstr_printf(vstr, ",");
1594 }
1595 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001596 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001597 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001598 }
Damiendb4c3612013-12-10 17:27:24 +00001599#else
1600 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001601 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1602 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1603 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001604 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001605 }
Damien Georgeb9791222014-01-23 00:34:21 +00001606 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001607#endif
1608
1609 // do the import
Damien George635543c2014-04-10 12:56:52 +01001610 qstr dummy_q;
1611 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001612 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001613 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1614 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1615 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001616 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001617 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001618 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001619 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001620 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001621 }
1622 }
1623 EMIT(pop_top);
1624 }
1625}
1626
Damien George584ba672014-12-21 17:26:45 +00001627STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1628 bool added;
1629 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1630 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001631 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001632 compile_syntax_error(comp, pn, "identifier already used");
1633 return;
1634 }
1635 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1636
1637 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1638 id_info = scope_find_global(comp->scope_cur, qst);
1639 if (id_info != NULL) {
1640 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1641 }
1642}
1643
Damien George969a6b32014-12-10 22:07:04 +00001644STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001645 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001646 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001647 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001648 for (int i = 0; i < n; i++) {
1649 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001650 }
1651 }
1652}
1653
Damien George584ba672014-12-21 17:26:45 +00001654STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1655 bool added;
1656 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1657 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001658 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001659 compile_syntax_error(comp, pn, "identifier already used");
1660 return;
1661 }
1662 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1663 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)) {
1664 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1665 return;
1666 }
1667 id_info->kind = ID_INFO_KIND_FREE;
1668 scope_close_over_in_parents(comp->scope_cur, qst);
1669}
1670
Damien George969a6b32014-12-10 22:07:04 +00001671STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001672 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001673 if (comp->scope_cur->kind == SCOPE_MODULE) {
1674 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1675 return;
1676 }
1677 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001678 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001679 for (int i = 0; i < n; i++) {
1680 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001681 }
1682 }
1683}
1684
Damien George969a6b32014-12-10 22:07:04 +00001685STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001686 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001687 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001688 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001689 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001690 // assertion message
1691 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001692 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001693 }
Damien Georgeb9791222014-01-23 00:34:21 +00001694 EMIT_ARG(raise_varargs, 1);
1695 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001696}
1697
Damien George969a6b32014-12-10 22:07:04 +00001698STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001699 // TODO proper and/or short circuiting
1700
Damien George6f355fd2014-04-10 14:11:31 +01001701 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001702
Damien George391db862014-10-17 17:57:33 +00001703 // optimisation: don't emit anything when "if False" (not in CPython)
1704 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1705 uint l_fail = comp_next_label(comp);
1706 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001707
Damien George391db862014-10-17 17:57:33 +00001708 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001709
Damien George391db862014-10-17 17:57:33 +00001710 // optimisation: skip everything else when "if True" (not in CPython)
1711 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1712 goto done;
1713 }
1714
1715 if (
1716 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1717 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1718 // optimisation: don't jump if last instruction was return
1719 && !EMIT(last_emit_was_return_value)
1720 ) {
1721 // jump over elif/else blocks
1722 EMIT_ARG(jump, l_end);
1723 }
1724
1725 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001726 }
1727
Damien George235f9b32014-10-17 17:30:16 +00001728 // compile elif blocks (if any)
1729 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001730 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 +00001731 for (int i = 0; i < n_elif; i++) {
1732 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1733 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001734
Damien George391db862014-10-17 17:57:33 +00001735 // optimisation: don't emit anything when "if False" (not in CPython)
1736 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1737 uint l_fail = comp_next_label(comp);
1738 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001739
Damien George391db862014-10-17 17:57:33 +00001740 compile_node(comp, pns_elif->nodes[1]); // elif block
1741
1742 // optimisation: skip everything else when "elif True" (not in CPython)
1743 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1744 goto done;
1745 }
1746
1747 // optimisation: don't jump if last instruction was return
1748 if (!EMIT(last_emit_was_return_value)) {
1749 EMIT_ARG(jump, l_end);
1750 }
1751 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001752 }
1753 }
1754
1755 // compile else block
1756 compile_node(comp, pns->nodes[3]); // can be null
1757
Damien George391db862014-10-17 17:57:33 +00001758done:
Damien Georgeb9791222014-01-23 00:34:21 +00001759 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001760}
1761
Damien Georgecbddb272014-02-01 20:08:18 +00001762#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001763 uint16_t old_break_label = comp->break_label; \
1764 uint16_t old_continue_label = comp->continue_label; \
1765 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001766 uint break_label = comp_next_label(comp); \
1767 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001768 comp->break_label = break_label; \
1769 comp->continue_label = continue_label; \
1770 comp->break_continue_except_level = comp->cur_except_level;
1771
1772#define END_BREAK_CONTINUE_BLOCK \
1773 comp->break_label = old_break_label; \
1774 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001775 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001776
Damien George969a6b32014-12-10 22:07:04 +00001777STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001778 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001779
Damience89a212013-10-15 22:25:17 +01001780 // compared to CPython, we have an optimised version of while loops
1781#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001782 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001783 EMIT_ARG(setup_loop, break_label);
1784 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001785 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1786 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001787 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001788 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001789 }
Damien Georgeb9791222014-01-23 00:34:21 +00001790 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001791 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1792 // this is a small hack to agree with CPython
1793 if (!node_is_const_true(pns->nodes[0])) {
1794 EMIT(pop_block);
1795 }
Damience89a212013-10-15 22:25:17 +01001796#else
Damien George391db862014-10-17 17:57:33 +00001797 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1798 uint top_label = comp_next_label(comp);
1799 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1800 EMIT_ARG(jump, continue_label);
1801 }
1802 EMIT_ARG(label_assign, top_label);
1803 compile_node(comp, pns->nodes[1]); // body
1804 EMIT_ARG(label_assign, continue_label);
1805 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1806 }
Damience89a212013-10-15 22:25:17 +01001807#endif
1808
1809 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001810 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001811
1812 compile_node(comp, pns->nodes[2]); // else
1813
Damien Georgeb9791222014-01-23 00:34:21 +00001814 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001815}
1816
Damien George2ac4af62014-08-15 16:45:41 +01001817#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001818// This function compiles an optimised for-loop of the form:
1819// for <var> in range(<start>, <end>, <step>):
1820// <body>
1821// else:
1822// <else>
1823// <var> must be an identifier and <step> must be a small-int.
1824//
1825// Semantics of for-loop require:
1826// - final failing value should not be stored in the loop variable
1827// - if the loop never runs, the loop variable should never be assigned
1828// - assignments to <var>, <end> or <step> in the body do not alter the loop
1829// (<step> is a constant for us, so no need to worry about it changing)
1830//
1831// If <end> is a small-int, then the stack during the for-loop contains just
1832// the current value of <var>. Otherwise, the stack contains <end> then the
1833// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001834STATIC 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 +00001835 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001836
Damien George6f355fd2014-04-10 14:11:31 +01001837 uint top_label = comp_next_label(comp);
1838 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001839
Damien Georgee181c0d2014-12-12 17:19:56 +00001840 // put the end value on the stack if it's not a small-int constant
1841 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1842 if (end_on_stack) {
1843 compile_node(comp, pn_end);
1844 }
1845
1846 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001847 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001848
Damien Georgeb9791222014-01-23 00:34:21 +00001849 EMIT_ARG(jump, entry_label);
1850 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001851
Damien Georgec33ce602014-12-11 17:35:23 +00001852 // duplicate next value and store it to var
1853 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001854 c_assign(comp, pn_var, ASSIGN_STORE);
1855
Damienf3822fc2013-11-09 20:12:03 +00001856 // compile body
1857 compile_node(comp, pn_body);
1858
Damien Georgeb9791222014-01-23 00:34:21 +00001859 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001860
Damien Georgee181c0d2014-12-12 17:19:56 +00001861 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001862 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001863 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001864
Damien Georgeb9791222014-01-23 00:34:21 +00001865 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001866
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001867 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001868 if (end_on_stack) {
1869 EMIT(dup_top_two);
1870 EMIT(rot_two);
1871 } else {
1872 EMIT(dup_top);
1873 compile_node(comp, pn_end);
1874 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001875 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1876 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001877 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001878 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001879 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001880 }
Damien George63f38322015-02-28 15:04:06 +00001881 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001882
1883 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001884 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001885
1886 compile_node(comp, pn_else);
1887
Damien Georgeb9791222014-01-23 00:34:21 +00001888 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001889
1890 // discard final value of var that failed the loop condition
1891 EMIT(pop_top);
1892
1893 // discard <end> value if it's on the stack
1894 if (end_on_stack) {
1895 EMIT(pop_top);
1896 }
Damienf72fd0e2013-11-06 20:20:49 +00001897}
Damien George2ac4af62014-08-15 16:45:41 +01001898#endif
Damienf72fd0e2013-11-06 20:20:49 +00001899
Damien George969a6b32014-12-10 22:07:04 +00001900STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001901#if !MICROPY_EMIT_CPYTHON
1902 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1903 // this is actually slower, but uses no heap memory
1904 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001905 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 +00001906 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001907 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1908 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1909 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1910 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001911 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1912 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001913 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001914 mp_parse_node_t pn_range_start;
1915 mp_parse_node_t pn_range_end;
1916 mp_parse_node_t pn_range_step;
1917 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001918 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001919 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001920 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001921 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001922 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001923 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001924 } else if (n_args == 2) {
1925 pn_range_start = args[0];
1926 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001927 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001928 } else {
1929 pn_range_start = args[0];
1930 pn_range_end = args[1];
1931 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001932 // We need to know sign of step. This is possible only if it's constant
1933 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1934 optimize = false;
1935 }
Damienf72fd0e2013-11-06 20:20:49 +00001936 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001937 }
1938 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001939 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1940 return;
1941 }
1942 }
1943 }
1944#endif
1945
Damien Georgecbddb272014-02-01 20:08:18 +00001946 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001947 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001948
Damien George6f355fd2014-04-10 14:11:31 +01001949 uint pop_label = comp_next_label(comp);
1950 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001951
Damience89a212013-10-15 22:25:17 +01001952 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1953#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001954 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001955#endif
1956
Damien429d7192013-10-04 19:53:11 +01001957 compile_node(comp, pns->nodes[1]); // iterator
1958 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001959 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001960 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001961 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1962 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001963 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001964 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001965 }
Damien Georgeb9791222014-01-23 00:34:21 +00001966 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001967 EMIT(for_iter_end);
1968
1969 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001970 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001971
Damience89a212013-10-15 22:25:17 +01001972#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001973 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001974#endif
Damien429d7192013-10-04 19:53:11 +01001975
1976 compile_node(comp, pns->nodes[3]); // else (not tested)
1977
Damien Georgeb9791222014-01-23 00:34:21 +00001978 EMIT_ARG(label_assign, break_label);
1979 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001980}
1981
Damien George6be0b0a2014-08-15 14:30:52 +01001982STATIC 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 +01001983 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001984 uint l1 = comp_next_label(comp);
1985 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001986
Damien Georgeb9791222014-01-23 00:34:21 +00001987 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001988 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001989
Damien429d7192013-10-04 19:53:11 +01001990 compile_node(comp, pn_body); // body
1991 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001992 EMIT_ARG(jump, success_label); // jump over exception handler
1993
1994 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001995 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001996
Damien George6f355fd2014-04-10 14:11:31 +01001997 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001998
1999 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00002000 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
2001 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01002002
2003 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01002004 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002005
Damiend99b0522013-12-21 18:17:45 +00002006 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002007 // this is a catch all exception handler
2008 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002009 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00002010 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002011 return;
2012 }
2013 } else {
2014 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00002015 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
2016 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
2017 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
2018 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01002019 // handler binds the exception to a local
2020 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002021 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002022 }
2023 }
2024 EMIT(dup_top);
2025 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002026 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00002027 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002028 }
2029
2030 EMIT(pop_top);
2031
2032 if (qstr_exception_local == 0) {
2033 EMIT(pop_top);
2034 } else {
Damien George542bd6b2015-03-26 14:42:40 +00002035 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002036 }
2037
2038 EMIT(pop_top);
2039
Damien George6f355fd2014-04-10 14:11:31 +01002040 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002041 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002042 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002043 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002044 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002045 }
2046 compile_node(comp, pns_except->nodes[1]);
2047 if (qstr_exception_local != 0) {
2048 EMIT(pop_block);
2049 }
2050 EMIT(pop_except);
2051 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002052 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2053 EMIT_ARG(label_assign, l3);
2054 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00002055 compile_store_id(comp, qstr_exception_local);
2056 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002057
Damien George8dcc0c72014-03-27 10:55:21 +00002058 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002059 EMIT(end_finally);
2060 }
Damien Georgeb9791222014-01-23 00:34:21 +00002061 EMIT_ARG(jump, l2);
2062 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002063 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002064 }
2065
Damien George8dcc0c72014-03-27 10:55:21 +00002066 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002067 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002068 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002069
Damien Georgeb9791222014-01-23 00:34:21 +00002070 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002071 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002072 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002073}
2074
Damien George6be0b0a2014-08-15 14:30:52 +01002075STATIC 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 +01002076 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002077
Damien Georgeb9791222014-01-23 00:34:21 +00002078 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002079 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002080
Damien429d7192013-10-04 19:53:11 +01002081 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002082 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002083 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002084 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002085 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002086 } else {
2087 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2088 }
2089 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002090 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2091 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002092 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002093
Damien George8dcc0c72014-03-27 10:55:21 +00002094 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002095 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002096}
2097
Damien George969a6b32014-12-10 22:07:04 +00002098STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00002099 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2100 {
Damiend99b0522013-12-21 18:17:45 +00002101 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2102 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002103 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002104 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2105 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002106 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002107 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002108 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002109 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002110 // no finally
2111 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2112 } else {
2113 // have finally
Damiend99b0522013-12-21 18:17:45 +00002114 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 +01002115 }
2116 } else {
2117 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002118 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002119 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002120 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002121 }
Damien429d7192013-10-04 19:53:11 +01002122 }
2123}
2124
Damien George6be0b0a2014-08-15 14:30:52 +01002125STATIC 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 +01002126 if (n == 0) {
2127 // no more pre-bits, compile the body of the with
2128 compile_node(comp, body);
2129 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002130 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002131 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002132 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002133 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002134 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002135 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002136 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2137 } else {
2138 // this pre-bit is just an expression
2139 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002140 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002141 EMIT(pop_top);
2142 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002143 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002144 // compile additional pre-bits and the body
2145 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2146 // finish this with block
2147 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002148 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2149 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002150 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002151 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002152 EMIT(end_finally);
2153 }
2154}
2155
Damien George969a6b32014-12-10 22:07:04 +00002156STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002157 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002158 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002159 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002160 assert(n > 0);
2161
2162 // compile in a nested fashion
2163 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2164}
2165
Damien George969a6b32014-12-10 22:07:04 +00002166STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002167 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002168 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2169 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00002170 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002171 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002172 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002173 EMIT(pop_top);
2174
Damien429d7192013-10-04 19:53:11 +01002175 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002176 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002177 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002178 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002179 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2180 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002181 // do nothing with a lonely constant
2182 } else {
2183 compile_node(comp, pns->nodes[0]); // just an expression
2184 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2185 }
Damien429d7192013-10-04 19:53:11 +01002186 }
2187 } else {
Damiend99b0522013-12-21 18:17:45 +00002188 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2189 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002190 if (kind == PN_expr_stmt_augassign) {
2191 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2192 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002193 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002194 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002195 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002196 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2197 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2198 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2199 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2200 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2201 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2202 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2203 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2204 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2205 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2206 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002207 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002208 }
Damien George7e5fb242014-02-01 22:18:47 +00002209 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002210 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2211 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002212 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2213 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002214 // following CPython, we store left-most first
2215 if (rhs > 0) {
2216 EMIT(dup_top);
2217 }
2218 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2219 for (int i = 0; i < rhs; i++) {
2220 if (i + 1 < rhs) {
2221 EMIT(dup_top);
2222 }
Damiend99b0522013-12-21 18:17:45 +00002223 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002224 }
Damien George0bb97132015-02-27 14:25:47 +00002225 } else {
2226 assert(kind == PN_expr_stmt_assign); // should be
Damien George42e0c592015-03-14 13:11:35 +00002227 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
2228 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002229 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2230 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2231 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002232 // optimisation for a, b = c, d; to match CPython's optimisation
Damien George4dea9222015-04-09 15:29:54 +00002233 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2234 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002235 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2236 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2237 // can't optimise when it's a star expression on the lhs
2238 goto no_optimisation;
2239 }
Damien429d7192013-10-04 19:53:11 +01002240 compile_node(comp, pns10->nodes[0]); // rhs
2241 compile_node(comp, pns10->nodes[1]); // rhs
2242 EMIT(rot_two);
2243 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2244 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00002245 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2246 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002247 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2248 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2249 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002250 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damien George4dea9222015-04-09 15:29:54 +00002251 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2252 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002253 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2254 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2255 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2256 // can't optimise when it's a star expression on the lhs
2257 goto no_optimisation;
2258 }
Damien429d7192013-10-04 19:53:11 +01002259 compile_node(comp, pns10->nodes[0]); // rhs
2260 compile_node(comp, pns10->nodes[1]); // rhs
2261 compile_node(comp, pns10->nodes[2]); // rhs
2262 EMIT(rot_three);
2263 EMIT(rot_two);
2264 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2265 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2266 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2267 } else {
Damien George495d7812014-04-08 17:51:47 +01002268 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002269 compile_node(comp, pns1->nodes[0]); // rhs
2270 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2271 }
Damien429d7192013-10-04 19:53:11 +01002272 }
2273 }
2274}
2275
Damien George6be0b0a2014-08-15 14:30:52 +01002276STATIC 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 +00002277 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002278 compile_node(comp, pns->nodes[0]);
2279 for (int i = 1; i < num_nodes; i += 1) {
2280 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002281 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002282 }
2283}
2284
Damien George969a6b32014-12-10 22:07:04 +00002285STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002286 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2287 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002288
Damien George6f355fd2014-04-10 14:11:31 +01002289 uint l_fail = comp_next_label(comp);
2290 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002291 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2292 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002293 EMIT_ARG(jump, l_end);
2294 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002295 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002296 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002297 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002298}
2299
Damien George969a6b32014-12-10 22:07:04 +00002300STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002301 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002302 //mp_parse_node_t pn_params = pns->nodes[0];
2303 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002304
Damien George36db6bc2014-05-07 17:24:22 +01002305 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002306 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002307 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 +01002308 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002309 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002310 }
2311
2312 // get the scope for this lambda
2313 scope_t *this_scope = (scope_t*)pns->nodes[2];
2314
2315 // make the lambda
2316 close_over_variables_etc(comp, this_scope, 0, 0);
2317}
2318
Damien George7711afb2015-02-28 15:10:18 +00002319STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002320 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002321 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002322 for (int i = 0; i < n; i += 1) {
2323 compile_node(comp, pns->nodes[i]);
2324 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002325 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002326 }
2327 }
Damien Georgeb9791222014-01-23 00:34:21 +00002328 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002329}
2330
Damien George7711afb2015-02-28 15:10:18 +00002331STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2332 compile_or_and_test(comp, pns, true);
2333}
2334
Damien George969a6b32014-12-10 22:07:04 +00002335STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002336 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002337}
2338
Damien George969a6b32014-12-10 22:07:04 +00002339STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002340 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002341 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002342}
2343
Damien George969a6b32014-12-10 22:07:04 +00002344STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002345 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002346 compile_node(comp, pns->nodes[0]);
2347 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002348 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002349 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002350 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002351 }
2352 for (int i = 1; i + 1 < num_nodes; i += 2) {
2353 compile_node(comp, pns->nodes[i + 1]);
2354 if (i + 2 < num_nodes) {
2355 EMIT(dup_top);
2356 EMIT(rot_three);
2357 }
Damien George7e5fb242014-02-01 22:18:47 +00002358 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002359 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002360 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002361 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2362 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2363 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2364 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2365 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2366 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002367 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002368 }
2369 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002370 } else {
2371 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002372 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2373 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002374 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002375 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002376 } else {
2377 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002378 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002379 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002380 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002381 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002382 }
Damien429d7192013-10-04 19:53:11 +01002383 }
Damien429d7192013-10-04 19:53:11 +01002384 }
2385 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002386 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002387 }
2388 }
2389 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002390 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002391 EMIT_ARG(jump, l_end);
2392 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002393 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002394 EMIT(rot_two);
2395 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002396 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002397 }
2398}
2399
Damien George969a6b32014-12-10 22:07:04 +00002400STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002401 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002402}
2403
Damien George969a6b32014-12-10 22:07:04 +00002404STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002405 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002406}
2407
Damien George969a6b32014-12-10 22:07:04 +00002408STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002409 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002410}
2411
Damien George969a6b32014-12-10 22:07:04 +00002412STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002413 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002414}
2415
Damien George969a6b32014-12-10 22:07:04 +00002416STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002417 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002418 compile_node(comp, pns->nodes[0]);
2419 for (int i = 1; i + 1 < num_nodes; i += 2) {
2420 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002421 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002422 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002423 } else {
Damien George0bb97132015-02-27 14:25:47 +00002424 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2425 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002426 }
2427 }
2428}
2429
Damien George969a6b32014-12-10 22:07:04 +00002430STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002431 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002432 compile_node(comp, pns->nodes[0]);
2433 for (int i = 1; i + 1 < num_nodes; i += 2) {
2434 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002435 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002436 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002437 } else {
Damien George0bb97132015-02-27 14:25:47 +00002438 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2439 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002440 }
2441 }
2442}
2443
Damien George969a6b32014-12-10 22:07:04 +00002444STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002445 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002446 compile_node(comp, pns->nodes[0]);
2447 for (int i = 1; i + 1 < num_nodes; i += 2) {
2448 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002449 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002450 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002451 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002452 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002453 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002454 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002455 } else {
Damien George0bb97132015-02-27 14:25:47 +00002456 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2457 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002458 }
2459 }
2460}
2461
Damien George969a6b32014-12-10 22:07:04 +00002462STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002463 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002464 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002465 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002466 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002467 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002468 } else {
Damien George0bb97132015-02-27 14:25:47 +00002469 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2470 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002471 }
2472}
2473
Damien George969a6b32014-12-10 22:07:04 +00002474STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002475 // this is to handle special super() call
2476 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2477
2478 compile_generic_all_nodes(comp, pns);
2479}
2480
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002481STATIC 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 +01002482 // function to call is on top of stack
2483
Damien George35e2a4e2014-02-05 00:51:47 +00002484#if !MICROPY_EMIT_CPYTHON
2485 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002486 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 +00002487 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002488 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002489 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002490 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002491 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002492 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002493 EMIT_ARG(call_function, 2, 0, 0);
2494 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002495 }
2496 }
Damien George01039b52014-12-21 17:44:27 +00002497 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002498 return;
2499 }
2500#endif
2501
Damien George2c0842b2014-04-27 16:46:51 +01002502 // get the list of arguments
2503 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002504 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002505
Damien George2c0842b2014-04-27 16:46:51 +01002506 // compile the arguments
2507 // Rather than calling compile_node on the list, we go through the list of args
2508 // explicitly here so that we can count the number of arguments and give sensible
2509 // error messages.
2510 int n_positional = n_positional_extra;
2511 uint n_keyword = 0;
2512 uint star_flags = 0;
2513 for (int i = 0; i < n_args; i++) {
2514 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2515 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2516 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2517 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2518 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2519 return;
2520 }
2521 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2522 compile_node(comp, pns_arg->nodes[0]);
2523 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2524 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2525 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2526 return;
2527 }
2528 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2529 compile_node(comp, pns_arg->nodes[0]);
2530 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2531 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2532 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2533 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2534 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2535 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2536 return;
2537 }
Damien George59fba2d2015-06-25 14:42:13 +00002538 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien George2c0842b2014-04-27 16:46:51 +01002539 compile_node(comp, pns2->nodes[0]);
2540 n_keyword += 1;
2541 } else {
2542 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2543 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2544 n_positional++;
2545 }
2546 } else {
2547 goto normal_argument;
2548 }
2549 } else {
2550 normal_argument:
2551 if (n_keyword > 0) {
2552 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2553 return;
2554 }
2555 compile_node(comp, args[i]);
2556 n_positional++;
2557 }
Damien429d7192013-10-04 19:53:11 +01002558 }
2559
Damien George2c0842b2014-04-27 16:46:51 +01002560 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002561 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002562 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002563 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002564 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002565 }
Damien429d7192013-10-04 19:53:11 +01002566}
2567
Damien George969a6b32014-12-10 22:07:04 +00002568STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002569 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002570 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002571 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 +01002572 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002573 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2574 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002575 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002576 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002577 i += 1;
2578 } else {
2579 compile_node(comp, pns->nodes[i]);
2580 }
Damien George35e2a4e2014-02-05 00:51:47 +00002581 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002582 }
2583}
2584
Damien George969a6b32014-12-10 22:07:04 +00002585STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002586 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002587 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002588}
2589
Damien George969a6b32014-12-10 22:07:04 +00002590STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002591 // a list of strings
Damien63321742013-12-10 17:41:49 +00002592
2593 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002594 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002595 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002596 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002597 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002598 int pn_kind;
2599 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2600 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2601 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2602 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2603 } else {
2604 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2605 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002606 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2607 pn_kind = MP_PARSE_NODE_STRING;
2608 } else {
2609 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2610 pn_kind = MP_PARSE_NODE_BYTES;
2611 }
Damien George40f3c022014-07-03 13:25:24 +01002612 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002613 }
Damien63321742013-12-10 17:41:49 +00002614 if (i == 0) {
2615 string_kind = pn_kind;
2616 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002617 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002618 return;
2619 }
Damien429d7192013-10-04 19:53:11 +01002620 }
Damien63321742013-12-10 17:41:49 +00002621
Damien George65ef6b72015-01-14 21:17:27 +00002622 // if we are not in the last pass, just load a dummy object
2623 if (comp->pass != MP_PASS_EMIT) {
2624 EMIT_ARG(load_const_obj, mp_const_none);
2625 return;
2626 }
2627
Damien63321742013-12-10 17:41:49 +00002628 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002629 vstr_t vstr;
2630 vstr_init_len(&vstr, n_bytes);
2631 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002632 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002633 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002634 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002635 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2636 memcpy(s_dest, s, s_len);
2637 s_dest += s_len;
2638 } else {
2639 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002640 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2641 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002642 }
Damien63321742013-12-10 17:41:49 +00002643 }
Damien George65ef6b72015-01-14 21:17:27 +00002644
2645 // load the object
Damien George05005f62015-01-21 22:48:37 +00002646 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 +01002647}
2648
2649// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002650STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002651 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2652 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2653 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002654
Damien George36db6bc2014-05-07 17:24:22 +01002655 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002656 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002657 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 +01002658 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002659 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002660 }
2661
2662 // get the scope for this comprehension
2663 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2664
2665 // compile the comprehension
2666 close_over_variables_etc(comp, this_scope, 0, 0);
2667
2668 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2669 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002670 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002671}
2672
Damien George969a6b32014-12-10 22:07:04 +00002673STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002674 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002675 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002676 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2677 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2678 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2679 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2680 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2681 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2682 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002683 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002684 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002685 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002686 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002687 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002688 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002689 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002690 // generator expression
2691 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2692 } else {
2693 // tuple with 2 items
2694 goto tuple_with_2_items;
2695 }
2696 } else {
2697 // tuple with 2 items
2698 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002699 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002700 }
2701 } else {
2702 // parenthesis around a single item, is just that item
2703 compile_node(comp, pns->nodes[0]);
2704 }
2705}
2706
Damien George969a6b32014-12-10 22:07:04 +00002707STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002708 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002709 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002710 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002711 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2712 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2713 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2714 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2715 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002716 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002717 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002718 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002719 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002720 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002721 // list of many items
2722 compile_node(comp, pns2->nodes[0]);
2723 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002724 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002725 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002726 // list comprehension
2727 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2728 } else {
2729 // list with 2 items
2730 goto list_with_2_items;
2731 }
2732 } else {
2733 // list with 2 items
2734 list_with_2_items:
2735 compile_node(comp, pns2->nodes[0]);
2736 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002737 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002738 }
2739 } else {
2740 // list with 1 item
2741 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002742 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002743 }
2744}
2745
Damien George969a6b32014-12-10 22:07:04 +00002746STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002747 mp_parse_node_t pn = pns->nodes[0];
2748 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002749 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002750 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002751 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2752 pns = (mp_parse_node_struct_t*)pn;
2753 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002754 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002755 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002756 compile_node(comp, pn);
2757 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002758 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2759 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2760 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2761 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002762 // dict/set with multiple elements
2763
2764 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002765 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002766 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002767
2768 // first element sets whether it's a dict or set
2769 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002770 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002771 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002772 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002773 compile_node(comp, pns->nodes[0]);
2774 EMIT(store_map);
2775 is_dict = true;
2776 } else {
2777 // a set
2778 compile_node(comp, pns->nodes[0]); // 1st value of set
2779 is_dict = false;
2780 }
2781
2782 // process rest of elements
2783 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002784 mp_parse_node_t pn_i = nodes[i];
2785 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2786 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002787 if (is_dict) {
2788 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002789 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002790 return;
2791 }
2792 EMIT(store_map);
2793 } else {
2794 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002795 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002796 return;
2797 }
2798 }
2799 }
2800
Damien Georgee37dcaa2014-12-27 17:07:16 +00002801 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002802 // if it's a set, build it
2803 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002804 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002805 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002806 #endif
Damien George0bb97132015-02-27 14:25:47 +00002807 } else {
2808 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002809 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002810 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002811 // a dictionary comprehension
2812 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2813 } else {
2814 // a set comprehension
2815 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2816 }
Damien429d7192013-10-04 19:53:11 +01002817 }
2818 } else {
2819 // set with one element
2820 goto set_with_one_element;
2821 }
2822 } else {
2823 // set with one element
2824 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002825 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002826 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002827 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002828 #else
2829 assert(0);
2830 #endif
Damien429d7192013-10-04 19:53:11 +01002831 }
2832}
2833
Damien George969a6b32014-12-10 22:07:04 +00002834STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002835 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002836}
2837
Damien George969a6b32014-12-10 22:07:04 +00002838STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002839 // object who's index we want is on top of stack
2840 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002841 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002842}
2843
Damien George969a6b32014-12-10 22:07:04 +00002844STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002845 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002846 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002847}
2848
Damien George83204f32014-12-27 17:20:41 +00002849#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002850STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002851 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2852 mp_parse_node_t pn = pns->nodes[0];
2853 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002854 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002855 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2856 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002857 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2858 pns = (mp_parse_node_struct_t*)pn;
2859 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002861 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002862 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002863 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002864 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002865 } else {
2866 // [?::x]
2867 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002868 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002869 }
Damiend99b0522013-12-21 18:17:45 +00002870 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002871 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002872 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2873 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2874 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2875 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002876 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002877 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002878 } else {
2879 // [?:x:x]
2880 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002881 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002882 }
2883 } else {
2884 // [?:x]
2885 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002886 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002887 }
2888 } else {
2889 // [?:x]
2890 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002892 }
2893}
2894
Damien George969a6b32014-12-10 22:07:04 +00002895STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002896 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002897 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2898 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002899}
2900
Damien George969a6b32014-12-10 22:07:04 +00002901STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002902 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002903 compile_subscript_3_helper(comp, pns);
2904}
Damien George83204f32014-12-27 17:20:41 +00002905#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002906
Damien George969a6b32014-12-10 22:07:04 +00002907STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002908 // if this is called then we are compiling a dict key:value pair
2909 compile_node(comp, pns->nodes[1]); // value
2910 compile_node(comp, pns->nodes[0]); // key
2911}
2912
Damien George969a6b32014-12-10 22:07:04 +00002913STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002914 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002915 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002916 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002917}
2918
Damien George969a6b32014-12-10 22:07:04 +00002919STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002920 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002921 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002922 return;
2923 }
Damiend99b0522013-12-21 18:17:45 +00002924 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002925 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002926 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002927 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2928 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002929 compile_node(comp, pns->nodes[0]);
2930 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002931 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002932 EMIT(yield_from);
2933 } else {
2934 compile_node(comp, pns->nodes[0]);
2935 EMIT(yield_value);
2936 }
2937}
2938
Damien George65ef6b72015-01-14 21:17:27 +00002939STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2940 // only create and load the actual str object on the last pass
2941 if (comp->pass != MP_PASS_EMIT) {
2942 EMIT_ARG(load_const_obj, mp_const_none);
2943 } else {
2944 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2945 }
2946}
2947
2948STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2949 // only create and load the actual bytes object on the last pass
2950 if (comp->pass != MP_PASS_EMIT) {
2951 EMIT_ARG(load_const_obj, mp_const_none);
2952 } else {
2953 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2954 }
2955}
2956
Damien George7d414a12015-02-08 01:57:40 +00002957STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2958 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2959}
2960
Damiend99b0522013-12-21 18:17:45 +00002961typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002962STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002963#define nc NULL
2964#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002965#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002966#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002967#undef nc
2968#undef c
2969#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002970 NULL,
2971 compile_string,
2972 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002973 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002974};
2975
Damien George6be0b0a2014-08-15 14:30:52 +01002976STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002977 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002978 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002979 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002980 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002981 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002982 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002983 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002984 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002985 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002986 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2987 case MP_PARSE_NODE_BYTES:
2988 // only create and load the actual bytes object on the last pass
2989 if (comp->pass != MP_PASS_EMIT) {
2990 EMIT_ARG(load_const_obj, mp_const_none);
2991 } else {
2992 mp_uint_t len;
2993 const byte *data = qstr_data(arg, &len);
2994 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2995 }
2996 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002997 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002998 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002999 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01003000 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01003001 // do nothing
3002 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00003003 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01003004 }
3005 break;
Damien429d7192013-10-04 19:53:11 +01003006 }
3007 } else {
Damiend99b0522013-12-21 18:17:45 +00003008 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00003009 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00003010 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
3011 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01003012#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00003013 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
3014 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01003015#endif
Damien George65ef6b72015-01-14 21:17:27 +00003016 compile_syntax_error(comp, pn, "internal compiler error");
3017 } else {
3018 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01003019 }
3020 }
3021}
3022
Damien George2ac4af62014-08-15 16:45:41 +01003023STATIC 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 +01003024 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01003025 qstr param_name = MP_QSTR_NULL;
3026 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003027 if (MP_PARSE_NODE_IS_ID(pn)) {
3028 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01003029 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003030 // comes after a star, so counts as a keyword-only parameter
3031 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01003032 } else {
Damien George2827d622014-04-27 15:50:52 +01003033 // comes before a star, so counts as a positional parameter
3034 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003035 }
Damienb14de212013-10-06 00:28:28 +01003036 } else {
Damiend99b0522013-12-21 18:17:45 +00003037 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3038 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3039 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3040 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003041 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003042 // comes after a star, so counts as a keyword-only parameter
3043 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003044 } else {
Damien George2827d622014-04-27 15:50:52 +01003045 // comes before a star, so counts as a positional parameter
3046 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003047 }
Damiend99b0522013-12-21 18:17:45 +00003048 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003049 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003050 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003051 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003052 // bare star
3053 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003054 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003055 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003056 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003057 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003058 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00003059 } else {
3060 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01003061 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003062 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003063 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3064 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003065 }
Damien George0bb97132015-02-27 14:25:47 +00003066 } else {
3067 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00003068 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003069 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003070 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003071 }
Damien429d7192013-10-04 19:53:11 +01003072 }
3073
Damien George2827d622014-04-27 15:50:52 +01003074 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003075 bool added;
3076 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3077 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003078 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003079 return;
3080 }
Damien429d7192013-10-04 19:53:11 +01003081 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003082 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003083 }
3084}
3085
Damien George2827d622014-04-27 15:50:52 +01003086STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003087 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003088}
3089
Damien George2827d622014-04-27 15:50:52 +01003090STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003091 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3092}
3093
3094STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3095 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3096 // no annotation
3097 return;
3098 }
3099
3100 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3101 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3102 // named parameter with possible annotation
3103 // fallthrough
3104 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3105 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3106 // named star with possible annotation
3107 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3108 // fallthrough
3109 } else {
3110 // no annotation
3111 return;
3112 }
3113 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3114 // double star with possible annotation
3115 // fallthrough
3116 } else {
3117 // no annotation
3118 return;
3119 }
3120
3121 mp_parse_node_t pn_annotation = pns->nodes[1];
3122
3123 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3124 #if MICROPY_EMIT_NATIVE
3125 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3126 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3127 assert(id_info != NULL);
3128
3129 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3130 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3131 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3132 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3133 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003134 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003135 }
3136 }
3137 #endif // MICROPY_EMIT_NATIVE
3138 }
Damien429d7192013-10-04 19:53:11 +01003139}
3140
Damien George6be0b0a2014-08-15 14:30:52 +01003141STATIC 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 +01003142 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003143 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003144 // no more nested if/for; compile inner expression
3145 compile_node(comp, pn_inner_expr);
3146 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003147 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003148 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003149 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003150 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003151 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003152 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003153 #endif
Damien429d7192013-10-04 19:53:11 +01003154 } else {
3155 EMIT(yield_value);
3156 EMIT(pop_top);
3157 }
Damiend99b0522013-12-21 18:17:45 +00003158 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003159 // if condition
Damiend99b0522013-12-21 18:17:45 +00003160 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003161 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3162 pn_iter = pns_comp_if->nodes[1];
3163 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00003164 } else {
3165 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01003166 // for loop
Damiend99b0522013-12-21 18:17:45 +00003167 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003168 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003169 uint l_end2 = comp_next_label(comp);
3170 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003171 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003172 EMIT_ARG(label_assign, l_top2);
3173 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003174 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3175 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 +00003176 EMIT_ARG(jump, l_top2);
3177 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003178 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01003179 }
3180}
3181
Damien George1463c1f2014-04-25 23:52:57 +01003182STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3183#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003184 // see http://www.python.org/dev/peps/pep-0257/
3185
3186 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003187 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003188 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003189 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003190 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003191 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3192 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003193 for (int i = 0; i < num_nodes; i++) {
3194 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003195 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 +00003196 // not a newline, so this is the first statement; finish search
3197 break;
3198 }
3199 }
3200 // 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 +00003201 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003202 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003203 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003204 } else {
3205 return;
3206 }
3207
3208 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003209 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00003210 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003211 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3212 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3213 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3214 // compile the doc string
3215 compile_node(comp, pns->nodes[0]);
3216 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00003217 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003218 }
3219 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003220#else
3221 (void)comp;
3222 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003223#endif
Damien429d7192013-10-04 19:53:11 +01003224}
3225
Damien George1463c1f2014-04-25 23:52:57 +01003226STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003227 comp->pass = pass;
3228 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003229 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003230 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003231
Damien George36db6bc2014-05-07 17:24:22 +01003232 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003233 // reset maximum stack sizes in scope
3234 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003235 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003236 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003237 }
3238
Damien5ac1b2e2013-10-18 19:58:12 +01003239#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003240 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003241 scope_print_info(scope);
3242 }
Damien5ac1b2e2013-10-18 19:58:12 +01003243#endif
Damien429d7192013-10-04 19:53:11 +01003244
3245 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003246 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3247 assert(scope->kind == SCOPE_MODULE);
3248 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3249 compile_node(comp, pns->nodes[0]); // compile the expression
3250 EMIT(return_value);
3251 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003252 if (!comp->is_repl) {
3253 check_for_doc_string(comp, scope->pn);
3254 }
Damien429d7192013-10-04 19:53:11 +01003255 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003256 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003257 EMIT(return_value);
3258 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003259 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3260 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3261 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003262
3263 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003264 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003265 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003266 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003267 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003268 } else {
3269 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003270
Damien George2ac4af62014-08-15 16:45:41 +01003271 // argument annotations
3272 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3273
3274 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003275 mp_parse_node_t pn_annotation = pns->nodes[2];
3276 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3277 #if MICROPY_EMIT_NATIVE
3278 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3279 // nodes[2] can be null or a test-expr
3280 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3281 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3282 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3283 } else {
3284 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3285 }
Damien George2ac4af62014-08-15 16:45:41 +01003286 }
Damien Georgea5190a72014-08-15 22:39:08 +01003287 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003288 }
Damien George2ac4af62014-08-15 16:45:41 +01003289 }
Damien429d7192013-10-04 19:53:11 +01003290
3291 compile_node(comp, pns->nodes[3]); // 3 is function body
3292 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003293 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003294 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003295 EMIT(return_value);
3296 }
3297 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003298 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3299 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3300 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003301
3302 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003303 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003304 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003305 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003306 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3307 }
3308
3309 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003310
3311 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3312 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3313 EMIT(pop_top);
3314 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3315 }
Damien429d7192013-10-04 19:53:11 +01003316 EMIT(return_value);
3317 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3318 // a bit of a hack at the moment
3319
Damiend99b0522013-12-21 18:17:45 +00003320 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3321 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3322 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3323 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3324 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003325
Damien George708c0732014-04-27 19:23:46 +01003326 // We need a unique name for the comprehension argument (the iterator).
3327 // CPython uses .0, but we should be able to use anything that won't
3328 // clash with a user defined variable. Best to use an existing qstr,
3329 // so we use the blank qstr.
3330#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003331 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003332#else
3333 qstr qstr_arg = MP_QSTR_;
3334#endif
Damien George36db6bc2014-05-07 17:24:22 +01003335 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003336 bool added;
3337 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3338 assert(added);
3339 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003340 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003341 }
3342
3343 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003344 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003345 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003346 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003347 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003348 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003349 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003350 #endif
Damien429d7192013-10-04 19:53:11 +01003351 }
3352
Damien George6f355fd2014-04-10 14:11:31 +01003353 uint l_end = comp_next_label(comp);
3354 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00003355 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00003356 EMIT_ARG(label_assign, l_top);
3357 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003358 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3359 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003360 EMIT_ARG(jump, l_top);
3361 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003362 EMIT(for_iter_end);
3363
3364 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003365 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003366 }
3367 EMIT(return_value);
3368 } else {
3369 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003370 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3371 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3372 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003373
Damien George36db6bc2014-05-07 17:24:22 +01003374 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003375 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003376 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003377 assert(added);
3378 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003379 }
3380
Damien George542bd6b2015-03-26 14:42:40 +00003381 compile_load_id(comp, MP_QSTR___name__);
3382 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003383 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003384 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003385
3386 check_for_doc_string(comp, pns->nodes[2]);
3387 compile_node(comp, pns->nodes[2]); // 2 is class body
3388
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003389 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003390 assert(id != NULL);
3391 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003392 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003393 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003394#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003395 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003396#else
Damien George542bd6b2015-03-26 14:42:40 +00003397 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003398#endif
Damien429d7192013-10-04 19:53:11 +01003399 }
3400 EMIT(return_value);
3401 }
3402
Damien415eb6f2013-10-05 12:19:06 +01003403 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003404
3405 // make sure we match all the exception levels
3406 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003407}
3408
Damien George094d4502014-04-02 17:31:27 +01003409#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003410// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003411STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003412 comp->pass = pass;
3413 comp->scope_cur = scope;
3414 comp->next_label = 1;
3415
3416 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003417 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003418 return;
3419 }
3420
Damien George36db6bc2014-05-07 17:24:22 +01003421 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003422 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003423 }
3424
Damien826005c2013-10-05 23:17:28 +01003425 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003426 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3427 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3428 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003429
Damiend99b0522013-12-21 18:17:45 +00003430 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003431
Damiena2f2f7d2013-10-06 00:14:13 +01003432 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003433 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003434 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003435 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003436 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003437 if (comp->compile_error != MP_OBJ_NULL) {
3438 goto inline_asm_error;
3439 }
Damiena2f2f7d2013-10-06 00:14:13 +01003440 }
3441
Damiend99b0522013-12-21 18:17:45 +00003442 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003443
Damiend99b0522013-12-21 18:17:45 +00003444 mp_parse_node_t pn_body = pns->nodes[3]; // body
3445 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003446 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003447
Damien826005c2013-10-05 23:17:28 +01003448 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003449 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3450 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003451 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3452 // no instructions
3453 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003454 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003455 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003456 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003457 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003458 return;
3459 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003460
3461 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003462 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003463 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3464 goto not_an_instruction;
3465 }
Damiend99b0522013-12-21 18:17:45 +00003466 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003467 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3468 goto not_an_instruction;
3469 }
3470 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3471 goto not_an_instruction;
3472 }
3473 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3474 goto not_an_instruction;
3475 }
Damiend99b0522013-12-21 18:17:45 +00003476 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003477
3478 // parse node looks like an instruction
3479 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003480 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3481 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3482 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003483 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003484
3485 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003486 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003487 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003488 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003489 return;
3490 }
Damien George6f355fd2014-04-10 14:11:31 +01003491 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003492 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003493 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3494 compile_syntax_error(comp, nodes[i], "label redefined");
3495 return;
3496 }
Damien826005c2013-10-05 23:17:28 +01003497 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003498 } else if (op == MP_QSTR_align) {
3499 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003500 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003501 return;
3502 }
Damien George36db6bc2014-05-07 17:24:22 +01003503 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003504 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3505 }
3506 } else if (op == MP_QSTR_data) {
3507 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003508 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003509 return;
3510 }
Damien George36db6bc2014-05-07 17:24:22 +01003511 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003512 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003513 for (uint j = 1; j < n_args; j++) {
3514 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003515 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003516 return;
3517 }
Damien George50912e72015-01-20 11:55:10 +00003518 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003519 }
3520 }
Damien826005c2013-10-05 23:17:28 +01003521 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003522 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003523 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003524 }
3525 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003526
3527 if (comp->compile_error != MP_OBJ_NULL) {
3528 pns = pns2; // this is the parse node that had the error
3529 goto inline_asm_error;
3530 }
Damien826005c2013-10-05 23:17:28 +01003531 }
3532
Damien George36db6bc2014-05-07 17:24:22 +01003533 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003534 EMIT_INLINE_ASM(end_pass);
3535 }
3536
3537 if (comp->compile_error != MP_OBJ_NULL) {
3538 // inline assembler had an error; add traceback to its exception
3539 inline_asm_error:
3540 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, (mp_uint_t)pns->source_line, comp->scope_cur->simple_name);
Damienb05d7072013-10-05 13:37:10 +01003541 }
Damien429d7192013-10-04 19:53:11 +01003542}
Damien George094d4502014-04-02 17:31:27 +01003543#endif
Damien429d7192013-10-04 19:53:11 +01003544
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003545STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003546#if !MICROPY_EMIT_CPYTHON
3547 // in Micro Python we put the *x parameter after all other parameters (except **y)
3548 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3549 id_info_t *id_param = NULL;
3550 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3551 id_info_t *id = &scope->id_info[i];
3552 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3553 if (id_param != NULL) {
3554 // swap star param with last param
3555 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3556 }
3557 break;
3558 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3559 id_param = id;
3560 }
3561 }
3562 }
3563#endif
3564
Damien429d7192013-10-04 19:53:11 +01003565 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003566 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003567 scope->num_locals = 0;
3568 for (int i = 0; i < scope->id_info_len; i++) {
3569 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003570 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003571 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3572 continue;
3573 }
3574 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3575 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3576 }
Damien George2827d622014-04-27 15:50:52 +01003577 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003578 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003579 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003580 }
3581 }
3582
3583 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003584#if MICROPY_EMIT_CPYTHON
3585 int num_cell = 0;
3586#endif
Damien9ecbcff2013-12-11 00:41:43 +00003587 for (int i = 0; i < scope->id_info_len; i++) {
3588 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003589#if MICROPY_EMIT_CPYTHON
3590 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003591 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003592 id->local_num = num_cell;
3593 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003594 }
Damien George6baf76e2013-12-30 22:32:17 +00003595#else
3596 // in Micro Python the cells come right after the fast locals
3597 // parameters are not counted here, since they remain at the start
3598 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003599 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003600 id->local_num = scope->num_locals;
3601 scope->num_locals += 1;
3602 }
3603#endif
Damien9ecbcff2013-12-11 00:41:43 +00003604 }
Damien9ecbcff2013-12-11 00:41:43 +00003605
3606 // compute the index of free vars (freevars[idx] in CPython)
3607 // make sure they are in the order of the parent scope
3608 if (scope->parent != NULL) {
3609 int num_free = 0;
3610 for (int i = 0; i < scope->parent->id_info_len; i++) {
3611 id_info_t *id = &scope->parent->id_info[i];
3612 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3613 for (int j = 0; j < scope->id_info_len; j++) {
3614 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003615 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003616 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003617#if MICROPY_EMIT_CPYTHON
3618 // in CPython the frees are numbered after the cells
3619 id2->local_num = num_cell + num_free;
3620#else
3621 // in Micro Python the frees come first, before the params
3622 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003623#endif
3624 num_free += 1;
3625 }
3626 }
3627 }
Damien429d7192013-10-04 19:53:11 +01003628 }
Damien George6baf76e2013-12-30 22:32:17 +00003629#if !MICROPY_EMIT_CPYTHON
3630 // in Micro Python shift all other locals after the free locals
3631 if (num_free > 0) {
3632 for (int i = 0; i < scope->id_info_len; i++) {
3633 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003634 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003635 id->local_num += num_free;
3636 }
3637 }
Damien George2827d622014-04-27 15:50:52 +01003638 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 +00003639 scope->num_locals += num_free;
3640 }
3641#endif
Damien429d7192013-10-04 19:53:11 +01003642 }
3643
Damien George8725f8f2014-02-15 19:33:11 +00003644 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003645
3646#if MICROPY_EMIT_CPYTHON
3647 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003648 if (scope->kind == SCOPE_FUNCTION || scope->kind == SCOPE_LAMBDA || scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3649 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003650 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003651 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003652 if ((SCOPE_FUNCTION <= scope->parent->kind && scope->parent->kind <= SCOPE_SET_COMP) || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
Damien George8725f8f2014-02-15 19:33:11 +00003653 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003654 }
3655 }
Damien George882b3632014-04-02 15:56:31 +01003656#endif
3657
Damien429d7192013-10-04 19:53:11 +01003658 int num_free = 0;
3659 for (int i = 0; i < scope->id_info_len; i++) {
3660 id_info_t *id = &scope->id_info[i];
3661 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3662 num_free += 1;
3663 }
3664 }
3665 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003666 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003667 }
3668}
3669
Damien George65cad122014-04-06 11:48:15 +01003670mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
Damien Georgeb140bff2014-04-09 12:54:21 +01003671 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003672 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003673 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003674 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003675
Damien George62a3a282015-03-01 12:04:05 +00003676 // create the module scope
3677 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3678
3679 // optimise constants (scope must be set for error messages to work)
3680 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003681 mp_map_t consts;
3682 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003683 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003684 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003685
Damien Georgea210c772015-03-26 15:49:53 +00003686 // create standard emitter; it's used at least for MP_PASS_SCOPE
3687 #if MICROPY_EMIT_CPYTHON
3688 emit_t *emit_cpython = emit_cpython_new();
3689 #else
3690 emit_t *emit_bc = emit_bc_new();
3691 #endif
3692
Damien826005c2013-10-05 23:17:28 +01003693 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003694 #if MICROPY_EMIT_CPYTHON
3695 comp->emit = emit_cpython;
3696 comp->emit_method_table = &emit_cpython_method_table;
3697 #else
3698 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003699 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003700 comp->emit_method_table = &emit_bc_method_table;
3701 #endif
Damien George41125902015-03-26 16:44:14 +00003702 #endif
Damien Georgea77ffe62015-03-14 12:59:31 +00003703 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003704 comp->emit_inline_asm = NULL;
3705 comp->emit_inline_asm_method_table = NULL;
Damien Georgea77ffe62015-03-14 12:59:31 +00003706 #endif
Damien826005c2013-10-05 23:17:28 +01003707 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003708 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003709 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003710#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003711 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003712 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003713#endif
Damien826005c2013-10-05 23:17:28 +01003714 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003715 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003716 }
3717
3718 // update maximim number of labels needed
3719 if (comp->next_label > max_num_labels) {
3720 max_num_labels = comp->next_label;
3721 }
Damien429d7192013-10-04 19:53:11 +01003722 }
3723
Damien826005c2013-10-05 23:17:28 +01003724 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003725 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 +00003726 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003727 }
3728
Damien Georgea210c772015-03-26 15:49:53 +00003729 // set max number of labels now that it's calculated
3730 #if MICROPY_EMIT_CPYTHON
3731 emit_cpython_set_max_num_labels(emit_cpython, max_num_labels);
3732 #else
3733 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
3734 #endif
3735
Damien826005c2013-10-05 23:17:28 +01003736 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003737#if !MICROPY_EMIT_CPYTHON
Damien Georgee67ed5d2014-01-04 13:55:24 +00003738#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003739 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003740#endif
Damien3ef4abb2013-10-12 16:53:13 +01003741#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003742 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003743#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003744#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003745 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003746 if (false) {
3747 // dummy
3748
Damien3ef4abb2013-10-12 16:53:13 +01003749#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003750 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003751 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003752 if (emit_inline_thumb == NULL) {
3753 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3754 }
3755 comp->emit = NULL;
3756 comp->emit_method_table = NULL;
3757 comp->emit_inline_asm = emit_inline_thumb;
3758 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003759 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003760 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003761 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003762 }
Damienc025ebb2013-10-12 14:30:21 +01003763#endif
3764
Damien826005c2013-10-05 23:17:28 +01003765 } else {
Damienc025ebb2013-10-12 14:30:21 +01003766
3767 // choose the emit type
3768
Damien3ef4abb2013-10-12 16:53:13 +01003769#if MICROPY_EMIT_CPYTHON
Damien Georgea210c772015-03-26 15:49:53 +00003770 comp->emit = emit_cpython;
Damienc025ebb2013-10-12 14:30:21 +01003771 comp->emit_method_table = &emit_cpython_method_table;
3772#else
Damien826005c2013-10-05 23:17:28 +01003773 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003774
3775#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003776 case MP_EMIT_OPT_NATIVE_PYTHON:
3777 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003778#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003779 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003780 emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003781 }
Damien13ed3a62013-10-08 09:05:10 +01003782 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003783#elif MICROPY_EMIT_X86
3784 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003785 emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels);
Damien Georgec90f59e2014-09-06 23:06:36 +01003786 }
3787 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003788#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003789 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003790 emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels);
Damienc025ebb2013-10-12 14:30:21 +01003791 }
3792 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003793#elif MICROPY_EMIT_ARM
3794 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003795 emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003796 }
3797 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003798#endif
3799 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003800 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003801 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003802#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003803
Damien826005c2013-10-05 23:17:28 +01003804 default:
Damien826005c2013-10-05 23:17:28 +01003805 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003806 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003807 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003808 #endif
Damien826005c2013-10-05 23:17:28 +01003809 break;
3810 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003811#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003812
Damien George1e1779e2015-01-14 00:20:28 +00003813 // need a pass to compute stack size
3814 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3815
Damien George36db6bc2014-05-07 17:24:22 +01003816 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003817 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003818 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3819 }
3820
3821 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003822 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003823 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003824 }
Damien George84d59c22015-07-27 22:20:00 +01003825
3826 #if MICROPY_EMIT_NATIVE
3827 // if viper had an error then add traceback
3828 if (comp->compile_error != MP_OBJ_NULL && s->emit_options == MP_EMIT_OPT_VIPER) {
3829 compile_error_add_traceback(comp, s->pn);
3830 }
3831 #endif
Damien6cdd3af2013-10-05 18:08:26 +01003832 }
Damien429d7192013-10-04 19:53:11 +01003833 }
3834
Damien George41d02b62014-01-24 22:42:28 +00003835 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003836
3837#if MICROPY_EMIT_CPYTHON
3838 emit_cpython_free(emit_cpython);
3839#else
3840 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003841#if MICROPY_EMIT_NATIVE
3842 if (emit_native != NULL) {
3843#if MICROPY_EMIT_X64
3844 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003845#elif MICROPY_EMIT_X86
3846 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003847#elif MICROPY_EMIT_THUMB
3848 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003849#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003850 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003851#endif
3852 }
3853#endif
3854#if MICROPY_EMIT_INLINE_THUMB
3855 if (emit_inline_thumb != NULL) {
3856 emit_inline_thumb_free(emit_inline_thumb);
3857 }
3858#endif
Damien Georgea210c772015-03-26 15:49:53 +00003859#endif // MICROPY_EMIT_CPYTHON
Damien George41d02b62014-01-24 22:42:28 +00003860
Damien George52b5d762014-09-23 15:31:56 +00003861 // free the parse tree
Damien George62a3a282015-03-01 12:04:05 +00003862 mp_parse_node_free(module_scope->pn);
Damien George52b5d762014-09-23 15:31:56 +00003863
Damien George41d02b62014-01-24 22:42:28 +00003864 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003865 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003866 for (scope_t *s = module_scope; s;) {
3867 scope_t *next = s->next;
3868 scope_free(s);
3869 s = next;
3870 }
Damien5ac1b2e2013-10-18 19:58:12 +01003871
Damien George41d02b62014-01-24 22:42:28 +00003872 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003873 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003874 m_del_obj(compiler_t, comp);
3875
Damien Georgea91ac202014-10-05 19:01:34 +01003876 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003877 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003878 } else {
3879#if MICROPY_EMIT_CPYTHON
3880 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003881 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003882 return mp_const_true;
3883#else
3884 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003885 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003886#endif
3887 }
Damien429d7192013-10-04 19:53:11 +01003888}