blob: 81a9dc9eaa4e598df1c791bb07166651444fc982 [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>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
Damiend99b0522013-12-21 18:17:45 +000034#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030035#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010037#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010038#include "parse.h"
Damiend99b0522013-12-21 18:17:45 +000039#include "runtime0.h"
Damien George1fb03172014-01-03 14:22:03 +000040#include "obj.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010041#include "emitglue.h"
42#include "scope.h"
43#include "emit.h"
Damien George1fb03172014-01-03 14:22:03 +000044#include "compile.h"
45#include "runtime.h"
Damien Georgea26dc502014-04-12 17:54:52 +010046#include "builtin.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000047#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010048
49// TODO need to mangle __attr names
50
51typedef enum {
52 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000053#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010054#include "grammar.h"
55#undef DEF_RULE
56 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010057 PN_string, // special node for non-interned string
Damien429d7192013-10-04 19:53:11 +010058} pn_kind_t;
59
Damien Georgeb9791222014-01-23 00:34:21 +000060#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
61#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
62#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
63#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 +010064
Damien Georgea91ac202014-10-05 19:01:34 +010065// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010066typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000067 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010068
Damien George78035b92014-04-09 12:27:39 +010069 uint8_t is_repl;
70 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010071 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010072 uint8_t have_star;
73
74 // try to keep compiler clean from nlr
75 // this is set to an exception object if we have a compile error
76 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010077
Damien George6f355fd2014-04-10 14:11:31 +010078 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010079
Damien George69b89d22014-04-11 13:38:30 +000080 uint16_t num_dict_params;
81 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010082
Damien Georgea91ac202014-10-05 19:01:34 +010083 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
84 uint16_t continue_label;
85 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000086 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010087
Damien429d7192013-10-04 19:53:11 +010088 scope_t *scope_head;
89 scope_t *scope_cur;
90
Damien6cdd3af2013-10-05 18:08:26 +010091 emit_t *emit; // current emitter
92 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010093
94 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
95 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010096} compiler_t;
97
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010098STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010099 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
100 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100101 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien Georgea91ac202014-10-05 19:01:34 +0100102 mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100103 } else {
Damien Georgea91ac202014-10-05 19:01:34 +0100104 // we don't have a line number, so just pass 0
105 mp_obj_exception_add_traceback(exc, comp->source_file, 0, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100106 }
Damien Georgea91ac202014-10-05 19:01:34 +0100107 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000108}
109
Damien George57e99eb2014-04-10 22:42:11 +0100110STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300111 #if MICROPY_PY_UCTYPES
112 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
113 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100114 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100115 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100116};
117
118STATIC const mp_map_t mp_constants_map = {
119 .all_keys_are_qstrs = 1,
120 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200121 .used = MP_ARRAY_SIZE(mp_constants_table),
122 .alloc = MP_ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +0100123 .table = (mp_map_elem_t*)mp_constants_table,
124};
125
Damien Georgeffae48d2014-05-08 15:58:39 +0000126// this function is essentially a simple preprocessor
127STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
128 if (0) {
129 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100130#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000131 } else if (MP_PARSE_NODE_IS_ID(pn)) {
132 // lookup identifier in table of dynamic constants
133 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
134 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
135 if (elem != NULL) {
136 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
137 }
138#endif
139 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000140 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100141
Damien Georgeffae48d2014-05-08 15:58:39 +0000142 // fold some parse nodes before folding their arguments
143 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100144#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000145 case PN_expr_stmt:
146 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
147 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
148 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
149 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
150 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
151 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
152 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
153 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
154 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
155 ) {
156 // code to assign dynamic constants: id = const(value)
157
158 // get the id
159 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
160
161 // get the value
162 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
163 pn_value = fold_constants(comp, pn_value, consts);
164 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
165 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
166 break;
167 }
Damien George40f3c022014-07-03 13:25:24 +0100168 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000169
170 // store the value in the table of dynamic constants
171 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
172 if (elem->value != MP_OBJ_NULL) {
173 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
174 break;
175 }
176 elem->value = MP_OBJ_NEW_SMALL_INT(value);
177
178 // replace const(value) with value
179 pns1->nodes[0] = pn_value;
180
181 // finished folding this assignment
182 return pn;
183 }
184 }
185 }
186 break;
187#endif
Damien George5042bce2014-05-25 22:06:06 +0100188 case PN_string:
189 return pn;
Damien429d7192013-10-04 19:53:11 +0100190 }
191
Damien Georgeffae48d2014-05-08 15:58:39 +0000192 // fold arguments
193 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
194 for (int i = 0; i < n; i++) {
195 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
196 }
197
198 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000199 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100200 case PN_atom_paren:
201 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
202 // (int)
203 pn = pns->nodes[0];
204 }
205 break;
206
207 case PN_expr:
208 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
209 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100210 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
211 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100212 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
213 }
214 break;
215
216 case PN_and_expr:
217 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
218 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100219 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
220 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100221 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
222 }
223 break;
224
Damien429d7192013-10-04 19:53:11 +0100225 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000226 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 +0100227 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
228 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000229 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100230 // int << int
231 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
232 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
233 }
Damiend99b0522013-12-21 18:17:45 +0000234 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100235 // int >> int
Damiend99b0522013-12-21 18:17:45 +0000236 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100237 } else {
238 // shouldn't happen
239 assert(0);
240 }
241 }
242 break;
243
244 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100245 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000246 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 +0100247 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
248 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000249 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100250 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000251 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000252 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100253 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000254 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100255 } else {
256 // shouldn't happen
257 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100258 }
Damien Georged1e355e2014-05-28 14:51:12 +0100259 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100260 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000261 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100262 }
263 }
264 break;
265
266 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000267 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 +0100268 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
269 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000270 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100271 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000272 if (!mp_small_int_mul_overflow(arg0, arg1)) {
273 arg0 *= arg1;
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);
276 }
277 }
Damiend99b0522013-12-21 18:17:45 +0000278 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100279 // int / int
280 // pass
Damiend99b0522013-12-21 18:17:45 +0000281 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100282 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000283 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000284 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300285 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100286 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000287 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 +0300288 }
Damien429d7192013-10-04 19:53:11 +0100289 } else {
290 // shouldn't happen
291 assert(0);
292 }
293 }
294 break;
295
296 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000297 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100298 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000299 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100300 // +int
Damiend99b0522013-12-21 18:17:45 +0000301 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
302 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100303 // -int
Damiend99b0522013-12-21 18:17:45 +0000304 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
305 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100306 // ~int
Damiend99b0522013-12-21 18:17:45 +0000307 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100308 } else {
309 // shouldn't happen
310 assert(0);
311 }
312 }
313 break;
314
315 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100316 if (0) {
317#if MICROPY_EMIT_CPYTHON
318 } 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 +0100319 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100320 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000321 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
322 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200323 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100324 if (power >= 0) {
325 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200326 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100327 for (; power > 0; power--) {
328 ans *= base;
329 }
Damiend99b0522013-12-21 18:17:45 +0000330 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100331 }
332 }
Damien George57e99eb2014-04-10 22:42:11 +0100333#endif
334 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
335 // id.id
336 // look it up in constant table, see if it can be replaced with an integer
337 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
338 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
339 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
340 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
341 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
342 if (elem != NULL) {
343 mp_obj_t dest[2];
344 mp_load_method_maybe(elem->value, q_attr, dest);
345 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100346 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georged1e355e2014-05-28 14:51:12 +0100347 if (MP_SMALL_INT_FITS(val)) {
Damien George57e99eb2014-04-10 22:42:11 +0100348 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
349 }
350 }
351 }
Damien429d7192013-10-04 19:53:11 +0100352 }
353 break;
354 }
355 }
356
357 return pn;
358}
359
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200360STATIC 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 +0100361STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000362STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100363
Damien George6f355fd2014-04-10 14:11:31 +0100364STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100365 return comp->next_label++;
366}
367
Damien George8dcc0c72014-03-27 10:55:21 +0000368STATIC void compile_increase_except_level(compiler_t *comp) {
369 comp->cur_except_level += 1;
370 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
371 comp->scope_cur->exc_stack_size = comp->cur_except_level;
372 }
373}
374
375STATIC void compile_decrease_except_level(compiler_t *comp) {
376 assert(comp->cur_except_level > 0);
377 comp->cur_except_level -= 1;
378}
379
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200380STATIC 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 +0100381 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100382 scope->parent = comp->scope_cur;
383 scope->next = NULL;
384 if (comp->scope_head == NULL) {
385 comp->scope_head = scope;
386 } else {
387 scope_t *s = comp->scope_head;
388 while (s->next != NULL) {
389 s = s->next;
390 }
391 s->next = scope;
392 }
393 return scope;
394}
395
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200396STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000397 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
398 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
399 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100400 for (int i = 0; i < num_nodes; i++) {
401 f(comp, pns->nodes[i]);
402 }
Damiend99b0522013-12-21 18:17:45 +0000403 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100404 f(comp, pn);
405 }
406}
407
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200408STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000409 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100410 *nodes = NULL;
411 return 0;
Damiend99b0522013-12-21 18:17:45 +0000412 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100413 *nodes = pn;
414 return 1;
415 } else {
Damiend99b0522013-12-21 18:17:45 +0000416 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
417 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100418 *nodes = pn;
419 return 1;
420 } else {
421 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000422 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100423 }
424 }
425}
426
Damiend99b0522013-12-21 18:17:45 +0000427void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100428}
429
Damiend99b0522013-12-21 18:17:45 +0000430void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
431 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100432 for (int i = 0; i < num_nodes; i++) {
433 compile_node(comp, pns->nodes[i]);
434 }
435}
436
Damien3ef4abb2013-10-12 16:53:13 +0100437#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200438STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100439 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
440 return true;
441 }
Damiend99b0522013-12-21 18:17:45 +0000442 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100443 return false;
444 }
Damiend99b0522013-12-21 18:17:45 +0000445 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100446 return false;
447 }
448 return true;
449}
450
Damien George5042bce2014-05-25 22:06:06 +0100451STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000452 bool has_single_quote = false;
453 bool has_double_quote = false;
454 for (int i = 0; i < len; i++) {
455 if (str[i] == '\'') {
456 has_single_quote = true;
457 } else if (str[i] == '"') {
458 has_double_quote = true;
459 }
460 }
461 if (bytes) {
462 vstr_printf(vstr, "b");
463 }
464 bool quote_single = false;
465 if (has_single_quote && !has_double_quote) {
466 vstr_printf(vstr, "\"");
467 } else {
468 quote_single = true;
469 vstr_printf(vstr, "'");
470 }
471 for (int i = 0; i < len; i++) {
472 if (str[i] == '\n') {
473 vstr_printf(vstr, "\\n");
474 } else if (str[i] == '\\') {
475 vstr_printf(vstr, "\\\\");
476 } else if (str[i] == '\'' && quote_single) {
477 vstr_printf(vstr, "\\'");
478 } else {
479 vstr_printf(vstr, "%c", str[i]);
480 }
481 }
482 if (has_single_quote && !has_double_quote) {
483 vstr_printf(vstr, "\"");
484 } else {
485 vstr_printf(vstr, "'");
486 }
487}
488
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200489STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George5042bce2014-05-25 22:06:06 +0100490 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
491 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George40f3c022014-07-03 13:25:24 +0100492 cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false);
Damien George5042bce2014-05-25 22:06:06 +0100493 return;
494 }
495
Damiend99b0522013-12-21 18:17:45 +0000496 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200497 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
498 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
499 return;
500 }
501
Damien George42f3de92014-10-03 17:44:14 +0000502 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000503 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
504 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000505 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
506 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100507 case MP_PARSE_NODE_STRING:
508 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100509 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100510 const byte *str = qstr_data(arg, &len);
511 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
512 break;
513 }
Damiend99b0522013-12-21 18:17:45 +0000514 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100515 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000516 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
517 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
518 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100519 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100520 }
521 break;
522 default: assert(0);
523 }
524}
525
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200526STATIC 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 +0100527 int n = 0;
528 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000529 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100530 }
531 int total = n;
532 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000533 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100534 total += 1;
Damien3a205172013-10-12 15:01:56 +0100535 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100536 is_const = false;
537 }
538 }
539 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100540 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100541 is_const = false;
542 break;
543 }
544 }
545 if (total > 0 && is_const) {
546 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000547 vstr_t *vstr = vstr_new();
548 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000549 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000550 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100551 need_comma = true;
552 }
553 for (int i = 0; i < n; i++) {
554 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000555 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100556 }
Damien02f89412013-12-12 15:13:36 +0000557 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100558 need_comma = true;
559 }
560 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000561 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100562 } else {
Damien02f89412013-12-12 15:13:36 +0000563 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100564 }
Damien Georgeb9791222014-01-23 00:34:21 +0000565 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000566 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100567 } else {
Damiend99b0522013-12-21 18:17:45 +0000568 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100569 compile_node(comp, pn);
570 }
571 for (int i = 0; i < n; i++) {
572 compile_node(comp, pns_list->nodes[i]);
573 }
Damien Georgeb9791222014-01-23 00:34:21 +0000574 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100575 }
576}
Damien3a205172013-10-12 15:01:56 +0100577#endif
578
579// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100580STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100581#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100582 cpython_c_tuple(comp, pn, pns_list);
583#else
584 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000585 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100586 compile_node(comp, pn);
587 total += 1;
588 }
589 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000590 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100591 for (int i = 0; i < n; i++) {
592 compile_node(comp, pns_list->nodes[i]);
593 }
594 total += n;
595 }
Damien Georgeb9791222014-01-23 00:34:21 +0000596 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100597#endif
598}
Damien429d7192013-10-04 19:53:11 +0100599
Damiend99b0522013-12-21 18:17:45 +0000600void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100601 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000602 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100603}
604
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200605STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000606 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200607 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100608}
609
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200610STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200611 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100612}
613
Damien3ef4abb2013-10-12 16:53:13 +0100614#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100615// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200616STATIC 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 +0100617 if (node_is_const_false(pn)) {
618 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000619 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100620 }
621 return;
622 } else if (node_is_const_true(pn)) {
623 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000624 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100625 }
626 return;
Damiend99b0522013-12-21 18:17:45 +0000627 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
628 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
629 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
630 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100631 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100632 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100633 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100634 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100635 }
Damien3a205172013-10-12 15:01:56 +0100636 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000637 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100638 } else {
639 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100640 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100641 }
642 }
643 return;
Damiend99b0522013-12-21 18:17:45 +0000644 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100645 if (jump_if == false) {
646 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100647 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100648 }
649 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100650 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100651 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100652 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100653 }
Damien3a205172013-10-12 15:01:56 +0100654 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000655 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100656 }
657 return;
Damiend99b0522013-12-21 18:17:45 +0000658 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100659 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100660 return;
661 }
662 }
663
664 // nothing special, fall back to default compiling for node and jump
665 compile_node(comp, pn);
666 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000667 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100668 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000669 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100670 }
671}
Damien3a205172013-10-12 15:01:56 +0100672#endif
Damien429d7192013-10-04 19:53:11 +0100673
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200674STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100675#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100676 cpython_c_if_cond(comp, pn, jump_if, label, false);
677#else
678 if (node_is_const_false(pn)) {
679 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000680 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100681 }
682 return;
683 } else if (node_is_const_true(pn)) {
684 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000685 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100686 }
687 return;
Damiend99b0522013-12-21 18:17:45 +0000688 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
689 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
690 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
691 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100692 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100693 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100694 for (int i = 0; i < n - 1; i++) {
695 c_if_cond(comp, pns->nodes[i], true, label2);
696 }
697 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000698 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100699 } else {
700 for (int i = 0; i < n; i++) {
701 c_if_cond(comp, pns->nodes[i], true, label);
702 }
703 }
704 return;
Damiend99b0522013-12-21 18:17:45 +0000705 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100706 if (jump_if == false) {
707 for (int i = 0; i < n; i++) {
708 c_if_cond(comp, pns->nodes[i], false, label);
709 }
710 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100711 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100712 for (int i = 0; i < n - 1; i++) {
713 c_if_cond(comp, pns->nodes[i], false, label2);
714 }
715 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000716 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100717 }
718 return;
Damiend99b0522013-12-21 18:17:45 +0000719 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100720 c_if_cond(comp, pns->nodes[0], !jump_if, label);
721 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100722 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
723 // cond is something in parenthesis
724 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
725 // empty tuple, acts as false for the condition
726 if (jump_if == false) {
727 EMIT_ARG(jump, label);
728 }
729 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
730 // non-empty tuple, acts as true for the condition
731 if (jump_if == true) {
732 EMIT_ARG(jump, label);
733 }
734 } else {
735 // parenthesis around 1 item, is just that item
736 c_if_cond(comp, pns->nodes[0], jump_if, label);
737 }
738 return;
Damien3a205172013-10-12 15:01:56 +0100739 }
740 }
741
742 // nothing special, fall back to default compiling for node and jump
743 compile_node(comp, pn);
744 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000745 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100746 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000747 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100748 }
749#endif
Damien429d7192013-10-04 19:53:11 +0100750}
751
752typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100753STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100754
Damien George6be0b0a2014-08-15 14:30:52 +0100755STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100756 if (assign_kind != ASSIGN_AUG_STORE) {
757 compile_node(comp, pns->nodes[0]);
758 }
759
Damiend99b0522013-12-21 18:17:45 +0000760 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
761 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
762 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
763 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100764 if (assign_kind != ASSIGN_AUG_STORE) {
765 for (int i = 0; i < n - 1; i++) {
766 compile_node(comp, pns1->nodes[i]);
767 }
768 }
Damiend99b0522013-12-21 18:17:45 +0000769 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
770 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100771 }
Damiend99b0522013-12-21 18:17:45 +0000772 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100773 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000774 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100775 if (assign_kind == ASSIGN_AUG_STORE) {
776 EMIT(rot_three);
777 EMIT(store_subscr);
778 } else {
779 compile_node(comp, pns1->nodes[0]);
780 if (assign_kind == ASSIGN_AUG_LOAD) {
781 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100782 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100783 } else {
784 EMIT(store_subscr);
785 }
786 }
Damiend99b0522013-12-21 18:17:45 +0000787 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
788 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100789 if (assign_kind == ASSIGN_AUG_LOAD) {
790 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000791 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100792 } else {
793 if (assign_kind == ASSIGN_AUG_STORE) {
794 EMIT(rot_two);
795 }
Damien Georgeb9791222014-01-23 00:34:21 +0000796 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100797 }
798 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100799 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100800 }
801 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100802 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100803 }
804
Damiend99b0522013-12-21 18:17:45 +0000805 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100806 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100807 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100808
809 return;
810
811cannot_assign:
812 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100813}
814
Damien George0288cf02014-04-11 11:53:00 +0000815// 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 +0100816STATIC 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 +0000817 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
818
819 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100820 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000821 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
822 EMIT_ARG(unpack_ex, 0, num_tail);
823 have_star_index = 0;
824 }
825 for (int i = 0; i < num_tail; i++) {
826 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100827 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000828 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
829 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100830 } else {
Damien George9d181f62014-04-27 16:55:27 +0100831 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100832 return;
833 }
834 }
835 }
836 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000837 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100838 }
Damien George0288cf02014-04-11 11:53:00 +0000839 if (num_head != 0) {
840 if (0 == have_star_index) {
841 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100842 } else {
Damien George0288cf02014-04-11 11:53:00 +0000843 c_assign(comp, node_head, ASSIGN_STORE);
844 }
845 }
846 for (int i = 0; i < num_tail; i++) {
847 if (num_head + i == have_star_index) {
848 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
849 } else {
850 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100851 }
852 }
853}
854
855// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100856STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100857 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000858 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100859 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000860 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
861 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000862 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100863 switch (assign_kind) {
864 case ASSIGN_STORE:
865 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000866 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100867 break;
868 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000869 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100870 break;
871 }
872 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100873 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100874 return;
875 }
876 } else {
Damiend99b0522013-12-21 18:17:45 +0000877 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
878 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100879 case PN_power:
880 // lhs is an index or attribute
881 c_assign_power(comp, pns, assign_kind);
882 break;
883
884 case PN_testlist_star_expr:
885 case PN_exprlist:
886 // lhs is a tuple
887 if (assign_kind != ASSIGN_STORE) {
888 goto bad_aug;
889 }
Damien George0288cf02014-04-11 11:53:00 +0000890 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100891 break;
892
893 case PN_atom_paren:
894 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000895 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100896 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100897 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000898 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
899 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100900 goto testlist_comp;
901 } else {
902 // parenthesis around 1 item, is just that item
903 pn = pns->nodes[0];
904 goto tail_recursion;
905 }
906 break;
907
908 case PN_atom_bracket:
909 // lhs is something in brackets
910 if (assign_kind != ASSIGN_STORE) {
911 goto bad_aug;
912 }
Damiend99b0522013-12-21 18:17:45 +0000913 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100914 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000915 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000916 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
917 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100918 goto testlist_comp;
919 } else {
920 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000921 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100922 }
923 break;
924
925 default:
Damien George9d181f62014-04-27 16:55:27 +0100926 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100927 }
928 return;
929
930 testlist_comp:
931 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000932 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
933 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
934 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100935 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000936 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000937 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000938 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100939 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000940 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
941 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000942 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100943 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100944 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100945 } else {
946 // sequence with 2 items
947 goto sequence_with_2_items;
948 }
949 } else {
950 // sequence with 2 items
951 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000952 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100953 }
954 return;
955 }
956 return;
957
Damien George9d181f62014-04-27 16:55:27 +0100958 cannot_assign:
959 compile_syntax_error(comp, pn, "can't assign to expression");
960 return;
961
Damien429d7192013-10-04 19:53:11 +0100962 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100963 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100964}
965
966// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100967// if we are not in CPython compatibility mode then:
968// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
969// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
970// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100971STATIC 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 +0100972 assert(n_pos_defaults >= 0);
973 assert(n_kw_defaults >= 0);
974
Damien429d7192013-10-04 19:53:11 +0100975 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000976 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100977 int nfree = 0;
978 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000979 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
980 id_info_t *id = &comp->scope_cur->id_info[i];
981 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
982 for (int j = 0; j < this_scope->id_info_len; j++) {
983 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100984 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000985#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100986 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000987#else
988 // in Micro Python we load closures using LOAD_FAST
Damien George7ff996c2014-09-08 23:05:16 +0100989 EMIT_ARG(load_fast, id->qst, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000990#endif
Damien318aec62013-12-10 18:28:17 +0000991 nfree += 1;
992 }
993 }
Damien429d7192013-10-04 19:53:11 +0100994 }
995 }
996 }
Damien429d7192013-10-04 19:53:11 +0100997
998 // make the function/closure
999 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001000 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001001 } else {
Damien George3558f622014-04-20 17:50:40 +01001002 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001003 }
1004}
1005
Damien George6be0b0a2014-08-15 14:30:52 +01001006STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001007 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001008 comp->have_star = true;
1009 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001010 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1011 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001012 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001013 } else {
1014 // named star
Damien429d7192013-10-04 19:53:11 +01001015 }
Damien George8b19db02014-04-11 23:25:34 +01001016 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001017
1018 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001019 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001020 // TODO do we need to do anything with this?
1021
1022 } else {
1023 mp_parse_node_t pn_id;
1024 mp_parse_node_t pn_colon;
1025 mp_parse_node_t pn_equal;
1026 if (MP_PARSE_NODE_IS_ID(pn)) {
1027 // this parameter is just an id
1028
1029 pn_id = pn;
1030 pn_colon = MP_PARSE_NODE_NULL;
1031 pn_equal = MP_PARSE_NODE_NULL;
1032
1033 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1034 // this parameter has a colon and/or equal specifier
1035
1036 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1037 pn_id = pns->nodes[0];
1038 pn_colon = pns->nodes[1];
1039 pn_equal = pns->nodes[2];
1040
1041 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001042 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001043 assert(0);
1044 return;
1045 }
1046
1047 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1048 // this parameter does not have a default value
1049
1050 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001051 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001052 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001053 return;
1054 }
1055
1056 } else {
1057 // this parameter has a default value
1058 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1059
Damien George8b19db02014-04-11 23:25:34 +01001060 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001061 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001062#if MICROPY_EMIT_CPYTHON
1063 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1064 compile_node(comp, pn_equal);
1065#else
Damien George69b89d22014-04-11 13:38:30 +00001066 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1067 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001068 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1069 // we need to do this here before we start building the map for the default keywords
1070 if (comp->num_default_params > 0) {
1071 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001072 } else {
1073 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001074 }
Damien George69b89d22014-04-11 13:38:30 +00001075 // first default dict param, so make the map
1076 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001077 }
Damien Georgef0778a72014-06-07 22:01:00 +01001078
1079 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001080 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001081 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001082 EMIT(store_map);
1083#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001084 } else {
Damien George69b89d22014-04-11 13:38:30 +00001085 comp->num_default_params += 1;
1086 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001087 }
1088 }
1089
1090 // TODO pn_colon not implemented
1091 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001092 }
1093}
1094
1095// leaves function object on stack
1096// returns function name
Damiend99b0522013-12-21 18:17:45 +00001097qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001098 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001099 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001100 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001101 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001102 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001103 }
1104
1105 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001106 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001107 uint old_num_dict_params = comp->num_dict_params;
1108 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001109
1110 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001111 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001112 comp->num_dict_params = 0;
1113 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001114 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001115
Damien Georgea91ac202014-10-05 19:01:34 +01001116 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001117 return MP_QSTR_NULL;
1118 }
1119
Damien Georgee337f1e2014-03-31 15:18:37 +01001120#if !MICROPY_EMIT_CPYTHON
1121 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001122 // the default keywords args may have already made the tuple; if not, do it now
1123 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001124 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001125 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001126 }
1127#endif
1128
Damien429d7192013-10-04 19:53:11 +01001129 // get the scope for this function
1130 scope_t *fscope = (scope_t*)pns->nodes[4];
1131
1132 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001133 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001134
1135 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001136 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001137 comp->num_dict_params = old_num_dict_params;
1138 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001139
1140 // return its name (the 'f' in "def f(...):")
1141 return fscope->simple_name;
1142}
1143
1144// leaves class object on stack
1145// returns class name
Damiend99b0522013-12-21 18:17:45 +00001146qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001147 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001148 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001149 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001150 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001151 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001152 }
1153
1154 EMIT(load_build_class);
1155
1156 // scope for this class
1157 scope_t *cscope = (scope_t*)pns->nodes[3];
1158
1159 // compile the class
1160 close_over_variables_etc(comp, cscope, 0, 0);
1161
1162 // get its name
Damien George968bf342014-04-27 19:12:05 +01001163 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001164
1165 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001166 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1167 mp_parse_node_t parents = pns->nodes[1];
1168 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1169 parents = MP_PARSE_NODE_NULL;
1170 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001171 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001172 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001173
1174 // return its name (the 'C' in class C(...):")
1175 return cscope->simple_name;
1176}
1177
Damien6cdd3af2013-10-05 18:08:26 +01001178// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001179STATIC 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 +00001180 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001181 return false;
1182 }
1183
1184 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001185 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001186 return true;
1187 }
1188
Damiend99b0522013-12-21 18:17:45 +00001189 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001190 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001191 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001192#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001193 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001194 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001195 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001196 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001197#endif
Damien3ef4abb2013-10-12 16:53:13 +01001198#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001199 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001200 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001201#endif
Damien6cdd3af2013-10-05 18:08:26 +01001202 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001203 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001204 }
1205
1206 return true;
1207}
1208
Damiend99b0522013-12-21 18:17:45 +00001209void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001210 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001211 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001212 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1213
Damien6cdd3af2013-10-05 18:08:26 +01001214 // inherit emit options for this function/class definition
1215 uint emit_options = comp->scope_cur->emit_options;
1216
1217 // compile each decorator
1218 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001219 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001220 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1221 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001222
1223 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001224 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001225 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1226
1227 // check for built-in decorators
1228 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1229 // this was a built-in
1230 num_built_in_decorators += 1;
1231
1232 } else {
1233 // not a built-in, compile normally
1234
1235 // compile the decorator function
1236 compile_node(comp, name_nodes[0]);
1237 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001238 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001239 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001240 }
1241
1242 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001243 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001244 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001245 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001246 compile_node(comp, pns_decorator->nodes[1]);
1247 }
Damien429d7192013-10-04 19:53:11 +01001248 }
1249 }
1250
1251 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001252 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001253 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001254 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001255 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001256 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001257 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001258 } else {
1259 // shouldn't happen
1260 assert(0);
1261 }
1262
1263 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001264 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001265 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001266 }
1267
1268 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001269 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001270}
1271
Damiend99b0522013-12-21 18:17:45 +00001272void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001273 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001274 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001275 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001276}
1277
Damien George6be0b0a2014-08-15 14:30:52 +01001278STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001279 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001280 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001281 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1282 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001283
1284 compile_node(comp, pns->nodes[0]); // base of the power node
1285
Damiend99b0522013-12-21 18:17:45 +00001286 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1287 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1288 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1289 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001290 for (int i = 0; i < n - 1; i++) {
1291 compile_node(comp, pns1->nodes[i]);
1292 }
Damiend99b0522013-12-21 18:17:45 +00001293 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1294 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001295 }
Damiend99b0522013-12-21 18:17:45 +00001296 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001297 // can't delete function calls
1298 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001299 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001300 compile_node(comp, pns1->nodes[0]);
1301 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001302 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1303 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001304 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001305 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001306 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001307 }
1308 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001309 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001310 }
1311
Damiend99b0522013-12-21 18:17:45 +00001312 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001313 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001314 }
Damiend99b0522013-12-21 18:17:45 +00001315 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1316 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1317 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1318 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001319 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1320
Damiend99b0522013-12-21 18:17:45 +00001321 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1322 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1323 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001324 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001325 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001326 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001327 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001328 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001329 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001330 c_del_stmt(comp, pns->nodes[0]);
1331 for (int i = 0; i < n; i++) {
1332 c_del_stmt(comp, pns1->nodes[i]);
1333 }
Damiend99b0522013-12-21 18:17:45 +00001334 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001335 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001336 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001337 } else {
1338 // sequence with 2 items
1339 goto sequence_with_2_items;
1340 }
1341 } else {
1342 // sequence with 2 items
1343 sequence_with_2_items:
1344 c_del_stmt(comp, pns->nodes[0]);
1345 c_del_stmt(comp, pns->nodes[1]);
1346 }
1347 } else {
1348 // tuple with 1 element
1349 c_del_stmt(comp, pn);
1350 }
1351 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001352 // TODO is there anything else to implement?
1353 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001354 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001355
1356 return;
1357
1358cannot_delete:
1359 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001360}
1361
Damiend99b0522013-12-21 18:17:45 +00001362void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001363 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1364}
1365
Damiend99b0522013-12-21 18:17:45 +00001366void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001367 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001368 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001369 }
Damien George090c9232014-10-17 14:08:49 +00001370 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001371 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001372}
1373
Damiend99b0522013-12-21 18:17:45 +00001374void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001375 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001376 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001377 }
Damien George090c9232014-10-17 14:08:49 +00001378 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001379 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001380}
1381
Damiend99b0522013-12-21 18:17:45 +00001382void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001383 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001384 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001385 return;
1386 }
Damiend99b0522013-12-21 18:17:45 +00001387 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001388 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001389 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001390 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001391 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001392 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1393 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 +01001394
Damien George6f355fd2014-04-10 14:11:31 +01001395 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001396 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1397 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1398 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001399 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001400 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1401 } else {
1402 compile_node(comp, pns->nodes[0]);
1403 }
1404 EMIT(return_value);
1405}
1406
Damiend99b0522013-12-21 18:17:45 +00001407void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001408 compile_node(comp, pns->nodes[0]);
1409 EMIT(pop_top);
1410}
1411
Damiend99b0522013-12-21 18:17:45 +00001412void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1413 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001414 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001415 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001416 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001417 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001418 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001419 compile_node(comp, pns->nodes[0]);
1420 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001421 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001422 } else {
1423 // raise x
1424 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001425 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001426 }
1427}
1428
Damien George635543c2014-04-10 12:56:52 +01001429// q_base holds the base of the name
1430// eg a -> q_base=a
1431// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001432STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001433 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001434 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1435 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001436 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001437 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001438 pn = pns->nodes[0];
1439 is_as = true;
1440 }
Damien George635543c2014-04-10 12:56:52 +01001441 if (MP_PARSE_NODE_IS_NULL(pn)) {
1442 // empty name (eg, from . import x)
1443 *q_base = MP_QSTR_;
1444 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1445 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001446 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001447 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001448 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001449 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001450 }
Damien George635543c2014-04-10 12:56:52 +01001451 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001452 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1453 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1454 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001455 // a name of the form a.b.c
1456 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001457 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001458 }
Damiend99b0522013-12-21 18:17:45 +00001459 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001460 int len = n - 1;
1461 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001462 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001463 }
Damien George55baff42014-01-21 21:40:13 +00001464 byte *q_ptr;
1465 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001466 for (int i = 0; i < n; i++) {
1467 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001468 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001469 }
Damien George39dc1452014-10-03 19:52:22 +01001470 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001471 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001472 memcpy(str_dest, str_src, str_src_len);
1473 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001474 }
Damien George635543c2014-04-10 12:56:52 +01001475 qstr q_full = qstr_build_end(q_ptr);
1476 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001477 if (is_as) {
1478 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001479 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001480 }
1481 }
1482 } else {
Damien George635543c2014-04-10 12:56:52 +01001483 // shouldn't happen
1484 assert(0);
Damien429d7192013-10-04 19:53:11 +01001485 }
1486 } else {
Damien George635543c2014-04-10 12:56:52 +01001487 // shouldn't happen
1488 assert(0);
Damien429d7192013-10-04 19:53:11 +01001489 }
1490}
1491
Damien George6be0b0a2014-08-15 14:30:52 +01001492STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001493 EMIT_ARG(load_const_small_int, 0); // level 0 import
1494 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1495 qstr q_base;
1496 do_import_name(comp, pn, &q_base);
1497 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001498}
1499
Damiend99b0522013-12-21 18:17:45 +00001500void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001501 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1502}
1503
Damiend99b0522013-12-21 18:17:45 +00001504void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001505 mp_parse_node_t pn_import_source = pns->nodes[0];
1506
1507 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1508 uint import_level = 0;
1509 do {
1510 mp_parse_node_t pn_rel;
1511 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)) {
1512 // This covers relative imports with dots only like "from .. import"
1513 pn_rel = pn_import_source;
1514 pn_import_source = MP_PARSE_NODE_NULL;
1515 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1516 // This covers relative imports starting with dot(s) like "from .foo import"
1517 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1518 pn_rel = pns_2b->nodes[0];
1519 pn_import_source = pns_2b->nodes[1];
1520 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1521 } else {
1522 // Not a relative import
1523 break;
1524 }
1525
1526 // get the list of . and/or ...'s
1527 mp_parse_node_t *nodes;
1528 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1529
1530 // count the total number of .'s
1531 for (int i = 0; i < n; i++) {
1532 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1533 import_level++;
1534 } else {
1535 // should be an MP_TOKEN_ELLIPSIS
1536 import_level += 3;
1537 }
1538 }
1539 } while (0);
1540
Damiend99b0522013-12-21 18:17:45 +00001541 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001542 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001543
1544 // build the "fromlist" tuple
1545#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001546 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001547#else
Damien George708c0732014-04-27 19:23:46 +01001548 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001549 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001550#endif
1551
1552 // do the import
Damien George635543c2014-04-10 12:56:52 +01001553 qstr dummy_q;
1554 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001555 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001556
Damien429d7192013-10-04 19:53:11 +01001557 } else {
Damien George635543c2014-04-10 12:56:52 +01001558 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001559
1560 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001561 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001562 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001563#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001564 {
1565 vstr_t *vstr = vstr_new();
1566 vstr_printf(vstr, "(");
1567 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001568 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1569 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1570 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001571 if (i > 0) {
1572 vstr_printf(vstr, ", ");
1573 }
1574 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001575 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001576 const byte *str = qstr_data(id2, &len);
1577 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001578 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001579 }
Damien02f89412013-12-12 15:13:36 +00001580 if (n == 1) {
1581 vstr_printf(vstr, ",");
1582 }
1583 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001584 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001585 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001586 }
Damiendb4c3612013-12-10 17:27:24 +00001587#else
1588 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001589 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1590 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1591 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001592 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001593 }
Damien Georgeb9791222014-01-23 00:34:21 +00001594 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001595#endif
1596
1597 // do the import
Damien George635543c2014-04-10 12:56:52 +01001598 qstr dummy_q;
1599 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001600 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 Georgeb9791222014-01-23 00:34:21 +00001604 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001605 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001606 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001607 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001608 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001609 }
1610 }
1611 EMIT(pop_top);
1612 }
1613}
1614
Damiend99b0522013-12-21 18:17:45 +00001615void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001616 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001617 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1618 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001619 } else {
Damiend99b0522013-12-21 18:17:45 +00001620 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1621 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001622 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001623 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001624 }
Damien429d7192013-10-04 19:53:11 +01001625 }
1626 }
1627}
1628
Damiend99b0522013-12-21 18:17:45 +00001629void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001630 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001631 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1632 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001633 } else {
Damiend99b0522013-12-21 18:17:45 +00001634 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1635 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001636 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001637 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001638 }
Damien429d7192013-10-04 19:53:11 +01001639 }
1640 }
1641}
1642
Damiend99b0522013-12-21 18:17:45 +00001643void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001644 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001645 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001646 EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001647 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001648 // assertion message
1649 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001650 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001651 }
Damien Georgeb9791222014-01-23 00:34:21 +00001652 EMIT_ARG(raise_varargs, 1);
1653 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001654}
1655
Damiend99b0522013-12-21 18:17:45 +00001656void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001657 // TODO proper and/or short circuiting
1658
Damien George6f355fd2014-04-10 14:11:31 +01001659 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001660
Damien George6f355fd2014-04-10 14:11:31 +01001661 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001662 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1663
1664 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001665
1666 if (
1667#if !MICROPY_EMIT_CPYTHON
1668 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1669 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1670#endif
1671 // optimisation to not jump if last instruction was return
1672 !EMIT(last_emit_was_return_value)
1673 ) {
1674 // jump over elif/else blocks
1675 EMIT_ARG(jump, l_end);
1676 }
1677
Damien Georgeb9791222014-01-23 00:34:21 +00001678 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001679
Damiend99b0522013-12-21 18:17:45 +00001680 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001681 // compile elif blocks
1682
Damiend99b0522013-12-21 18:17:45 +00001683 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001684
Damiend99b0522013-12-21 18:17:45 +00001685 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001686 // multiple elif blocks
1687
Damiend99b0522013-12-21 18:17:45 +00001688 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001689 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001690 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001691 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001692 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1693
1694 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001695 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001696 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001697 }
Damien Georgeb9791222014-01-23 00:34:21 +00001698 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001699 }
1700
1701 } else {
1702 // a single elif block
1703
Damienb05d7072013-10-05 13:37:10 +01001704 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001705 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1706
1707 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001708 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001709 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001710 }
Damien Georgeb9791222014-01-23 00:34:21 +00001711 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001712 }
1713 }
1714
1715 // compile else block
1716 compile_node(comp, pns->nodes[3]); // can be null
1717
Damien Georgeb9791222014-01-23 00:34:21 +00001718 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001719}
1720
Damien Georgecbddb272014-02-01 20:08:18 +00001721#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001722 uint16_t old_break_label = comp->break_label; \
1723 uint16_t old_continue_label = comp->continue_label; \
1724 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001725 uint break_label = comp_next_label(comp); \
1726 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001727 comp->break_label = break_label; \
1728 comp->continue_label = continue_label; \
1729 comp->break_continue_except_level = comp->cur_except_level;
1730
1731#define END_BREAK_CONTINUE_BLOCK \
1732 comp->break_label = old_break_label; \
1733 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001734 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001735
Damiend99b0522013-12-21 18:17:45 +00001736void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001737 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001738
Damience89a212013-10-15 22:25:17 +01001739 // compared to CPython, we have an optimised version of while loops
1740#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001741 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001742 EMIT_ARG(setup_loop, break_label);
1743 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001744 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1745 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001746 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001747 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001748 }
Damien Georgeb9791222014-01-23 00:34:21 +00001749 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001750 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1751 // this is a small hack to agree with CPython
1752 if (!node_is_const_true(pns->nodes[0])) {
1753 EMIT(pop_block);
1754 }
Damience89a212013-10-15 22:25:17 +01001755#else
Damien George6f355fd2014-04-10 14:11:31 +01001756 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001757 EMIT_ARG(jump, continue_label);
1758 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001759 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001760 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001761 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1762#endif
1763
1764 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001765 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001766
1767 compile_node(comp, pns->nodes[2]); // else
1768
Damien Georgeb9791222014-01-23 00:34:21 +00001769 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001770}
1771
Damien George2ac4af62014-08-15 16:45:41 +01001772#if !MICROPY_EMIT_CPYTHON
Damienf72fd0e2013-11-06 20:20:49 +00001773// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001774// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1775// And, if the loop never runs, the loop variable should never be assigned
Damien George6be0b0a2014-08-15 14:30:52 +01001776STATIC 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 +00001777 START_BREAK_CONTINUE_BLOCK
Damien George5b5562c2014-05-31 17:59:11 +01001778 // note that we don't need to pop anything when breaking from an optimise for loop
Damienf72fd0e2013-11-06 20:20:49 +00001779
Damien George6f355fd2014-04-10 14:11:31 +01001780 uint top_label = comp_next_label(comp);
1781 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001782
Damien George3ff2d032014-03-31 18:02:22 +01001783 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001784 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001785 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001786
Damien Georgeb9791222014-01-23 00:34:21 +00001787 EMIT_ARG(jump, entry_label);
1788 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001789
Damien George3ff2d032014-03-31 18:02:22 +01001790 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001791 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001792
1793 // store next value to var
1794 c_assign(comp, pn_var, ASSIGN_STORE);
1795
Damienf3822fc2013-11-09 20:12:03 +00001796 // compile body
1797 compile_node(comp, pn_body);
1798
Damien Georgeb9791222014-01-23 00:34:21 +00001799 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001800
Damien George3ff2d032014-03-31 18:02:22 +01001801 // compile: var + step, duplicated on stack
1802 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001803 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001804 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001805 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001806
Damien Georgeb9791222014-01-23 00:34:21 +00001807 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001808
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001809 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001810 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001811 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1812 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001813 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001814 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001815 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001816 }
Damien Georgeb9791222014-01-23 00:34:21 +00001817 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001818
Damien George3ff2d032014-03-31 18:02:22 +01001819 // discard final value of var that failed the loop condition
1820 EMIT(pop_top);
1821
Damienf72fd0e2013-11-06 20:20:49 +00001822 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001823 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001824
1825 compile_node(comp, pn_else);
1826
Damien Georgeb9791222014-01-23 00:34:21 +00001827 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001828}
Damien George2ac4af62014-08-15 16:45:41 +01001829#endif
Damienf72fd0e2013-11-06 20:20:49 +00001830
Damiend99b0522013-12-21 18:17:45 +00001831void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001832#if !MICROPY_EMIT_CPYTHON
1833 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1834 // this is actually slower, but uses no heap memory
1835 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001836 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 +00001837 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001838 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1839 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1840 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1841 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001842 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1843 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001844 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001845 mp_parse_node_t pn_range_start;
1846 mp_parse_node_t pn_range_end;
1847 mp_parse_node_t pn_range_step;
1848 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001849 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001850 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001851 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001852 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001853 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001854 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001855 } else if (n_args == 2) {
1856 pn_range_start = args[0];
1857 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001858 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001859 } else {
1860 pn_range_start = args[0];
1861 pn_range_end = args[1];
1862 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001863 // We need to know sign of step. This is possible only if it's constant
1864 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1865 optimize = false;
1866 }
Damienf72fd0e2013-11-06 20:20:49 +00001867 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001868 }
1869 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001870 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1871 return;
1872 }
1873 }
1874 }
1875#endif
1876
Damien Georgecbddb272014-02-01 20:08:18 +00001877 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001878 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001879
Damien George6f355fd2014-04-10 14:11:31 +01001880 uint pop_label = comp_next_label(comp);
1881 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001882
Damience89a212013-10-15 22:25:17 +01001883 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1884#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001885 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001886#endif
1887
Damien429d7192013-10-04 19:53:11 +01001888 compile_node(comp, pns->nodes[1]); // iterator
1889 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001890 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001891 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001892 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1893 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001894 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001895 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001896 }
Damien Georgeb9791222014-01-23 00:34:21 +00001897 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001898 EMIT(for_iter_end);
1899
1900 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001901 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001902
Damience89a212013-10-15 22:25:17 +01001903#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001904 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001905#endif
Damien429d7192013-10-04 19:53:11 +01001906
1907 compile_node(comp, pns->nodes[3]); // else (not tested)
1908
Damien Georgeb9791222014-01-23 00:34:21 +00001909 EMIT_ARG(label_assign, break_label);
1910 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001911}
1912
Damien George6be0b0a2014-08-15 14:30:52 +01001913STATIC 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 +01001914 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001915 uint l1 = comp_next_label(comp);
1916 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001917
Damien Georgeb9791222014-01-23 00:34:21 +00001918 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001919 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001920
Damien429d7192013-10-04 19:53:11 +01001921 compile_node(comp, pn_body); // body
1922 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001923 EMIT_ARG(jump, success_label); // jump over exception handler
1924
1925 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001926 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001927
Damien George6f355fd2014-04-10 14:11:31 +01001928 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001929
1930 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001931 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1932 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001933
1934 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001935 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001936
Damiend99b0522013-12-21 18:17:45 +00001937 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001938 // this is a catch all exception handler
1939 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001940 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001941 return;
1942 }
1943 } else {
1944 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001945 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1946 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1947 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1948 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001949 // handler binds the exception to a local
1950 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001951 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001952 }
1953 }
1954 EMIT(dup_top);
1955 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001956 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001957 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001958 }
1959
1960 EMIT(pop_top);
1961
1962 if (qstr_exception_local == 0) {
1963 EMIT(pop_top);
1964 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001965 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001966 }
1967
1968 EMIT(pop_top);
1969
Damien George6f355fd2014-04-10 14:11:31 +01001970 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001971 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001972 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001973 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001974 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001975 }
1976 compile_node(comp, pns_except->nodes[1]);
1977 if (qstr_exception_local != 0) {
1978 EMIT(pop_block);
1979 }
1980 EMIT(pop_except);
1981 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1983 EMIT_ARG(label_assign, l3);
1984 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1985 EMIT_ARG(store_id, qstr_exception_local);
1986 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001987
Damien George8dcc0c72014-03-27 10:55:21 +00001988 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001989 EMIT(end_finally);
1990 }
Damien Georgeb9791222014-01-23 00:34:21 +00001991 EMIT_ARG(jump, l2);
1992 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001993 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001994 }
1995
Damien George8dcc0c72014-03-27 10:55:21 +00001996 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001997 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001998 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001999
Damien Georgeb9791222014-01-23 00:34:21 +00002000 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002001 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002002 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002003}
2004
Damien George6be0b0a2014-08-15 14:30:52 +01002005STATIC 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 +01002006 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002007
Damien Georgeb9791222014-01-23 00:34:21 +00002008 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002009 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002010
Damien429d7192013-10-04 19:53:11 +01002011 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002012 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002013 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002014 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002015 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002016 } else {
2017 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2018 }
2019 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002020 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2021 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002022 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002023
Damien George8dcc0c72014-03-27 10:55:21 +00002024 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002025 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002026}
2027
Damiend99b0522013-12-21 18:17:45 +00002028void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2029 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2030 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2031 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002032 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002033 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2034 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002035 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002036 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002037 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002038 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002039 // no finally
2040 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2041 } else {
2042 // have finally
Damiend99b0522013-12-21 18:17:45 +00002043 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 +01002044 }
2045 } else {
2046 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002047 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002048 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002049 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002050 }
2051 } else {
2052 // shouldn't happen
2053 assert(0);
2054 }
2055}
2056
Damien George6be0b0a2014-08-15 14:30:52 +01002057STATIC 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 +01002058 if (n == 0) {
2059 // no more pre-bits, compile the body of the with
2060 compile_node(comp, body);
2061 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002062 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002063 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002064 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002065 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002066 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002067 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002068 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2069 } else {
2070 // this pre-bit is just an expression
2071 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002072 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002073 EMIT(pop_top);
2074 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002075 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002076 // compile additional pre-bits and the body
2077 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2078 // finish this with block
2079 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002080 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2081 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002082 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002083 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002084 EMIT(end_finally);
2085 }
2086}
2087
Damiend99b0522013-12-21 18:17:45 +00002088void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002089 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002090 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002091 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2092 assert(n > 0);
2093
2094 // compile in a nested fashion
2095 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2096}
2097
Damiend99b0522013-12-21 18:17:45 +00002098void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2099 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002100 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2101 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002102 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002103 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002104 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002105 EMIT(pop_top);
2106
Damien429d7192013-10-04 19:53:11 +01002107 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002108 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002109 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2110 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002111 // do nothing with a lonely constant
2112 } else {
2113 compile_node(comp, pns->nodes[0]); // just an expression
2114 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2115 }
Damien429d7192013-10-04 19:53:11 +01002116 }
2117 } else {
Damiend99b0522013-12-21 18:17:45 +00002118 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2119 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002120 if (kind == PN_expr_stmt_augassign) {
2121 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2122 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002123 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002124 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002125 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002126 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2127 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2128 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2129 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2130 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2131 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2132 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2133 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2134 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2135 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2136 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2137 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2138 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002139 }
Damien George7e5fb242014-02-01 22:18:47 +00002140 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002141 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2142 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002143 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2144 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002145 // following CPython, we store left-most first
2146 if (rhs > 0) {
2147 EMIT(dup_top);
2148 }
2149 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2150 for (int i = 0; i < rhs; i++) {
2151 if (i + 1 < rhs) {
2152 EMIT(dup_top);
2153 }
Damiend99b0522013-12-21 18:17:45 +00002154 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002155 }
2156 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002157 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002158 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2159 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2160 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002161 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002162 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2163 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002164 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2165 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2166 // can't optimise when it's a star expression on the lhs
2167 goto no_optimisation;
2168 }
Damien429d7192013-10-04 19:53:11 +01002169 compile_node(comp, pns10->nodes[0]); // rhs
2170 compile_node(comp, pns10->nodes[1]); // rhs
2171 EMIT(rot_two);
2172 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2173 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002174 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2175 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2176 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2177 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002178 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002179 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2180 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002181 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2182 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2183 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2184 // can't optimise when it's a star expression on the lhs
2185 goto no_optimisation;
2186 }
Damien429d7192013-10-04 19:53:11 +01002187 compile_node(comp, pns10->nodes[0]); // rhs
2188 compile_node(comp, pns10->nodes[1]); // rhs
2189 compile_node(comp, pns10->nodes[2]); // rhs
2190 EMIT(rot_three);
2191 EMIT(rot_two);
2192 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2193 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2194 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2195 } else {
Damien George495d7812014-04-08 17:51:47 +01002196 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002197 compile_node(comp, pns1->nodes[0]); // rhs
2198 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2199 }
2200 } else {
2201 // shouldn't happen
2202 assert(0);
2203 }
2204 }
2205}
2206
Damien George6be0b0a2014-08-15 14:30:52 +01002207STATIC 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 +00002208 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002209 compile_node(comp, pns->nodes[0]);
2210 for (int i = 1; i < num_nodes; i += 1) {
2211 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002212 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002213 }
2214}
2215
Damiend99b0522013-12-21 18:17:45 +00002216void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2217 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2218 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002219
Damien George6f355fd2014-04-10 14:11:31 +01002220 uint l_fail = comp_next_label(comp);
2221 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002222 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2223 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002224 EMIT_ARG(jump, l_end);
2225 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002226 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002227 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002228 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002229}
2230
Damiend99b0522013-12-21 18:17:45 +00002231void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002232 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002233 //mp_parse_node_t pn_params = pns->nodes[0];
2234 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002235
Damien George36db6bc2014-05-07 17:24:22 +01002236 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002237 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002238 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 +01002239 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002240 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002241 }
2242
2243 // get the scope for this lambda
2244 scope_t *this_scope = (scope_t*)pns->nodes[2];
2245
2246 // make the lambda
2247 close_over_variables_etc(comp, this_scope, 0, 0);
2248}
2249
Damiend99b0522013-12-21 18:17:45 +00002250void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002251 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002252 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002253 for (int i = 0; i < n; i += 1) {
2254 compile_node(comp, pns->nodes[i]);
2255 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002256 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002257 }
2258 }
Damien Georgeb9791222014-01-23 00:34:21 +00002259 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002260}
2261
Damiend99b0522013-12-21 18:17:45 +00002262void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002263 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002264 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002265 for (int i = 0; i < n; i += 1) {
2266 compile_node(comp, pns->nodes[i]);
2267 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002268 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002269 }
2270 }
Damien Georgeb9791222014-01-23 00:34:21 +00002271 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002272}
2273
Damiend99b0522013-12-21 18:17:45 +00002274void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002275 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002276 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002277}
2278
Damiend99b0522013-12-21 18:17:45 +00002279void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002280 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002281 compile_node(comp, pns->nodes[0]);
2282 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002283 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002284 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002285 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002286 }
2287 for (int i = 1; i + 1 < num_nodes; i += 2) {
2288 compile_node(comp, pns->nodes[i + 1]);
2289 if (i + 2 < num_nodes) {
2290 EMIT(dup_top);
2291 EMIT(rot_three);
2292 }
Damien George7e5fb242014-02-01 22:18:47 +00002293 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002294 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002295 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002296 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2297 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2298 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2299 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2300 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2301 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2302 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2303 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002304 }
2305 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002306 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2307 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2308 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002309 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002310 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002311 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002312 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002313 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002314 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002315 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002316 }
2317 } else {
2318 // shouldn't happen
2319 assert(0);
2320 }
2321 } else {
2322 // shouldn't happen
2323 assert(0);
2324 }
2325 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002326 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002327 }
2328 }
2329 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002330 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002331 EMIT_ARG(jump, l_end);
2332 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002333 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002334 EMIT(rot_two);
2335 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002336 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002337 }
2338}
2339
Damiend99b0522013-12-21 18:17:45 +00002340void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002341 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002342}
2343
Damiend99b0522013-12-21 18:17:45 +00002344void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002345 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002346}
2347
Damiend99b0522013-12-21 18:17:45 +00002348void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002349 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002350}
2351
Damiend99b0522013-12-21 18:17:45 +00002352void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002353 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002354}
2355
Damiend99b0522013-12-21 18:17:45 +00002356void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2357 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002358 compile_node(comp, pns->nodes[0]);
2359 for (int i = 1; i + 1 < num_nodes; i += 2) {
2360 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002361 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002362 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002363 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002364 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002365 } else {
2366 // shouldn't happen
2367 assert(0);
2368 }
2369 }
2370}
2371
Damiend99b0522013-12-21 18:17:45 +00002372void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2373 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002374 compile_node(comp, pns->nodes[0]);
2375 for (int i = 1; i + 1 < num_nodes; i += 2) {
2376 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002377 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002378 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002379 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002380 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002381 } else {
2382 // shouldn't happen
2383 assert(0);
2384 }
2385 }
2386}
2387
Damiend99b0522013-12-21 18:17:45 +00002388void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2389 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002390 compile_node(comp, pns->nodes[0]);
2391 for (int i = 1; i + 1 < num_nodes; i += 2) {
2392 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002393 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002394 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002395 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002396 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002397 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002398 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002399 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002400 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002401 } else {
2402 // shouldn't happen
2403 assert(0);
2404 }
2405 }
2406}
2407
Damiend99b0522013-12-21 18:17:45 +00002408void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002409 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002410 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002411 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002412 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002413 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002414 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002415 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002416 } else {
2417 // shouldn't happen
2418 assert(0);
2419 }
2420}
2421
Damien George35e2a4e2014-02-05 00:51:47 +00002422void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2423 // this is to handle special super() call
2424 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2425
2426 compile_generic_all_nodes(comp, pns);
2427}
2428
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002429STATIC 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 +01002430 // function to call is on top of stack
2431
Damien George35e2a4e2014-02-05 00:51:47 +00002432#if !MICROPY_EMIT_CPYTHON
2433 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002434 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George35e2a4e2014-02-05 00:51:47 +00002435 EMIT_ARG(load_id, MP_QSTR___class__);
2436 // get first argument to function
2437 bool found = false;
2438 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002439 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2440 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George35e2a4e2014-02-05 00:51:47 +00002441 found = true;
2442 break;
2443 }
2444 }
2445 if (!found) {
2446 printf("TypeError: super() call cannot find self\n");
2447 return;
2448 }
Damien George922ddd62014-04-09 12:43:17 +01002449 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002450 return;
2451 }
2452#endif
2453
Damien George2c0842b2014-04-27 16:46:51 +01002454 // get the list of arguments
2455 mp_parse_node_t *args;
2456 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002457
Damien George2c0842b2014-04-27 16:46:51 +01002458 // compile the arguments
2459 // Rather than calling compile_node on the list, we go through the list of args
2460 // explicitly here so that we can count the number of arguments and give sensible
2461 // error messages.
2462 int n_positional = n_positional_extra;
2463 uint n_keyword = 0;
2464 uint star_flags = 0;
2465 for (int i = 0; i < n_args; i++) {
2466 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2467 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2468 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2469 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2470 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2471 return;
2472 }
2473 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2474 compile_node(comp, pns_arg->nodes[0]);
2475 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2476 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2477 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2478 return;
2479 }
2480 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2481 compile_node(comp, pns_arg->nodes[0]);
2482 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2483 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2484 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2485 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2486 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2487 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2488 return;
2489 }
Damien George968bf342014-04-27 19:12:05 +01002490 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002491 compile_node(comp, pns2->nodes[0]);
2492 n_keyword += 1;
2493 } else {
2494 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2495 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2496 n_positional++;
2497 }
2498 } else {
2499 goto normal_argument;
2500 }
2501 } else {
2502 normal_argument:
2503 if (n_keyword > 0) {
2504 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2505 return;
2506 }
2507 compile_node(comp, args[i]);
2508 n_positional++;
2509 }
Damien429d7192013-10-04 19:53:11 +01002510 }
2511
Damien George2c0842b2014-04-27 16:46:51 +01002512 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002513 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002514 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002515 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002516 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002517 }
Damien429d7192013-10-04 19:53:11 +01002518}
2519
Damiend99b0522013-12-21 18:17:45 +00002520void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2521 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002522 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002523 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 +01002524 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002525 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2526 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002527 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002528 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002529 i += 1;
2530 } else {
2531 compile_node(comp, pns->nodes[i]);
2532 }
Damien George35e2a4e2014-02-05 00:51:47 +00002533 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002534 }
2535}
2536
Damiend99b0522013-12-21 18:17:45 +00002537void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002538 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002539 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002540}
2541
Damiend99b0522013-12-21 18:17:45 +00002542void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002543 // a list of strings
Damien63321742013-12-10 17:41:49 +00002544
2545 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002546 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002547 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002548 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002549 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002550 int pn_kind;
2551 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2552 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2553 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2554 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2555 } else {
2556 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2557 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2558 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2559 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002560 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002561 }
Damien63321742013-12-10 17:41:49 +00002562 if (i == 0) {
2563 string_kind = pn_kind;
2564 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002565 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002566 return;
2567 }
Damien429d7192013-10-04 19:53:11 +01002568 }
Damien63321742013-12-10 17:41:49 +00002569
Damien63321742013-12-10 17:41:49 +00002570 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002571 byte *q_ptr;
2572 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002573 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002574 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002575 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002576 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2577 memcpy(s_dest, s, s_len);
2578 s_dest += s_len;
2579 } else {
2580 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002581 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2582 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002583 }
Damien63321742013-12-10 17:41:49 +00002584 }
Damien George55baff42014-01-21 21:40:13 +00002585 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002586
Damien Georgeb9791222014-01-23 00:34:21 +00002587 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002588}
2589
2590// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002591STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002592 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2593 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2594 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002595
Damien George36db6bc2014-05-07 17:24:22 +01002596 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002597 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002598 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 +01002599 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002600 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002601 }
2602
2603 // get the scope for this comprehension
2604 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2605
2606 // compile the comprehension
2607 close_over_variables_etc(comp, this_scope, 0, 0);
2608
2609 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2610 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002611 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002612}
2613
Damiend99b0522013-12-21 18:17:45 +00002614void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2615 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002616 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002617 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2618 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2619 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2620 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2621 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2622 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2623 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002624 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002625 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002626 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002627 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002628 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002629 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002630 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002631 // generator expression
2632 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2633 } else {
2634 // tuple with 2 items
2635 goto tuple_with_2_items;
2636 }
2637 } else {
2638 // tuple with 2 items
2639 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002640 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002641 }
2642 } else {
2643 // parenthesis around a single item, is just that item
2644 compile_node(comp, pns->nodes[0]);
2645 }
2646}
2647
Damiend99b0522013-12-21 18:17:45 +00002648void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2649 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002650 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002651 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002652 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2653 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2654 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2655 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2656 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002657 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002658 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002659 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002660 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002661 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002662 // list of many items
2663 compile_node(comp, pns2->nodes[0]);
2664 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002665 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002666 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002667 // list comprehension
2668 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2669 } else {
2670 // list with 2 items
2671 goto list_with_2_items;
2672 }
2673 } else {
2674 // list with 2 items
2675 list_with_2_items:
2676 compile_node(comp, pns2->nodes[0]);
2677 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002678 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002679 }
2680 } else {
2681 // list with 1 item
2682 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002683 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002684 }
2685}
2686
Damiend99b0522013-12-21 18:17:45 +00002687void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2688 mp_parse_node_t pn = pns->nodes[0];
2689 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002690 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002691 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002692 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2693 pns = (mp_parse_node_struct_t*)pn;
2694 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002695 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002696 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002697 compile_node(comp, pn);
2698 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002699 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2700 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2701 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2702 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002703 // dict/set with multiple elements
2704
2705 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002706 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002707 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2708
2709 // first element sets whether it's a dict or set
2710 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002711 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002712 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002713 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002714 compile_node(comp, pns->nodes[0]);
2715 EMIT(store_map);
2716 is_dict = true;
2717 } else {
2718 // a set
2719 compile_node(comp, pns->nodes[0]); // 1st value of set
2720 is_dict = false;
2721 }
2722
2723 // process rest of elements
2724 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002725 mp_parse_node_t pn = nodes[i];
2726 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002727 compile_node(comp, pn);
2728 if (is_dict) {
2729 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002730 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002731 return;
2732 }
2733 EMIT(store_map);
2734 } else {
2735 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002736 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002737 return;
2738 }
2739 }
2740 }
2741
2742 // if it's a set, build it
2743 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002744 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002745 }
Damiend99b0522013-12-21 18:17:45 +00002746 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002747 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002748 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002749 // a dictionary comprehension
2750 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2751 } else {
2752 // a set comprehension
2753 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2754 }
2755 } else {
2756 // shouldn't happen
2757 assert(0);
2758 }
2759 } else {
2760 // set with one element
2761 goto set_with_one_element;
2762 }
2763 } else {
2764 // set with one element
2765 set_with_one_element:
2766 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002767 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002768 }
2769}
2770
Damiend99b0522013-12-21 18:17:45 +00002771void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002772 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002773}
2774
Damiend99b0522013-12-21 18:17:45 +00002775void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002776 // object who's index we want is on top of stack
2777 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002778 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002779}
2780
Damiend99b0522013-12-21 18:17:45 +00002781void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002782 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002783 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002784}
2785
Damiend99b0522013-12-21 18:17:45 +00002786void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2787 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2788 mp_parse_node_t pn = pns->nodes[0];
2789 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002790 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002791 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2792 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002793 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2794 pns = (mp_parse_node_struct_t*)pn;
2795 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002796 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002797 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002798 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002799 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002800 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002801 } else {
2802 // [?::x]
2803 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002804 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002805 }
Damiend99b0522013-12-21 18:17:45 +00002806 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002807 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002808 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2809 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2810 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2811 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002812 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002813 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002814 } else {
2815 // [?:x:x]
2816 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002817 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002818 }
2819 } else {
2820 // [?:x]
2821 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002822 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002823 }
2824 } else {
2825 // [?:x]
2826 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002827 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002828 }
2829}
2830
Damiend99b0522013-12-21 18:17:45 +00002831void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002832 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002833 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2834 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002835}
2836
Damiend99b0522013-12-21 18:17:45 +00002837void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002838 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002839 compile_subscript_3_helper(comp, pns);
2840}
2841
Damiend99b0522013-12-21 18:17:45 +00002842void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002843 // if this is called then we are compiling a dict key:value pair
2844 compile_node(comp, pns->nodes[1]); // value
2845 compile_node(comp, pns->nodes[0]); // key
2846}
2847
Damiend99b0522013-12-21 18:17:45 +00002848void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002849 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002850 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002851 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002852}
2853
Damiend99b0522013-12-21 18:17:45 +00002854void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002855 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002856 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002857 return;
2858 }
Damiend99b0522013-12-21 18:17:45 +00002859 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002861 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002862 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2863 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002864 compile_node(comp, pns->nodes[0]);
2865 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002866 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002867 EMIT(yield_from);
2868 } else {
2869 compile_node(comp, pns->nodes[0]);
2870 EMIT(yield_value);
2871 }
2872}
2873
Damiend99b0522013-12-21 18:17:45 +00002874typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002875STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002876 NULL,
2877#define nc NULL
2878#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002879#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002880#include "grammar.h"
2881#undef nc
2882#undef c
2883#undef DEF_RULE
2884};
2885
Damien George6be0b0a2014-08-15 14:30:52 +01002886STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002887 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002888 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002889 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002890 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002891 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002892 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002893 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002894 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002895 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002896 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2897 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2898 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2899 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002900 case MP_PARSE_NODE_TOKEN:
2901 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002902 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002903 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002904 // do nothing
2905 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002906 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002907 }
2908 break;
Damien429d7192013-10-04 19:53:11 +01002909 default: assert(0);
2910 }
2911 } else {
Damiend99b0522013-12-21 18:17:45 +00002912 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002913 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002914 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002915 EMIT_ARG(load_const_str, qstr_from_strn((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]), false);
Damien429d7192013-10-04 19:53:11 +01002916 } else {
Damien George5042bce2014-05-25 22:06:06 +01002917 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2918 if (f == NULL) {
2919 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2920#if MICROPY_DEBUG_PRINTERS
2921 mp_parse_node_print(pn, 0);
2922#endif
2923 compile_syntax_error(comp, pn, "internal compiler error");
2924 } else {
2925 f(comp, pns);
2926 }
Damien429d7192013-10-04 19:53:11 +01002927 }
2928 }
2929}
2930
Damien George2ac4af62014-08-15 16:45:41 +01002931STATIC 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 +01002932 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002933 qstr param_name = MP_QSTR_NULL;
2934 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002935 if (MP_PARSE_NODE_IS_ID(pn)) {
2936 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002937 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002938 // comes after a star, so counts as a keyword-only parameter
2939 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002940 } else {
Damien George2827d622014-04-27 15:50:52 +01002941 // comes before a star, so counts as a positional parameter
2942 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002943 }
Damienb14de212013-10-06 00:28:28 +01002944 } else {
Damiend99b0522013-12-21 18:17:45 +00002945 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2946 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2947 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2948 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002949 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002950 // comes after a star, so counts as a keyword-only parameter
2951 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002952 } else {
Damien George2827d622014-04-27 15:50:52 +01002953 // comes before a star, so counts as a positional parameter
2954 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002955 }
Damiend99b0522013-12-21 18:17:45 +00002956 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002957 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002958 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002959 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002960 // bare star
2961 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002962 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002963 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002964 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002965 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002966 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01002967 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002968 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002969 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002970 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2971 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002972 } else {
2973 // shouldn't happen
2974 assert(0);
2975 }
Damiend99b0522013-12-21 18:17:45 +00002976 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2977 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002978 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002979 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002980 } else {
Damienb14de212013-10-06 00:28:28 +01002981 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002982 assert(0);
2983 }
Damien429d7192013-10-04 19:53:11 +01002984 }
2985
Damien George2827d622014-04-27 15:50:52 +01002986 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002987 bool added;
2988 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2989 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002990 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002991 return;
2992 }
Damien429d7192013-10-04 19:53:11 +01002993 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002994 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002995 }
2996}
2997
Damien George2827d622014-04-27 15:50:52 +01002998STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002999 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003000}
3001
Damien George2827d622014-04-27 15:50:52 +01003002STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003003 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3004}
3005
3006STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3007 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3008 // no annotation
3009 return;
3010 }
3011
3012 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3013 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3014 // named parameter with possible annotation
3015 // fallthrough
3016 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3017 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3018 // named star with possible annotation
3019 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3020 // fallthrough
3021 } else {
3022 // no annotation
3023 return;
3024 }
3025 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3026 // double star with possible annotation
3027 // fallthrough
3028 } else {
3029 // no annotation
3030 return;
3031 }
3032
3033 mp_parse_node_t pn_annotation = pns->nodes[1];
3034
3035 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3036 #if MICROPY_EMIT_NATIVE
3037 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3038 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3039 assert(id_info != NULL);
3040
3041 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3042 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3043 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3044 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3045 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003046 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003047 }
3048 }
3049 #endif // MICROPY_EMIT_NATIVE
3050 }
Damien429d7192013-10-04 19:53:11 +01003051}
3052
Damien George6be0b0a2014-08-15 14:30:52 +01003053STATIC 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 +01003054 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003055 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003056 // no more nested if/for; compile inner expression
3057 compile_node(comp, pn_inner_expr);
3058 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003059 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003060 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003061 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003062 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003063 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003064 } else {
3065 EMIT(yield_value);
3066 EMIT(pop_top);
3067 }
Damiend99b0522013-12-21 18:17:45 +00003068 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003069 // if condition
Damiend99b0522013-12-21 18:17:45 +00003070 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003071 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3072 pn_iter = pns_comp_if->nodes[1];
3073 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003074 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003075 // for loop
Damiend99b0522013-12-21 18:17:45 +00003076 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003077 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003078 uint l_end2 = comp_next_label(comp);
3079 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003080 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003081 EMIT_ARG(label_assign, l_top2);
3082 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003083 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3084 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 +00003085 EMIT_ARG(jump, l_top2);
3086 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003087 EMIT(for_iter_end);
3088 } else {
3089 // shouldn't happen
3090 assert(0);
3091 }
3092}
3093
Damien George1463c1f2014-04-25 23:52:57 +01003094STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3095#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003096 // see http://www.python.org/dev/peps/pep-0257/
3097
3098 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003099 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003100 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003101 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003102 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003103 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3104 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003105 for (int i = 0; i < num_nodes; i++) {
3106 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003107 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 +00003108 // not a newline, so this is the first statement; finish search
3109 break;
3110 }
3111 }
3112 // 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 +00003113 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003114 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003115 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003116 } else {
3117 return;
3118 }
3119
3120 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003121 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3122 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003123 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3124 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3125 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3126 // compile the doc string
3127 compile_node(comp, pns->nodes[0]);
3128 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003129 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003130 }
3131 }
Damien George1463c1f2014-04-25 23:52:57 +01003132#endif
Damien429d7192013-10-04 19:53:11 +01003133}
3134
Damien George1463c1f2014-04-25 23:52:57 +01003135STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003136 comp->pass = pass;
3137 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003138 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003139 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003140
Damien George36db6bc2014-05-07 17:24:22 +01003141 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003142 // reset maximum stack sizes in scope
3143 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003144 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003145 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003146 }
3147
Damien5ac1b2e2013-10-18 19:58:12 +01003148#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003149 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003150 scope_print_info(scope);
3151 }
Damien5ac1b2e2013-10-18 19:58:12 +01003152#endif
Damien429d7192013-10-04 19:53:11 +01003153
3154 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003155 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3156 assert(scope->kind == SCOPE_MODULE);
3157 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3158 compile_node(comp, pns->nodes[0]); // compile the expression
3159 EMIT(return_value);
3160 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003161 if (!comp->is_repl) {
3162 check_for_doc_string(comp, scope->pn);
3163 }
Damien429d7192013-10-04 19:53:11 +01003164 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003165 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003166 EMIT(return_value);
3167 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003168 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3169 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3170 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003171
3172 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003173 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003174 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003175 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003176 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003177 } else {
3178 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003179
Damien George2ac4af62014-08-15 16:45:41 +01003180 // argument annotations
3181 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3182
3183 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003184 mp_parse_node_t pn_annotation = pns->nodes[2];
3185 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3186 #if MICROPY_EMIT_NATIVE
3187 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3188 // nodes[2] can be null or a test-expr
3189 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3190 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3191 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3192 } else {
3193 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3194 }
Damien George2ac4af62014-08-15 16:45:41 +01003195 }
Damien Georgea5190a72014-08-15 22:39:08 +01003196 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003197 }
Damien George2ac4af62014-08-15 16:45:41 +01003198 }
Damien429d7192013-10-04 19:53:11 +01003199
3200 compile_node(comp, pns->nodes[3]); // 3 is function body
3201 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003202 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003203 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003204 EMIT(return_value);
3205 }
3206 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003207 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3208 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3209 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003210
3211 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003212 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003213 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003214 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003215 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3216 }
3217
3218 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003219
3220 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3221 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3222 EMIT(pop_top);
3223 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3224 }
Damien429d7192013-10-04 19:53:11 +01003225 EMIT(return_value);
3226 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3227 // a bit of a hack at the moment
3228
Damiend99b0522013-12-21 18:17:45 +00003229 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3230 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3231 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3232 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3233 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003234
Damien George708c0732014-04-27 19:23:46 +01003235 // We need a unique name for the comprehension argument (the iterator).
3236 // CPython uses .0, but we should be able to use anything that won't
3237 // clash with a user defined variable. Best to use an existing qstr,
3238 // so we use the blank qstr.
3239#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003240 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003241#else
3242 qstr qstr_arg = MP_QSTR_;
3243#endif
Damien George36db6bc2014-05-07 17:24:22 +01003244 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003245 bool added;
3246 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3247 assert(added);
3248 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003249 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003250 }
3251
3252 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003253 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003254 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003255 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003256 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003257 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003258 }
3259
Damien George6f355fd2014-04-10 14:11:31 +01003260 uint l_end = comp_next_label(comp);
3261 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003262 EMIT_ARG(load_id, qstr_arg);
3263 EMIT_ARG(label_assign, l_top);
3264 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003265 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3266 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003267 EMIT_ARG(jump, l_top);
3268 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003269 EMIT(for_iter_end);
3270
3271 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003272 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003273 }
3274 EMIT(return_value);
3275 } else {
3276 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003277 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3278 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3279 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003280
Damien George36db6bc2014-05-07 17:24:22 +01003281 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003282 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003283 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003284 assert(added);
3285 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003286 }
3287
Damien Georgeb9791222014-01-23 00:34:21 +00003288 EMIT_ARG(load_id, MP_QSTR___name__);
3289 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003290 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), false); // 0 is class name
Damien Georgeb9791222014-01-23 00:34:21 +00003291 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003292
3293 check_for_doc_string(comp, pns->nodes[2]);
3294 compile_node(comp, pns->nodes[2]); // 2 is class body
3295
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003296 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003297 assert(id != NULL);
3298 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003299 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003300 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003301#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003302 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003303#else
Damien George2bf7c092014-04-09 15:26:46 +01003304 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003305#endif
Damien429d7192013-10-04 19:53:11 +01003306 }
3307 EMIT(return_value);
3308 }
3309
Damien415eb6f2013-10-05 12:19:06 +01003310 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003311
3312 // make sure we match all the exception levels
3313 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003314}
3315
Damien George094d4502014-04-02 17:31:27 +01003316#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003317// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003318STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003319 comp->pass = pass;
3320 comp->scope_cur = scope;
3321 comp->next_label = 1;
3322
3323 if (scope->kind != SCOPE_FUNCTION) {
3324 printf("Error: inline assembler must be a function\n");
3325 return;
3326 }
3327
Damien George36db6bc2014-05-07 17:24:22 +01003328 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003329 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003330 }
3331
Damien826005c2013-10-05 23:17:28 +01003332 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003333 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3334 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3335 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003336
Damiend99b0522013-12-21 18:17:45 +00003337 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003338
Damiena2f2f7d2013-10-06 00:14:13 +01003339 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003340 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003341 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003342 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003343 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003344 }
3345
Damiend99b0522013-12-21 18:17:45 +00003346 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003347
Damiend99b0522013-12-21 18:17:45 +00003348 mp_parse_node_t pn_body = pns->nodes[3]; // body
3349 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003350 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3351
Damien Georgecbd2f742014-01-19 11:48:48 +00003352 /*
Damien George36db6bc2014-05-07 17:24:22 +01003353 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003354 //printf("----\n");
3355 scope_print_info(scope);
3356 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003357 */
Damien826005c2013-10-05 23:17:28 +01003358
3359 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003360 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3361 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003362 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3363 // no instructions
3364 continue;
3365 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3366 // an instruction; fall through
3367 } else {
3368 // not an instruction; error
3369 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3370 return;
3371 }
Damiend99b0522013-12-21 18:17:45 +00003372 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3373 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3374 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3375 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3376 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3377 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3378 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3379 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3380 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3381 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003382 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3383
3384 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003385 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003386 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003387 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003388 return;
3389 }
Damien George6f355fd2014-04-10 14:11:31 +01003390 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003391 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003392 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003393 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003394 } else if (op == MP_QSTR_align) {
3395 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3396 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3397 return;
3398 }
Damien George36db6bc2014-05-07 17:24:22 +01003399 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003400 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3401 }
3402 } else if (op == MP_QSTR_data) {
3403 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3404 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3405 return;
3406 }
Damien George36db6bc2014-05-07 17:24:22 +01003407 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003408 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003409 for (uint i = 1; i < n_args; i++) {
3410 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3411 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3412 return;
3413 }
3414 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3415 }
3416 }
Damien826005c2013-10-05 23:17:28 +01003417 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003418 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003419 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003420 }
3421 }
3422 }
3423
Damien George36db6bc2014-05-07 17:24:22 +01003424 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003425 bool success = EMIT_INLINE_ASM(end_pass);
3426 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003427 // TODO get proper exception from inline assembler
3428 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003429 }
Damienb05d7072013-10-05 13:37:10 +01003430 }
Damien429d7192013-10-04 19:53:11 +01003431}
Damien George094d4502014-04-02 17:31:27 +01003432#endif
Damien429d7192013-10-04 19:53:11 +01003433
Damien George1463c1f2014-04-25 23:52:57 +01003434STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003435#if !MICROPY_EMIT_CPYTHON
3436 // in Micro Python we put the *x parameter after all other parameters (except **y)
3437 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3438 id_info_t *id_param = NULL;
3439 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3440 id_info_t *id = &scope->id_info[i];
3441 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3442 if (id_param != NULL) {
3443 // swap star param with last param
3444 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3445 }
3446 break;
3447 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3448 id_param = id;
3449 }
3450 }
3451 }
3452#endif
3453
Damien429d7192013-10-04 19:53:11 +01003454 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003455 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003456 scope->num_locals = 0;
3457 for (int i = 0; i < scope->id_info_len; i++) {
3458 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003459 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003460 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3461 continue;
3462 }
3463 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3464 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3465 }
Damien George2827d622014-04-27 15:50:52 +01003466 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003467 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003468 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003469 }
3470 }
3471
3472 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003473#if MICROPY_EMIT_CPYTHON
3474 int num_cell = 0;
3475#endif
Damien9ecbcff2013-12-11 00:41:43 +00003476 for (int i = 0; i < scope->id_info_len; i++) {
3477 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003478#if MICROPY_EMIT_CPYTHON
3479 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003480 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003481 id->local_num = num_cell;
3482 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003483 }
Damien George6baf76e2013-12-30 22:32:17 +00003484#else
3485 // in Micro Python the cells come right after the fast locals
3486 // parameters are not counted here, since they remain at the start
3487 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003488 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003489 id->local_num = scope->num_locals;
3490 scope->num_locals += 1;
3491 }
3492#endif
Damien9ecbcff2013-12-11 00:41:43 +00003493 }
Damien9ecbcff2013-12-11 00:41:43 +00003494
3495 // compute the index of free vars (freevars[idx] in CPython)
3496 // make sure they are in the order of the parent scope
3497 if (scope->parent != NULL) {
3498 int num_free = 0;
3499 for (int i = 0; i < scope->parent->id_info_len; i++) {
3500 id_info_t *id = &scope->parent->id_info[i];
3501 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3502 for (int j = 0; j < scope->id_info_len; j++) {
3503 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003504 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003505 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003506#if MICROPY_EMIT_CPYTHON
3507 // in CPython the frees are numbered after the cells
3508 id2->local_num = num_cell + num_free;
3509#else
3510 // in Micro Python the frees come first, before the params
3511 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003512#endif
3513 num_free += 1;
3514 }
3515 }
3516 }
Damien429d7192013-10-04 19:53:11 +01003517 }
Damien George6baf76e2013-12-30 22:32:17 +00003518#if !MICROPY_EMIT_CPYTHON
3519 // in Micro Python shift all other locals after the free locals
3520 if (num_free > 0) {
3521 for (int i = 0; i < scope->id_info_len; i++) {
3522 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003523 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003524 id->local_num += num_free;
3525 }
3526 }
Damien George2827d622014-04-27 15:50:52 +01003527 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 +00003528 scope->num_locals += num_free;
3529 }
3530#endif
Damien429d7192013-10-04 19:53:11 +01003531 }
3532
Damien George8725f8f2014-02-15 19:33:11 +00003533 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003534
3535#if MICROPY_EMIT_CPYTHON
3536 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003537 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) {
3538 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003539 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003540 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003541 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 +00003542 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003543 }
3544 }
Damien George882b3632014-04-02 15:56:31 +01003545#endif
3546
Damien429d7192013-10-04 19:53:11 +01003547 int num_free = 0;
3548 for (int i = 0; i < scope->id_info_len; i++) {
3549 id_info_t *id = &scope->id_info[i];
3550 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3551 num_free += 1;
3552 }
3553 }
3554 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003555 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003556 }
3557}
3558
Damien George65cad122014-04-06 11:48:15 +01003559mp_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 +01003560 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003561 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003562 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003563 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003564
Damien826005c2013-10-05 23:17:28 +01003565 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003566 mp_map_t consts;
3567 mp_map_init(&consts, 0);
3568 pn = fold_constants(comp, pn, &consts);
3569 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003570
3571 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003572 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003573
Damien826005c2013-10-05 23:17:28 +01003574 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003575 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003576 comp->emit_method_table = &emit_pass1_method_table;
3577 comp->emit_inline_asm = NULL;
3578 comp->emit_inline_asm_method_table = NULL;
3579 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003580 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003581 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003582#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003583 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003584 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003585#endif
Damien826005c2013-10-05 23:17:28 +01003586 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003587 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003588 }
3589
3590 // update maximim number of labels needed
3591 if (comp->next_label > max_num_labels) {
3592 max_num_labels = comp->next_label;
3593 }
Damien429d7192013-10-04 19:53:11 +01003594 }
3595
Damien826005c2013-10-05 23:17:28 +01003596 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003597 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003598 compile_scope_compute_things(comp, s);
3599 }
3600
Damien826005c2013-10-05 23:17:28 +01003601 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003602 emit_pass1_free(comp->emit);
3603
Damien826005c2013-10-05 23:17:28 +01003604 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003605#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003606 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003607#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003608 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003609#endif
Damien3ef4abb2013-10-12 16:53:13 +01003610#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003611 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003612#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003613#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003614 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003615 if (false) {
3616 // dummy
3617
Damien3ef4abb2013-10-12 16:53:13 +01003618#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003619 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003620 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003621 if (emit_inline_thumb == NULL) {
3622 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3623 }
3624 comp->emit = NULL;
3625 comp->emit_method_table = NULL;
3626 comp->emit_inline_asm = emit_inline_thumb;
3627 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003628 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003629 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003630 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003631 }
Damienc025ebb2013-10-12 14:30:21 +01003632#endif
3633
Damien826005c2013-10-05 23:17:28 +01003634 } else {
Damienc025ebb2013-10-12 14:30:21 +01003635
3636 // choose the emit type
3637
Damien3ef4abb2013-10-12 16:53:13 +01003638#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003639 comp->emit = emit_cpython_new(max_num_labels);
3640 comp->emit_method_table = &emit_cpython_method_table;
3641#else
Damien826005c2013-10-05 23:17:28 +01003642 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003643
3644#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003645 case MP_EMIT_OPT_NATIVE_PYTHON:
3646 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003647#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003648 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003649 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003650 }
Damien13ed3a62013-10-08 09:05:10 +01003651 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003652#elif MICROPY_EMIT_X86
3653 if (emit_native == NULL) {
3654 emit_native = emit_native_x86_new(max_num_labels);
3655 }
3656 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003657#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003658 if (emit_native == NULL) {
3659 emit_native = emit_native_thumb_new(max_num_labels);
3660 }
3661 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003662#elif MICROPY_EMIT_ARM
3663 if (emit_native == NULL) {
3664 emit_native = emit_native_arm_new(max_num_labels);
3665 }
3666 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003667#endif
3668 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003669 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien George36db6bc2014-05-07 17:24:22 +01003670
3671 // native emitters need an extra pass to compute stack size
3672 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3673
Damien7af3d192013-10-07 00:02:49 +01003674 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003675#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003676
Damien826005c2013-10-05 23:17:28 +01003677 default:
3678 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003679 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003680 }
3681 comp->emit = emit_bc;
3682 comp->emit_method_table = &emit_bc_method_table;
3683 break;
3684 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003685#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003686
Damien George36db6bc2014-05-07 17:24:22 +01003687 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003688 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003689 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3690 }
3691
3692 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003693 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003694 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003695 }
Damien6cdd3af2013-10-05 18:08:26 +01003696 }
Damien429d7192013-10-04 19:53:11 +01003697 }
3698
Damien George41d02b62014-01-24 22:42:28 +00003699 // free the emitters
3700#if !MICROPY_EMIT_CPYTHON
3701 if (emit_bc != NULL) {
3702 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003703 }
Damien George41d02b62014-01-24 22:42:28 +00003704#if MICROPY_EMIT_NATIVE
3705 if (emit_native != NULL) {
3706#if MICROPY_EMIT_X64
3707 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003708#elif MICROPY_EMIT_X86
3709 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003710#elif MICROPY_EMIT_THUMB
3711 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003712#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003713 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003714#endif
3715 }
3716#endif
3717#if MICROPY_EMIT_INLINE_THUMB
3718 if (emit_inline_thumb != NULL) {
3719 emit_inline_thumb_free(emit_inline_thumb);
3720 }
3721#endif
3722#endif // !MICROPY_EMIT_CPYTHON
3723
Damien George52b5d762014-09-23 15:31:56 +00003724 // free the parse tree
3725 mp_parse_node_free(pn);
3726
Damien George41d02b62014-01-24 22:42:28 +00003727 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003728 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003729 for (scope_t *s = module_scope; s;) {
3730 scope_t *next = s->next;
3731 scope_free(s);
3732 s = next;
3733 }
Damien5ac1b2e2013-10-18 19:58:12 +01003734
Damien George41d02b62014-01-24 22:42:28 +00003735 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003736 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003737 m_del_obj(compiler_t, comp);
3738
Damien Georgea91ac202014-10-05 19:01:34 +01003739 if (compile_error != MP_OBJ_NULL) {
3740 return compile_error;
Damien George1fb03172014-01-03 14:22:03 +00003741 } else {
3742#if MICROPY_EMIT_CPYTHON
3743 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003744 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003745 return mp_const_true;
3746#else
3747 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003748 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003749#endif
3750 }
Damien429d7192013-10-04 19:53:11 +01003751}