blob: 3633503958e6682532cd0bdfbd620d9dae5071b3 [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
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200236 if (arg1 >= BITS_PER_WORD) {
237 // Shifting to big amounts is underfined behavior
238 // in C and is CPU-dependent; propagate sign bit.
239 arg1 = BITS_PER_WORD - 1;
240 }
Damiend99b0522013-12-21 18:17:45 +0000241 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100242 } else {
243 // shouldn't happen
244 assert(0);
245 }
246 }
247 break;
248
249 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100250 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000251 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 +0100252 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
253 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000254 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100255 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000256 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000257 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100258 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000259 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100260 } else {
261 // shouldn't happen
262 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100263 }
Damien Georged1e355e2014-05-28 14:51:12 +0100264 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100265 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000266 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100267 }
268 }
269 break;
270
271 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000272 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 +0100273 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
274 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000275 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100276 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000277 if (!mp_small_int_mul_overflow(arg0, arg1)) {
278 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100279 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000280 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
281 }
282 }
Damiend99b0522013-12-21 18:17:45 +0000283 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100284 // int / int
285 // pass
Damiend99b0522013-12-21 18:17:45 +0000286 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100287 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000288 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000289 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300290 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100291 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000292 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 +0300293 }
Damien429d7192013-10-04 19:53:11 +0100294 } else {
295 // shouldn't happen
296 assert(0);
297 }
298 }
299 break;
300
301 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000302 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100303 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000304 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100305 // +int
Damiend99b0522013-12-21 18:17:45 +0000306 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
307 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100308 // -int
Damiend99b0522013-12-21 18:17:45 +0000309 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
310 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100311 // ~int
Damiend99b0522013-12-21 18:17:45 +0000312 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100313 } else {
314 // shouldn't happen
315 assert(0);
316 }
317 }
318 break;
319
320 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100321 if (0) {
322#if MICROPY_EMIT_CPYTHON
323 } 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 +0100324 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100325 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000326 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
327 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200328 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100329 if (power >= 0) {
330 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200331 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100332 for (; power > 0; power--) {
333 ans *= base;
334 }
Damiend99b0522013-12-21 18:17:45 +0000335 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100336 }
337 }
Damien George57e99eb2014-04-10 22:42:11 +0100338#endif
339 } 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])) {
340 // id.id
341 // look it up in constant table, see if it can be replaced with an integer
342 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
343 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
344 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
345 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
346 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
347 if (elem != NULL) {
348 mp_obj_t dest[2];
349 mp_load_method_maybe(elem->value, q_attr, dest);
350 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100351 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georged1e355e2014-05-28 14:51:12 +0100352 if (MP_SMALL_INT_FITS(val)) {
Damien George57e99eb2014-04-10 22:42:11 +0100353 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
354 }
355 }
356 }
Damien429d7192013-10-04 19:53:11 +0100357 }
358 break;
359 }
360 }
361
362 return pn;
363}
364
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200365STATIC 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 +0100366STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000367STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100368
Damien George6f355fd2014-04-10 14:11:31 +0100369STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100370 return comp->next_label++;
371}
372
Damien George8dcc0c72014-03-27 10:55:21 +0000373STATIC void compile_increase_except_level(compiler_t *comp) {
374 comp->cur_except_level += 1;
375 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
376 comp->scope_cur->exc_stack_size = comp->cur_except_level;
377 }
378}
379
380STATIC void compile_decrease_except_level(compiler_t *comp) {
381 assert(comp->cur_except_level > 0);
382 comp->cur_except_level -= 1;
383}
384
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200385STATIC 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 +0100386 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100387 scope->parent = comp->scope_cur;
388 scope->next = NULL;
389 if (comp->scope_head == NULL) {
390 comp->scope_head = scope;
391 } else {
392 scope_t *s = comp->scope_head;
393 while (s->next != NULL) {
394 s = s->next;
395 }
396 s->next = scope;
397 }
398 return scope;
399}
400
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200401STATIC 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 +0000402 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
403 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
404 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100405 for (int i = 0; i < num_nodes; i++) {
406 f(comp, pns->nodes[i]);
407 }
Damiend99b0522013-12-21 18:17:45 +0000408 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100409 f(comp, pn);
410 }
411}
412
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200413STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000414 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100415 *nodes = NULL;
416 return 0;
Damiend99b0522013-12-21 18:17:45 +0000417 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100418 *nodes = pn;
419 return 1;
420 } else {
Damiend99b0522013-12-21 18:17:45 +0000421 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
422 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100423 *nodes = pn;
424 return 1;
425 } else {
426 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000427 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100428 }
429 }
430}
431
Damiend99b0522013-12-21 18:17:45 +0000432void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100433}
434
Damiend99b0522013-12-21 18:17:45 +0000435void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
436 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100437 for (int i = 0; i < num_nodes; i++) {
438 compile_node(comp, pns->nodes[i]);
439 }
440}
441
Damien3ef4abb2013-10-12 16:53:13 +0100442#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200443STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100444 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
445 return true;
446 }
Damiend99b0522013-12-21 18:17:45 +0000447 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100448 return false;
449 }
Damiend99b0522013-12-21 18:17:45 +0000450 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100451 return false;
452 }
453 return true;
454}
455
Damien George5042bce2014-05-25 22:06:06 +0100456STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000457 bool has_single_quote = false;
458 bool has_double_quote = false;
459 for (int i = 0; i < len; i++) {
460 if (str[i] == '\'') {
461 has_single_quote = true;
462 } else if (str[i] == '"') {
463 has_double_quote = true;
464 }
465 }
466 if (bytes) {
467 vstr_printf(vstr, "b");
468 }
469 bool quote_single = false;
470 if (has_single_quote && !has_double_quote) {
471 vstr_printf(vstr, "\"");
472 } else {
473 quote_single = true;
474 vstr_printf(vstr, "'");
475 }
476 for (int i = 0; i < len; i++) {
477 if (str[i] == '\n') {
478 vstr_printf(vstr, "\\n");
479 } else if (str[i] == '\\') {
480 vstr_printf(vstr, "\\\\");
481 } else if (str[i] == '\'' && quote_single) {
482 vstr_printf(vstr, "\\'");
483 } else {
484 vstr_printf(vstr, "%c", str[i]);
485 }
486 }
487 if (has_single_quote && !has_double_quote) {
488 vstr_printf(vstr, "\"");
489 } else {
490 vstr_printf(vstr, "'");
491 }
492}
493
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200494STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George5042bce2014-05-25 22:06:06 +0100495 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
496 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George40f3c022014-07-03 13:25:24 +0100497 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 +0100498 return;
499 }
500
Damiend99b0522013-12-21 18:17:45 +0000501 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200502 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
503 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
504 return;
505 }
506
Damien George42f3de92014-10-03 17:44:14 +0000507 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000508 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
509 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000510 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
511 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100512 case MP_PARSE_NODE_STRING:
513 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100514 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100515 const byte *str = qstr_data(arg, &len);
516 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
517 break;
518 }
Damiend99b0522013-12-21 18:17:45 +0000519 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100520 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000521 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
522 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
523 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100524 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100525 }
526 break;
527 default: assert(0);
528 }
529}
530
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200531STATIC 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 +0100532 int n = 0;
533 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000534 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100535 }
536 int total = n;
537 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000538 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100539 total += 1;
Damien3a205172013-10-12 15:01:56 +0100540 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100541 is_const = false;
542 }
543 }
544 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100545 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100546 is_const = false;
547 break;
548 }
549 }
550 if (total > 0 && is_const) {
551 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000552 vstr_t *vstr = vstr_new();
553 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000554 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000555 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100556 need_comma = true;
557 }
558 for (int i = 0; i < n; i++) {
559 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000560 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100561 }
Damien02f89412013-12-12 15:13:36 +0000562 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100563 need_comma = true;
564 }
565 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000566 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100567 } else {
Damien02f89412013-12-12 15:13:36 +0000568 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100569 }
Damien Georgeb9791222014-01-23 00:34:21 +0000570 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000571 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100572 } else {
Damiend99b0522013-12-21 18:17:45 +0000573 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100574 compile_node(comp, pn);
575 }
576 for (int i = 0; i < n; i++) {
577 compile_node(comp, pns_list->nodes[i]);
578 }
Damien Georgeb9791222014-01-23 00:34:21 +0000579 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100580 }
581}
Damien3a205172013-10-12 15:01:56 +0100582#endif
583
584// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100585STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100586#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100587 cpython_c_tuple(comp, pn, pns_list);
588#else
589 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000590 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100591 compile_node(comp, pn);
592 total += 1;
593 }
594 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000595 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100596 for (int i = 0; i < n; i++) {
597 compile_node(comp, pns_list->nodes[i]);
598 }
599 total += n;
600 }
Damien Georgeb9791222014-01-23 00:34:21 +0000601 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100602#endif
603}
Damien429d7192013-10-04 19:53:11 +0100604
Damiend99b0522013-12-21 18:17:45 +0000605void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100606 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000607 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100608}
609
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200610STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000611 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
612 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100613}
614
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200615STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000616 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
617 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100618}
619
Damien3ef4abb2013-10-12 16:53:13 +0100620#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100621// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200622STATIC 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 +0100623 if (node_is_const_false(pn)) {
624 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000625 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100626 }
627 return;
628 } else if (node_is_const_true(pn)) {
629 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000630 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100631 }
632 return;
Damiend99b0522013-12-21 18:17:45 +0000633 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
634 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
635 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
636 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100637 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100638 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100639 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100640 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100641 }
Damien3a205172013-10-12 15:01:56 +0100642 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000643 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100644 } else {
645 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100646 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100647 }
648 }
649 return;
Damiend99b0522013-12-21 18:17:45 +0000650 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100651 if (jump_if == false) {
652 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100653 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100654 }
655 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100656 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100657 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100658 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100659 }
Damien3a205172013-10-12 15:01:56 +0100660 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000661 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100662 }
663 return;
Damiend99b0522013-12-21 18:17:45 +0000664 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100665 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100666 return;
667 }
668 }
669
670 // nothing special, fall back to default compiling for node and jump
671 compile_node(comp, pn);
672 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000673 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100674 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000675 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100676 }
677}
Damien3a205172013-10-12 15:01:56 +0100678#endif
Damien429d7192013-10-04 19:53:11 +0100679
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200680STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100681#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100682 cpython_c_if_cond(comp, pn, jump_if, label, false);
683#else
684 if (node_is_const_false(pn)) {
685 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000686 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100687 }
688 return;
689 } else if (node_is_const_true(pn)) {
690 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000691 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100692 }
693 return;
Damiend99b0522013-12-21 18:17:45 +0000694 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
695 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
696 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
697 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100698 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100699 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100700 for (int i = 0; i < n - 1; i++) {
701 c_if_cond(comp, pns->nodes[i], true, label2);
702 }
703 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000704 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100705 } else {
706 for (int i = 0; i < n; i++) {
707 c_if_cond(comp, pns->nodes[i], true, label);
708 }
709 }
710 return;
Damiend99b0522013-12-21 18:17:45 +0000711 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100712 if (jump_if == false) {
713 for (int i = 0; i < n; i++) {
714 c_if_cond(comp, pns->nodes[i], false, label);
715 }
716 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100717 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100718 for (int i = 0; i < n - 1; i++) {
719 c_if_cond(comp, pns->nodes[i], false, label2);
720 }
721 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000722 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100723 }
724 return;
Damiend99b0522013-12-21 18:17:45 +0000725 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100726 c_if_cond(comp, pns->nodes[0], !jump_if, label);
727 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100728 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
729 // cond is something in parenthesis
730 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
731 // empty tuple, acts as false for the condition
732 if (jump_if == false) {
733 EMIT_ARG(jump, label);
734 }
735 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
736 // non-empty tuple, acts as true for the condition
737 if (jump_if == true) {
738 EMIT_ARG(jump, label);
739 }
740 } else {
741 // parenthesis around 1 item, is just that item
742 c_if_cond(comp, pns->nodes[0], jump_if, label);
743 }
744 return;
Damien3a205172013-10-12 15:01:56 +0100745 }
746 }
747
748 // nothing special, fall back to default compiling for node and jump
749 compile_node(comp, pn);
750 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000751 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100752 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000753 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100754 }
755#endif
Damien429d7192013-10-04 19:53:11 +0100756}
757
758typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100759STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100760
Damien George6be0b0a2014-08-15 14:30:52 +0100761STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100762 if (assign_kind != ASSIGN_AUG_STORE) {
763 compile_node(comp, pns->nodes[0]);
764 }
765
Damiend99b0522013-12-21 18:17:45 +0000766 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
767 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
768 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
769 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100770 if (assign_kind != ASSIGN_AUG_STORE) {
771 for (int i = 0; i < n - 1; i++) {
772 compile_node(comp, pns1->nodes[i]);
773 }
774 }
Damiend99b0522013-12-21 18:17:45 +0000775 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
776 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100777 }
Damiend99b0522013-12-21 18:17:45 +0000778 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100779 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000780 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100781 if (assign_kind == ASSIGN_AUG_STORE) {
782 EMIT(rot_three);
783 EMIT(store_subscr);
784 } else {
785 compile_node(comp, pns1->nodes[0]);
786 if (assign_kind == ASSIGN_AUG_LOAD) {
787 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100788 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100789 } else {
790 EMIT(store_subscr);
791 }
792 }
Damiend99b0522013-12-21 18:17:45 +0000793 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
794 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100795 if (assign_kind == ASSIGN_AUG_LOAD) {
796 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000797 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100798 } else {
799 if (assign_kind == ASSIGN_AUG_STORE) {
800 EMIT(rot_two);
801 }
Damien Georgeb9791222014-01-23 00:34:21 +0000802 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100803 }
804 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100805 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100806 }
807 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100808 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100809 }
810
Damiend99b0522013-12-21 18:17:45 +0000811 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100812 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100813 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100814
815 return;
816
817cannot_assign:
818 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100819}
820
Damien George0288cf02014-04-11 11:53:00 +0000821// 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 +0100822STATIC 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 +0000823 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
824
825 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100826 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000827 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
828 EMIT_ARG(unpack_ex, 0, num_tail);
829 have_star_index = 0;
830 }
831 for (int i = 0; i < num_tail; i++) {
832 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100833 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000834 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
835 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100836 } else {
Damien George9d181f62014-04-27 16:55:27 +0100837 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100838 return;
839 }
840 }
841 }
842 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000843 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100844 }
Damien George0288cf02014-04-11 11:53:00 +0000845 if (num_head != 0) {
846 if (0 == have_star_index) {
847 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100848 } else {
Damien George0288cf02014-04-11 11:53:00 +0000849 c_assign(comp, node_head, ASSIGN_STORE);
850 }
851 }
852 for (int i = 0; i < num_tail; i++) {
853 if (num_head + i == have_star_index) {
854 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
855 } else {
856 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100857 }
858 }
859}
860
861// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100862STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100863 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000864 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100865 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000866 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
867 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000868 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100869 switch (assign_kind) {
870 case ASSIGN_STORE:
871 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000872 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100873 break;
874 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000875 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100876 break;
877 }
878 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100879 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100880 return;
881 }
882 } else {
Damiend99b0522013-12-21 18:17:45 +0000883 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
884 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100885 case PN_power:
886 // lhs is an index or attribute
887 c_assign_power(comp, pns, assign_kind);
888 break;
889
890 case PN_testlist_star_expr:
891 case PN_exprlist:
892 // lhs is a tuple
893 if (assign_kind != ASSIGN_STORE) {
894 goto bad_aug;
895 }
Damien George0288cf02014-04-11 11:53:00 +0000896 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100897 break;
898
899 case PN_atom_paren:
900 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000901 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100902 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100903 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000904 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
905 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100906 goto testlist_comp;
907 } else {
908 // parenthesis around 1 item, is just that item
909 pn = pns->nodes[0];
910 goto tail_recursion;
911 }
912 break;
913
914 case PN_atom_bracket:
915 // lhs is something in brackets
916 if (assign_kind != ASSIGN_STORE) {
917 goto bad_aug;
918 }
Damiend99b0522013-12-21 18:17:45 +0000919 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100920 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000921 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000922 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
923 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100924 goto testlist_comp;
925 } else {
926 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000927 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100928 }
929 break;
930
931 default:
Damien George9d181f62014-04-27 16:55:27 +0100932 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100933 }
934 return;
935
936 testlist_comp:
937 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000938 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
939 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
940 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100941 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000942 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000943 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000944 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100945 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000946 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
947 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000948 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100949 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100950 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100951 } else {
952 // sequence with 2 items
953 goto sequence_with_2_items;
954 }
955 } else {
956 // sequence with 2 items
957 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000958 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100959 }
960 return;
961 }
962 return;
963
Damien George9d181f62014-04-27 16:55:27 +0100964 cannot_assign:
965 compile_syntax_error(comp, pn, "can't assign to expression");
966 return;
967
Damien429d7192013-10-04 19:53:11 +0100968 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100969 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100970}
971
972// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100973// if we are not in CPython compatibility mode then:
974// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
975// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
976// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100977STATIC 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 +0100978 assert(n_pos_defaults >= 0);
979 assert(n_kw_defaults >= 0);
980
Damien429d7192013-10-04 19:53:11 +0100981 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000982 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100983 int nfree = 0;
984 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000985 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
986 id_info_t *id = &comp->scope_cur->id_info[i];
987 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
988 for (int j = 0; j < this_scope->id_info_len; j++) {
989 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100990 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000991#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100992 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000993#else
994 // in Micro Python we load closures using LOAD_FAST
Damien George7ff996c2014-09-08 23:05:16 +0100995 EMIT_ARG(load_fast, id->qst, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000996#endif
Damien318aec62013-12-10 18:28:17 +0000997 nfree += 1;
998 }
999 }
Damien429d7192013-10-04 19:53:11 +01001000 }
1001 }
1002 }
Damien429d7192013-10-04 19:53:11 +01001003
1004 // make the function/closure
1005 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001006 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001007 } else {
Damien George3558f622014-04-20 17:50:40 +01001008 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001009 }
1010}
1011
Damien George6be0b0a2014-08-15 14:30:52 +01001012STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001013 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001014 comp->have_star = true;
1015 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001016 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1017 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001018 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001019 } else {
1020 // named star
Damien429d7192013-10-04 19:53:11 +01001021 }
Damien George8b19db02014-04-11 23:25:34 +01001022 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001023
1024 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001025 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001026 // TODO do we need to do anything with this?
1027
1028 } else {
1029 mp_parse_node_t pn_id;
1030 mp_parse_node_t pn_colon;
1031 mp_parse_node_t pn_equal;
1032 if (MP_PARSE_NODE_IS_ID(pn)) {
1033 // this parameter is just an id
1034
1035 pn_id = pn;
1036 pn_colon = MP_PARSE_NODE_NULL;
1037 pn_equal = MP_PARSE_NODE_NULL;
1038
1039 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1040 // this parameter has a colon and/or equal specifier
1041
1042 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1043 pn_id = pns->nodes[0];
1044 pn_colon = pns->nodes[1];
1045 pn_equal = pns->nodes[2];
1046
1047 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001048 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001049 assert(0);
1050 return;
1051 }
1052
1053 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1054 // this parameter does not have a default value
1055
1056 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001057 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001058 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001059 return;
1060 }
1061
1062 } else {
1063 // this parameter has a default value
1064 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1065
Damien George8b19db02014-04-11 23:25:34 +01001066 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001067 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001068#if MICROPY_EMIT_CPYTHON
1069 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1070 compile_node(comp, pn_equal);
1071#else
Damien George69b89d22014-04-11 13:38:30 +00001072 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1073 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001074 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1075 // we need to do this here before we start building the map for the default keywords
1076 if (comp->num_default_params > 0) {
1077 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001078 } else {
1079 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001080 }
Damien George69b89d22014-04-11 13:38:30 +00001081 // first default dict param, so make the map
1082 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001083 }
Damien Georgef0778a72014-06-07 22:01:00 +01001084
1085 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001086 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001087 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001088 EMIT(store_map);
1089#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001090 } else {
Damien George69b89d22014-04-11 13:38:30 +00001091 comp->num_default_params += 1;
1092 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001093 }
1094 }
1095
1096 // TODO pn_colon not implemented
1097 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001098 }
1099}
1100
1101// leaves function object on stack
1102// returns function name
Damiend99b0522013-12-21 18:17:45 +00001103qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001104 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001105 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001106 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001107 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001108 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001109 }
1110
1111 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001112 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001113 uint old_num_dict_params = comp->num_dict_params;
1114 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001115
1116 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001117 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001118 comp->num_dict_params = 0;
1119 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001120 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001121
Damien Georgea91ac202014-10-05 19:01:34 +01001122 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001123 return MP_QSTR_NULL;
1124 }
1125
Damien Georgee337f1e2014-03-31 15:18:37 +01001126#if !MICROPY_EMIT_CPYTHON
1127 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001128 // the default keywords args may have already made the tuple; if not, do it now
1129 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001130 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001131 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001132 }
1133#endif
1134
Damien429d7192013-10-04 19:53:11 +01001135 // get the scope for this function
1136 scope_t *fscope = (scope_t*)pns->nodes[4];
1137
1138 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001139 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001140
1141 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001142 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001143 comp->num_dict_params = old_num_dict_params;
1144 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001145
1146 // return its name (the 'f' in "def f(...):")
1147 return fscope->simple_name;
1148}
1149
1150// leaves class object on stack
1151// returns class name
Damiend99b0522013-12-21 18:17:45 +00001152qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001153 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001154 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001155 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001156 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001157 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001158 }
1159
1160 EMIT(load_build_class);
1161
1162 // scope for this class
1163 scope_t *cscope = (scope_t*)pns->nodes[3];
1164
1165 // compile the class
1166 close_over_variables_etc(comp, cscope, 0, 0);
1167
1168 // get its name
Damien George968bf342014-04-27 19:12:05 +01001169 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001170
1171 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001172 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1173 mp_parse_node_t parents = pns->nodes[1];
1174 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1175 parents = MP_PARSE_NODE_NULL;
1176 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001177 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001178 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001179
1180 // return its name (the 'C' in class C(...):")
1181 return cscope->simple_name;
1182}
1183
Damien6cdd3af2013-10-05 18:08:26 +01001184// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001185STATIC 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 +00001186 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001187 return false;
1188 }
1189
1190 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001191 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001192 return true;
1193 }
1194
Damiend99b0522013-12-21 18:17:45 +00001195 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001196 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001197 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001198#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001199 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001200 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001201 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001202 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001203#endif
Damien3ef4abb2013-10-12 16:53:13 +01001204#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001205 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001206 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001207#endif
Damien6cdd3af2013-10-05 18:08:26 +01001208 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001209 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001210 }
1211
1212 return true;
1213}
1214
Damiend99b0522013-12-21 18:17:45 +00001215void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001216 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001217 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001218 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1219
Damien6cdd3af2013-10-05 18:08:26 +01001220 // inherit emit options for this function/class definition
1221 uint emit_options = comp->scope_cur->emit_options;
1222
1223 // compile each decorator
1224 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001225 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001226 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1227 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001228
1229 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001230 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001231 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1232
1233 // check for built-in decorators
1234 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1235 // this was a built-in
1236 num_built_in_decorators += 1;
1237
1238 } else {
1239 // not a built-in, compile normally
1240
1241 // compile the decorator function
1242 compile_node(comp, name_nodes[0]);
1243 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001244 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001245 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001246 }
1247
1248 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001249 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001250 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001251 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001252 compile_node(comp, pns_decorator->nodes[1]);
1253 }
Damien429d7192013-10-04 19:53:11 +01001254 }
1255 }
1256
1257 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001258 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001259 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001260 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001261 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001262 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001263 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001264 } else {
1265 // shouldn't happen
1266 assert(0);
1267 }
1268
1269 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001270 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001271 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001272 }
1273
1274 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001275 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001276}
1277
Damiend99b0522013-12-21 18:17:45 +00001278void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001279 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001280 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001281 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001282}
1283
Damien George6be0b0a2014-08-15 14:30:52 +01001284STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001285 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001286 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001287 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1288 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001289
1290 compile_node(comp, pns->nodes[0]); // base of the power node
1291
Damiend99b0522013-12-21 18:17:45 +00001292 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1293 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1294 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1295 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001296 for (int i = 0; i < n - 1; i++) {
1297 compile_node(comp, pns1->nodes[i]);
1298 }
Damiend99b0522013-12-21 18:17:45 +00001299 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1300 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001301 }
Damiend99b0522013-12-21 18:17:45 +00001302 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001303 // can't delete function calls
1304 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001305 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001306 compile_node(comp, pns1->nodes[0]);
1307 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001308 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1309 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001310 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001311 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001312 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001313 }
1314 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001315 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001316 }
1317
Damiend99b0522013-12-21 18:17:45 +00001318 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001319 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001320 }
Damiend99b0522013-12-21 18:17:45 +00001321 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1322 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1323 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1324 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001325 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1326
Damiend99b0522013-12-21 18:17:45 +00001327 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1328 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1329 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001330 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001331 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001332 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001333 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001334 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001335 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001336 c_del_stmt(comp, pns->nodes[0]);
1337 for (int i = 0; i < n; i++) {
1338 c_del_stmt(comp, pns1->nodes[i]);
1339 }
Damiend99b0522013-12-21 18:17:45 +00001340 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001341 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001342 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001343 } else {
1344 // sequence with 2 items
1345 goto sequence_with_2_items;
1346 }
1347 } else {
1348 // sequence with 2 items
1349 sequence_with_2_items:
1350 c_del_stmt(comp, pns->nodes[0]);
1351 c_del_stmt(comp, pns->nodes[1]);
1352 }
1353 } else {
1354 // tuple with 1 element
1355 c_del_stmt(comp, pn);
1356 }
1357 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001358 // TODO is there anything else to implement?
1359 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001360 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001361
1362 return;
1363
1364cannot_delete:
1365 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001366}
1367
Damiend99b0522013-12-21 18:17:45 +00001368void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001369 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1370}
1371
Damiend99b0522013-12-21 18:17:45 +00001372void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001373 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001374 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001375 }
Damien George090c9232014-10-17 14:08:49 +00001376 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001377 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001378}
1379
Damiend99b0522013-12-21 18:17:45 +00001380void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001381 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001382 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001383 }
Damien George090c9232014-10-17 14:08:49 +00001384 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001385 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001386}
1387
Damiend99b0522013-12-21 18:17:45 +00001388void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001389 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001390 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001391 return;
1392 }
Damiend99b0522013-12-21 18:17:45 +00001393 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001394 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001395 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001396 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001397 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001398 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1399 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 +01001400
Damien George6f355fd2014-04-10 14:11:31 +01001401 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001402 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1403 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1404 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001405 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001406 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1407 } else {
1408 compile_node(comp, pns->nodes[0]);
1409 }
1410 EMIT(return_value);
1411}
1412
Damiend99b0522013-12-21 18:17:45 +00001413void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001414 compile_node(comp, pns->nodes[0]);
1415 EMIT(pop_top);
1416}
1417
Damiend99b0522013-12-21 18:17:45 +00001418void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1419 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001420 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001421 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001422 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001423 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001424 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001425 compile_node(comp, pns->nodes[0]);
1426 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001427 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001428 } else {
1429 // raise x
1430 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001431 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001432 }
1433}
1434
Damien George635543c2014-04-10 12:56:52 +01001435// q_base holds the base of the name
1436// eg a -> q_base=a
1437// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001438STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001439 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001440 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1441 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001442 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001443 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001444 pn = pns->nodes[0];
1445 is_as = true;
1446 }
Damien George635543c2014-04-10 12:56:52 +01001447 if (MP_PARSE_NODE_IS_NULL(pn)) {
1448 // empty name (eg, from . import x)
1449 *q_base = MP_QSTR_;
1450 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1451 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001452 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001453 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001454 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001455 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001456 }
Damien George635543c2014-04-10 12:56:52 +01001457 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001458 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1459 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1460 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001461 // a name of the form a.b.c
1462 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001463 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001464 }
Damiend99b0522013-12-21 18:17:45 +00001465 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001466 int len = n - 1;
1467 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001468 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001469 }
Damien George55baff42014-01-21 21:40:13 +00001470 byte *q_ptr;
1471 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001472 for (int i = 0; i < n; i++) {
1473 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001474 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001475 }
Damien George39dc1452014-10-03 19:52:22 +01001476 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001477 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001478 memcpy(str_dest, str_src, str_src_len);
1479 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001480 }
Damien George635543c2014-04-10 12:56:52 +01001481 qstr q_full = qstr_build_end(q_ptr);
1482 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001483 if (is_as) {
1484 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001485 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001486 }
1487 }
1488 } else {
Damien George635543c2014-04-10 12:56:52 +01001489 // shouldn't happen
1490 assert(0);
Damien429d7192013-10-04 19:53:11 +01001491 }
1492 } else {
Damien George635543c2014-04-10 12:56:52 +01001493 // shouldn't happen
1494 assert(0);
Damien429d7192013-10-04 19:53:11 +01001495 }
1496}
1497
Damien George6be0b0a2014-08-15 14:30:52 +01001498STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001499 EMIT_ARG(load_const_small_int, 0); // level 0 import
1500 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1501 qstr q_base;
1502 do_import_name(comp, pn, &q_base);
1503 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001504}
1505
Damiend99b0522013-12-21 18:17:45 +00001506void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001507 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1508}
1509
Damiend99b0522013-12-21 18:17:45 +00001510void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001511 mp_parse_node_t pn_import_source = pns->nodes[0];
1512
1513 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1514 uint import_level = 0;
1515 do {
1516 mp_parse_node_t pn_rel;
1517 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)) {
1518 // This covers relative imports with dots only like "from .. import"
1519 pn_rel = pn_import_source;
1520 pn_import_source = MP_PARSE_NODE_NULL;
1521 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1522 // This covers relative imports starting with dot(s) like "from .foo import"
1523 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1524 pn_rel = pns_2b->nodes[0];
1525 pn_import_source = pns_2b->nodes[1];
1526 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1527 } else {
1528 // Not a relative import
1529 break;
1530 }
1531
1532 // get the list of . and/or ...'s
1533 mp_parse_node_t *nodes;
1534 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1535
1536 // count the total number of .'s
1537 for (int i = 0; i < n; i++) {
1538 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1539 import_level++;
1540 } else {
1541 // should be an MP_TOKEN_ELLIPSIS
1542 import_level += 3;
1543 }
1544 }
1545 } while (0);
1546
Damiend99b0522013-12-21 18:17:45 +00001547 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001548 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001549
1550 // build the "fromlist" tuple
1551#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001552 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001553#else
Damien George708c0732014-04-27 19:23:46 +01001554 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001555 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001556#endif
1557
1558 // do the import
Damien George635543c2014-04-10 12:56:52 +01001559 qstr dummy_q;
1560 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001561 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001562
Damien429d7192013-10-04 19:53:11 +01001563 } else {
Damien George635543c2014-04-10 12:56:52 +01001564 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001565
1566 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001567 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001568 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001569#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001570 {
1571 vstr_t *vstr = vstr_new();
1572 vstr_printf(vstr, "(");
1573 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001574 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1575 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1576 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001577 if (i > 0) {
1578 vstr_printf(vstr, ", ");
1579 }
1580 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001581 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001582 const byte *str = qstr_data(id2, &len);
1583 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001584 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001585 }
Damien02f89412013-12-12 15:13:36 +00001586 if (n == 1) {
1587 vstr_printf(vstr, ",");
1588 }
1589 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001590 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001591 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001592 }
Damiendb4c3612013-12-10 17:27:24 +00001593#else
1594 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001595 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1596 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1597 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001598 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001599 }
Damien Georgeb9791222014-01-23 00:34:21 +00001600 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001601#endif
1602
1603 // do the import
Damien George635543c2014-04-10 12:56:52 +01001604 qstr dummy_q;
1605 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001606 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001607 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1608 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1609 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001610 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001611 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001612 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001613 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001614 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001615 }
1616 }
1617 EMIT(pop_top);
1618 }
1619}
1620
Damiend99b0522013-12-21 18:17:45 +00001621void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001622 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001623 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1624 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001625 } else {
Damiend99b0522013-12-21 18:17:45 +00001626 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1627 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001628 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001629 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001630 }
Damien429d7192013-10-04 19:53:11 +01001631 }
1632 }
1633}
1634
Damiend99b0522013-12-21 18:17:45 +00001635void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001636 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001637 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1638 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001639 } else {
Damiend99b0522013-12-21 18:17:45 +00001640 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1641 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001642 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001643 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001644 }
Damien429d7192013-10-04 19:53:11 +01001645 }
1646 }
1647}
1648
Damiend99b0522013-12-21 18:17:45 +00001649void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001650 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001651 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001652 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 +00001653 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001654 // assertion message
1655 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001656 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001657 }
Damien Georgeb9791222014-01-23 00:34:21 +00001658 EMIT_ARG(raise_varargs, 1);
1659 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001660}
1661
Damiend99b0522013-12-21 18:17:45 +00001662void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001663 // TODO proper and/or short circuiting
1664
Damien George6f355fd2014-04-10 14:11:31 +01001665 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001666
Damien George391db862014-10-17 17:57:33 +00001667 // optimisation: don't emit anything when "if False" (not in CPython)
1668 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1669 uint l_fail = comp_next_label(comp);
1670 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001671
Damien George391db862014-10-17 17:57:33 +00001672 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001673
Damien George391db862014-10-17 17:57:33 +00001674 // optimisation: skip everything else when "if True" (not in CPython)
1675 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1676 goto done;
1677 }
1678
1679 if (
1680 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1681 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1682 // optimisation: don't jump if last instruction was return
1683 && !EMIT(last_emit_was_return_value)
1684 ) {
1685 // jump over elif/else blocks
1686 EMIT_ARG(jump, l_end);
1687 }
1688
1689 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001690 }
1691
Damien George235f9b32014-10-17 17:30:16 +00001692 // compile elif blocks (if any)
1693 mp_parse_node_t *pn_elif;
1694 int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1695 for (int i = 0; i < n_elif; i++) {
1696 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1697 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001698
Damien George391db862014-10-17 17:57:33 +00001699 // optimisation: don't emit anything when "if False" (not in CPython)
1700 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1701 uint l_fail = comp_next_label(comp);
1702 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001703
Damien George391db862014-10-17 17:57:33 +00001704 compile_node(comp, pns_elif->nodes[1]); // elif block
1705
1706 // optimisation: skip everything else when "elif True" (not in CPython)
1707 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1708 goto done;
1709 }
1710
1711 // optimisation: don't jump if last instruction was return
1712 if (!EMIT(last_emit_was_return_value)) {
1713 EMIT_ARG(jump, l_end);
1714 }
1715 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001716 }
1717 }
1718
1719 // compile else block
1720 compile_node(comp, pns->nodes[3]); // can be null
1721
Damien George391db862014-10-17 17:57:33 +00001722done:
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001724}
1725
Damien Georgecbddb272014-02-01 20:08:18 +00001726#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001727 uint16_t old_break_label = comp->break_label; \
1728 uint16_t old_continue_label = comp->continue_label; \
1729 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001730 uint break_label = comp_next_label(comp); \
1731 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001732 comp->break_label = break_label; \
1733 comp->continue_label = continue_label; \
1734 comp->break_continue_except_level = comp->cur_except_level;
1735
1736#define END_BREAK_CONTINUE_BLOCK \
1737 comp->break_label = old_break_label; \
1738 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001739 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001740
Damiend99b0522013-12-21 18:17:45 +00001741void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001742 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001743
Damience89a212013-10-15 22:25:17 +01001744 // compared to CPython, we have an optimised version of while loops
1745#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001746 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001747 EMIT_ARG(setup_loop, break_label);
1748 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001749 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1750 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001751 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001752 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001753 }
Damien Georgeb9791222014-01-23 00:34:21 +00001754 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001755 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1756 // this is a small hack to agree with CPython
1757 if (!node_is_const_true(pns->nodes[0])) {
1758 EMIT(pop_block);
1759 }
Damience89a212013-10-15 22:25:17 +01001760#else
Damien George391db862014-10-17 17:57:33 +00001761 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1762 uint top_label = comp_next_label(comp);
1763 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1764 EMIT_ARG(jump, continue_label);
1765 }
1766 EMIT_ARG(label_assign, top_label);
1767 compile_node(comp, pns->nodes[1]); // body
1768 EMIT_ARG(label_assign, continue_label);
1769 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1770 }
Damience89a212013-10-15 22:25:17 +01001771#endif
1772
1773 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001774 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001775
1776 compile_node(comp, pns->nodes[2]); // else
1777
Damien Georgeb9791222014-01-23 00:34:21 +00001778 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001779}
1780
Damien George2ac4af62014-08-15 16:45:41 +01001781#if !MICROPY_EMIT_CPYTHON
Damienf72fd0e2013-11-06 20:20:49 +00001782// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001783// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1784// And, if the loop never runs, the loop variable should never be assigned
Damien George6be0b0a2014-08-15 14:30:52 +01001785STATIC 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 +00001786 START_BREAK_CONTINUE_BLOCK
Damien George5b5562c2014-05-31 17:59:11 +01001787 // note that we don't need to pop anything when breaking from an optimise for loop
Damienf72fd0e2013-11-06 20:20:49 +00001788
Damien George6f355fd2014-04-10 14:11:31 +01001789 uint top_label = comp_next_label(comp);
1790 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001791
Damien George3ff2d032014-03-31 18:02:22 +01001792 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001793 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001794 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001795
Damien Georgeb9791222014-01-23 00:34:21 +00001796 EMIT_ARG(jump, entry_label);
1797 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001798
Damien George3ff2d032014-03-31 18:02:22 +01001799 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001800 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001801
1802 // store next value to var
1803 c_assign(comp, pn_var, ASSIGN_STORE);
1804
Damienf3822fc2013-11-09 20:12:03 +00001805 // compile body
1806 compile_node(comp, pn_body);
1807
Damien Georgeb9791222014-01-23 00:34:21 +00001808 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001809
Damien George3ff2d032014-03-31 18:02:22 +01001810 // compile: var + step, duplicated on stack
1811 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001812 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001813 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001814 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001815
Damien Georgeb9791222014-01-23 00:34:21 +00001816 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001817
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001818 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001819 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001820 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1821 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001822 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001823 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001824 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001825 }
Damien Georgeb9791222014-01-23 00:34:21 +00001826 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001827
Damien George3ff2d032014-03-31 18:02:22 +01001828 // discard final value of var that failed the loop condition
1829 EMIT(pop_top);
1830
Damienf72fd0e2013-11-06 20:20:49 +00001831 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001832 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001833
1834 compile_node(comp, pn_else);
1835
Damien Georgeb9791222014-01-23 00:34:21 +00001836 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001837}
Damien George2ac4af62014-08-15 16:45:41 +01001838#endif
Damienf72fd0e2013-11-06 20:20:49 +00001839
Damiend99b0522013-12-21 18:17:45 +00001840void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001841#if !MICROPY_EMIT_CPYTHON
1842 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1843 // this is actually slower, but uses no heap memory
1844 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001845 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 +00001846 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001847 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1848 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1849 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1850 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001851 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1852 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001853 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001854 mp_parse_node_t pn_range_start;
1855 mp_parse_node_t pn_range_end;
1856 mp_parse_node_t pn_range_step;
1857 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001858 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001859 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001860 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001861 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001862 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001863 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001864 } else if (n_args == 2) {
1865 pn_range_start = args[0];
1866 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001867 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001868 } else {
1869 pn_range_start = args[0];
1870 pn_range_end = args[1];
1871 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001872 // We need to know sign of step. This is possible only if it's constant
1873 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1874 optimize = false;
1875 }
Damienf72fd0e2013-11-06 20:20:49 +00001876 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001877 }
1878 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001879 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1880 return;
1881 }
1882 }
1883 }
1884#endif
1885
Damien Georgecbddb272014-02-01 20:08:18 +00001886 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001887 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001888
Damien George6f355fd2014-04-10 14:11:31 +01001889 uint pop_label = comp_next_label(comp);
1890 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001891
Damience89a212013-10-15 22:25:17 +01001892 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1893#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001894 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001895#endif
1896
Damien429d7192013-10-04 19:53:11 +01001897 compile_node(comp, pns->nodes[1]); // iterator
1898 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001899 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001900 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001901 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1902 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001903 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001904 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001905 }
Damien Georgeb9791222014-01-23 00:34:21 +00001906 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001907 EMIT(for_iter_end);
1908
1909 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001910 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001911
Damience89a212013-10-15 22:25:17 +01001912#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001913 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001914#endif
Damien429d7192013-10-04 19:53:11 +01001915
1916 compile_node(comp, pns->nodes[3]); // else (not tested)
1917
Damien Georgeb9791222014-01-23 00:34:21 +00001918 EMIT_ARG(label_assign, break_label);
1919 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001920}
1921
Damien George6be0b0a2014-08-15 14:30:52 +01001922STATIC 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 +01001923 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001924 uint l1 = comp_next_label(comp);
1925 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001926
Damien Georgeb9791222014-01-23 00:34:21 +00001927 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001928 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001929
Damien429d7192013-10-04 19:53:11 +01001930 compile_node(comp, pn_body); // body
1931 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001932 EMIT_ARG(jump, success_label); // jump over exception handler
1933
1934 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001935 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001936
Damien George6f355fd2014-04-10 14:11:31 +01001937 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001938
1939 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001940 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1941 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001942
1943 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001944 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001945
Damiend99b0522013-12-21 18:17:45 +00001946 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001947 // this is a catch all exception handler
1948 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001949 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001950 return;
1951 }
1952 } else {
1953 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001954 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1955 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1956 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1957 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001958 // handler binds the exception to a local
1959 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001960 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001961 }
1962 }
1963 EMIT(dup_top);
1964 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001965 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001966 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001967 }
1968
1969 EMIT(pop_top);
1970
1971 if (qstr_exception_local == 0) {
1972 EMIT(pop_top);
1973 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001974 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001975 }
1976
1977 EMIT(pop_top);
1978
Damien George6f355fd2014-04-10 14:11:31 +01001979 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001980 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001981 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001983 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001984 }
1985 compile_node(comp, pns_except->nodes[1]);
1986 if (qstr_exception_local != 0) {
1987 EMIT(pop_block);
1988 }
1989 EMIT(pop_except);
1990 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001991 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1992 EMIT_ARG(label_assign, l3);
1993 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1994 EMIT_ARG(store_id, qstr_exception_local);
1995 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001996
Damien George8dcc0c72014-03-27 10:55:21 +00001997 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001998 EMIT(end_finally);
1999 }
Damien Georgeb9791222014-01-23 00:34:21 +00002000 EMIT_ARG(jump, l2);
2001 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002002 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002003 }
2004
Damien George8dcc0c72014-03-27 10:55:21 +00002005 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002006 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002007 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002008
Damien Georgeb9791222014-01-23 00:34:21 +00002009 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002010 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002011 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002012}
2013
Damien George6be0b0a2014-08-15 14:30:52 +01002014STATIC 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 +01002015 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002016
Damien Georgeb9791222014-01-23 00:34:21 +00002017 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002018 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002019
Damien429d7192013-10-04 19:53:11 +01002020 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002021 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002022 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002023 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002024 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002025 } else {
2026 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2027 }
2028 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002029 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2030 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002031 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002032
Damien George8dcc0c72014-03-27 10:55:21 +00002033 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002034 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002035}
2036
Damiend99b0522013-12-21 18:17:45 +00002037void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2038 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2039 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2040 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002041 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002042 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2043 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002044 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002045 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002046 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002047 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002048 // no finally
2049 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2050 } else {
2051 // have finally
Damiend99b0522013-12-21 18:17:45 +00002052 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 +01002053 }
2054 } else {
2055 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002056 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002057 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002058 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002059 }
2060 } else {
2061 // shouldn't happen
2062 assert(0);
2063 }
2064}
2065
Damien George6be0b0a2014-08-15 14:30:52 +01002066STATIC 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 +01002067 if (n == 0) {
2068 // no more pre-bits, compile the body of the with
2069 compile_node(comp, body);
2070 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002071 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002072 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002073 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002074 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002075 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002076 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002077 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2078 } else {
2079 // this pre-bit is just an expression
2080 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002081 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002082 EMIT(pop_top);
2083 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002084 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002085 // compile additional pre-bits and the body
2086 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2087 // finish this with block
2088 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002089 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2090 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002091 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002092 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002093 EMIT(end_finally);
2094 }
2095}
2096
Damiend99b0522013-12-21 18:17:45 +00002097void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002098 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002099 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002100 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2101 assert(n > 0);
2102
2103 // compile in a nested fashion
2104 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2105}
2106
Damiend99b0522013-12-21 18:17:45 +00002107void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2108 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002109 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2110 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002111 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002112 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002113 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002114 EMIT(pop_top);
2115
Damien429d7192013-10-04 19:53:11 +01002116 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002117 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002118 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2119 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002120 // do nothing with a lonely constant
2121 } else {
2122 compile_node(comp, pns->nodes[0]); // just an expression
2123 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2124 }
Damien429d7192013-10-04 19:53:11 +01002125 }
2126 } else {
Damiend99b0522013-12-21 18:17:45 +00002127 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2128 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002129 if (kind == PN_expr_stmt_augassign) {
2130 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2131 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002132 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002133 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002134 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002135 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2136 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2137 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2138 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2139 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2140 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2141 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2142 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2143 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2144 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2145 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2146 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2147 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002148 }
Damien George7e5fb242014-02-01 22:18:47 +00002149 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002150 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2151 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002152 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2153 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002154 // following CPython, we store left-most first
2155 if (rhs > 0) {
2156 EMIT(dup_top);
2157 }
2158 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2159 for (int i = 0; i < rhs; i++) {
2160 if (i + 1 < rhs) {
2161 EMIT(dup_top);
2162 }
Damiend99b0522013-12-21 18:17:45 +00002163 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002164 }
2165 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002166 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002167 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2168 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2169 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002170 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002171 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2172 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002173 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2174 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2175 // can't optimise when it's a star expression on the lhs
2176 goto no_optimisation;
2177 }
Damien429d7192013-10-04 19:53:11 +01002178 compile_node(comp, pns10->nodes[0]); // rhs
2179 compile_node(comp, pns10->nodes[1]); // rhs
2180 EMIT(rot_two);
2181 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2182 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002183 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2184 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2185 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2186 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002187 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002188 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2189 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002190 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2191 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2192 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2193 // can't optimise when it's a star expression on the lhs
2194 goto no_optimisation;
2195 }
Damien429d7192013-10-04 19:53:11 +01002196 compile_node(comp, pns10->nodes[0]); // rhs
2197 compile_node(comp, pns10->nodes[1]); // rhs
2198 compile_node(comp, pns10->nodes[2]); // rhs
2199 EMIT(rot_three);
2200 EMIT(rot_two);
2201 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2202 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2203 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2204 } else {
Damien George495d7812014-04-08 17:51:47 +01002205 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002206 compile_node(comp, pns1->nodes[0]); // rhs
2207 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2208 }
2209 } else {
2210 // shouldn't happen
2211 assert(0);
2212 }
2213 }
2214}
2215
Damien George6be0b0a2014-08-15 14:30:52 +01002216STATIC 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 +00002217 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002218 compile_node(comp, pns->nodes[0]);
2219 for (int i = 1; i < num_nodes; i += 1) {
2220 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002221 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002222 }
2223}
2224
Damiend99b0522013-12-21 18:17:45 +00002225void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2226 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2227 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002228
Damien George6f355fd2014-04-10 14:11:31 +01002229 uint l_fail = comp_next_label(comp);
2230 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002231 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2232 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002233 EMIT_ARG(jump, l_end);
2234 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002235 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002236 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002237 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002238}
2239
Damiend99b0522013-12-21 18:17:45 +00002240void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002241 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002242 //mp_parse_node_t pn_params = pns->nodes[0];
2243 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002244
Damien George36db6bc2014-05-07 17:24:22 +01002245 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002246 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002247 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 +01002248 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002249 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002250 }
2251
2252 // get the scope for this lambda
2253 scope_t *this_scope = (scope_t*)pns->nodes[2];
2254
2255 // make the lambda
2256 close_over_variables_etc(comp, this_scope, 0, 0);
2257}
2258
Damiend99b0522013-12-21 18:17:45 +00002259void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002260 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002261 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002262 for (int i = 0; i < n; i += 1) {
2263 compile_node(comp, pns->nodes[i]);
2264 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002265 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002266 }
2267 }
Damien Georgeb9791222014-01-23 00:34:21 +00002268 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002269}
2270
Damiend99b0522013-12-21 18:17:45 +00002271void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002272 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002273 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002274 for (int i = 0; i < n; i += 1) {
2275 compile_node(comp, pns->nodes[i]);
2276 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002277 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002278 }
2279 }
Damien Georgeb9791222014-01-23 00:34:21 +00002280 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002281}
2282
Damiend99b0522013-12-21 18:17:45 +00002283void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002284 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002285 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002286}
2287
Damiend99b0522013-12-21 18:17:45 +00002288void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002289 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002290 compile_node(comp, pns->nodes[0]);
2291 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002292 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002293 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002294 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002295 }
2296 for (int i = 1; i + 1 < num_nodes; i += 2) {
2297 compile_node(comp, pns->nodes[i + 1]);
2298 if (i + 2 < num_nodes) {
2299 EMIT(dup_top);
2300 EMIT(rot_three);
2301 }
Damien George7e5fb242014-02-01 22:18:47 +00002302 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002303 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002304 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002305 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2306 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2307 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2308 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2309 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2310 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2311 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2312 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002313 }
2314 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002315 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2316 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2317 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002318 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002319 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002320 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002321 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002322 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002323 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002324 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002325 }
2326 } else {
2327 // shouldn't happen
2328 assert(0);
2329 }
2330 } else {
2331 // shouldn't happen
2332 assert(0);
2333 }
2334 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002335 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002336 }
2337 }
2338 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002339 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002340 EMIT_ARG(jump, l_end);
2341 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002342 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002343 EMIT(rot_two);
2344 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002345 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002346 }
2347}
2348
Damiend99b0522013-12-21 18:17:45 +00002349void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002350 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002351}
2352
Damiend99b0522013-12-21 18:17:45 +00002353void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002354 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002355}
2356
Damiend99b0522013-12-21 18:17:45 +00002357void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002358 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002359}
2360
Damiend99b0522013-12-21 18:17:45 +00002361void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002362 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002363}
2364
Damiend99b0522013-12-21 18:17:45 +00002365void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2366 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002367 compile_node(comp, pns->nodes[0]);
2368 for (int i = 1; i + 1 < num_nodes; i += 2) {
2369 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002370 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002371 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002372 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002373 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002374 } else {
2375 // shouldn't happen
2376 assert(0);
2377 }
2378 }
2379}
2380
Damiend99b0522013-12-21 18:17:45 +00002381void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2382 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002383 compile_node(comp, pns->nodes[0]);
2384 for (int i = 1; i + 1 < num_nodes; i += 2) {
2385 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002386 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002387 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002388 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002389 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002390 } else {
2391 // shouldn't happen
2392 assert(0);
2393 }
2394 }
2395}
2396
Damiend99b0522013-12-21 18:17:45 +00002397void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2398 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002399 compile_node(comp, pns->nodes[0]);
2400 for (int i = 1; i + 1 < num_nodes; i += 2) {
2401 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002402 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002403 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002404 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002405 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002406 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002407 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002408 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002409 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002410 } else {
2411 // shouldn't happen
2412 assert(0);
2413 }
2414 }
2415}
2416
Damiend99b0522013-12-21 18:17:45 +00002417void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002418 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002419 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002420 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002421 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002422 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002423 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002424 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002425 } else {
2426 // shouldn't happen
2427 assert(0);
2428 }
2429}
2430
Damien George35e2a4e2014-02-05 00:51:47 +00002431void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2432 // this is to handle special super() call
2433 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2434
2435 compile_generic_all_nodes(comp, pns);
2436}
2437
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002438STATIC 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 +01002439 // function to call is on top of stack
2440
Damien George35e2a4e2014-02-05 00:51:47 +00002441#if !MICROPY_EMIT_CPYTHON
2442 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002443 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 +00002444 EMIT_ARG(load_id, MP_QSTR___class__);
2445 // get first argument to function
2446 bool found = false;
2447 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002448 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2449 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 +00002450 found = true;
2451 break;
2452 }
2453 }
2454 if (!found) {
2455 printf("TypeError: super() call cannot find self\n");
2456 return;
2457 }
Damien George922ddd62014-04-09 12:43:17 +01002458 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002459 return;
2460 }
2461#endif
2462
Damien George2c0842b2014-04-27 16:46:51 +01002463 // get the list of arguments
2464 mp_parse_node_t *args;
2465 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002466
Damien George2c0842b2014-04-27 16:46:51 +01002467 // compile the arguments
2468 // Rather than calling compile_node on the list, we go through the list of args
2469 // explicitly here so that we can count the number of arguments and give sensible
2470 // error messages.
2471 int n_positional = n_positional_extra;
2472 uint n_keyword = 0;
2473 uint star_flags = 0;
2474 for (int i = 0; i < n_args; i++) {
2475 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2476 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2477 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2478 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2479 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2480 return;
2481 }
2482 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2483 compile_node(comp, pns_arg->nodes[0]);
2484 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2485 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2486 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2487 return;
2488 }
2489 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2490 compile_node(comp, pns_arg->nodes[0]);
2491 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2492 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2493 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2494 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2495 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2496 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2497 return;
2498 }
Damien George968bf342014-04-27 19:12:05 +01002499 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002500 compile_node(comp, pns2->nodes[0]);
2501 n_keyword += 1;
2502 } else {
2503 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2504 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2505 n_positional++;
2506 }
2507 } else {
2508 goto normal_argument;
2509 }
2510 } else {
2511 normal_argument:
2512 if (n_keyword > 0) {
2513 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2514 return;
2515 }
2516 compile_node(comp, args[i]);
2517 n_positional++;
2518 }
Damien429d7192013-10-04 19:53:11 +01002519 }
2520
Damien George2c0842b2014-04-27 16:46:51 +01002521 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002522 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002523 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002524 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002525 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002526 }
Damien429d7192013-10-04 19:53:11 +01002527}
2528
Damiend99b0522013-12-21 18:17:45 +00002529void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2530 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002531 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002532 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 +01002533 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002534 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2535 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002536 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002537 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002538 i += 1;
2539 } else {
2540 compile_node(comp, pns->nodes[i]);
2541 }
Damien George35e2a4e2014-02-05 00:51:47 +00002542 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002543 }
2544}
2545
Damiend99b0522013-12-21 18:17:45 +00002546void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002547 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002548 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002549}
2550
Damiend99b0522013-12-21 18:17:45 +00002551void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002552 // a list of strings
Damien63321742013-12-10 17:41:49 +00002553
2554 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002555 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002556 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002557 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002558 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002559 int pn_kind;
2560 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2561 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2562 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2563 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2564 } else {
2565 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2566 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2567 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2568 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002569 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002570 }
Damien63321742013-12-10 17:41:49 +00002571 if (i == 0) {
2572 string_kind = pn_kind;
2573 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002574 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002575 return;
2576 }
Damien429d7192013-10-04 19:53:11 +01002577 }
Damien63321742013-12-10 17:41:49 +00002578
Damien63321742013-12-10 17:41:49 +00002579 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002580 byte *q_ptr;
2581 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002582 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002583 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002584 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002585 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2586 memcpy(s_dest, s, s_len);
2587 s_dest += s_len;
2588 } else {
2589 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002590 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2591 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002592 }
Damien63321742013-12-10 17:41:49 +00002593 }
Damien George55baff42014-01-21 21:40:13 +00002594 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002595
Damien Georgeb9791222014-01-23 00:34:21 +00002596 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002597}
2598
2599// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002600STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002601 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2602 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2603 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002604
Damien George36db6bc2014-05-07 17:24:22 +01002605 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002606 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002607 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 +01002608 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002609 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002610 }
2611
2612 // get the scope for this comprehension
2613 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2614
2615 // compile the comprehension
2616 close_over_variables_etc(comp, this_scope, 0, 0);
2617
2618 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2619 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002620 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002621}
2622
Damiend99b0522013-12-21 18:17:45 +00002623void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2624 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002625 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002626 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2627 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2628 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2629 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2630 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2631 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2632 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002633 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002634 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002635 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002636 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002637 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002638 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002639 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002640 // generator expression
2641 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2642 } else {
2643 // tuple with 2 items
2644 goto tuple_with_2_items;
2645 }
2646 } else {
2647 // tuple with 2 items
2648 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002649 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002650 }
2651 } else {
2652 // parenthesis around a single item, is just that item
2653 compile_node(comp, pns->nodes[0]);
2654 }
2655}
2656
Damiend99b0522013-12-21 18:17:45 +00002657void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2658 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002659 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002660 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002661 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2662 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2663 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2664 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2665 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002666 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002667 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002668 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002669 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002670 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002671 // list of many items
2672 compile_node(comp, pns2->nodes[0]);
2673 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002674 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002675 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002676 // list comprehension
2677 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2678 } else {
2679 // list with 2 items
2680 goto list_with_2_items;
2681 }
2682 } else {
2683 // list with 2 items
2684 list_with_2_items:
2685 compile_node(comp, pns2->nodes[0]);
2686 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002687 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002688 }
2689 } else {
2690 // list with 1 item
2691 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002692 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002693 }
2694}
2695
Damiend99b0522013-12-21 18:17:45 +00002696void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2697 mp_parse_node_t pn = pns->nodes[0];
2698 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002699 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002700 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002701 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2702 pns = (mp_parse_node_struct_t*)pn;
2703 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002704 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002705 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002706 compile_node(comp, pn);
2707 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002708 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2709 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2710 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2711 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002712 // dict/set with multiple elements
2713
2714 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002715 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002716 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2717
2718 // first element sets whether it's a dict or set
2719 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002720 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002721 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002722 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002723 compile_node(comp, pns->nodes[0]);
2724 EMIT(store_map);
2725 is_dict = true;
2726 } else {
2727 // a set
2728 compile_node(comp, pns->nodes[0]); // 1st value of set
2729 is_dict = false;
2730 }
2731
2732 // process rest of elements
2733 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002734 mp_parse_node_t pn = nodes[i];
2735 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002736 compile_node(comp, pn);
2737 if (is_dict) {
2738 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002739 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002740 return;
2741 }
2742 EMIT(store_map);
2743 } else {
2744 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002745 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002746 return;
2747 }
2748 }
2749 }
2750
2751 // if it's a set, build it
2752 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002753 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002754 }
Damiend99b0522013-12-21 18:17:45 +00002755 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002756 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002757 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002758 // a dictionary comprehension
2759 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2760 } else {
2761 // a set comprehension
2762 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2763 }
2764 } else {
2765 // shouldn't happen
2766 assert(0);
2767 }
2768 } else {
2769 // set with one element
2770 goto set_with_one_element;
2771 }
2772 } else {
2773 // set with one element
2774 set_with_one_element:
2775 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002776 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002777 }
2778}
2779
Damiend99b0522013-12-21 18:17:45 +00002780void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002781 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002782}
2783
Damiend99b0522013-12-21 18:17:45 +00002784void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002785 // object who's index we want is on top of stack
2786 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002787 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002788}
2789
Damiend99b0522013-12-21 18:17:45 +00002790void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002791 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002792 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002793}
2794
Damiend99b0522013-12-21 18:17:45 +00002795void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2796 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2797 mp_parse_node_t pn = pns->nodes[0];
2798 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002799 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002800 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2801 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002802 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2803 pns = (mp_parse_node_struct_t*)pn;
2804 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002805 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002806 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002807 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002808 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002809 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002810 } else {
2811 // [?::x]
2812 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002813 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002814 }
Damiend99b0522013-12-21 18:17:45 +00002815 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002816 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002817 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2818 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2819 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2820 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002821 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002822 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002823 } else {
2824 // [?:x:x]
2825 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002826 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002827 }
2828 } else {
2829 // [?:x]
2830 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002831 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002832 }
2833 } else {
2834 // [?:x]
2835 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002836 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002837 }
2838}
2839
Damiend99b0522013-12-21 18:17:45 +00002840void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002841 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002842 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2843 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002844}
2845
Damiend99b0522013-12-21 18:17:45 +00002846void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002847 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002848 compile_subscript_3_helper(comp, pns);
2849}
2850
Damiend99b0522013-12-21 18:17:45 +00002851void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002852 // if this is called then we are compiling a dict key:value pair
2853 compile_node(comp, pns->nodes[1]); // value
2854 compile_node(comp, pns->nodes[0]); // key
2855}
2856
Damiend99b0522013-12-21 18:17:45 +00002857void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002858 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002859 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002861}
2862
Damiend99b0522013-12-21 18:17:45 +00002863void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002864 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002865 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002866 return;
2867 }
Damiend99b0522013-12-21 18:17:45 +00002868 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002869 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002870 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002871 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2872 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002873 compile_node(comp, pns->nodes[0]);
2874 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002875 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002876 EMIT(yield_from);
2877 } else {
2878 compile_node(comp, pns->nodes[0]);
2879 EMIT(yield_value);
2880 }
2881}
2882
Damiend99b0522013-12-21 18:17:45 +00002883typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002884STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002885 NULL,
2886#define nc NULL
2887#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002888#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002889#include "grammar.h"
2890#undef nc
2891#undef c
2892#undef DEF_RULE
2893};
2894
Damien George6be0b0a2014-08-15 14:30:52 +01002895STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002896 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002897 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002898 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002899 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002900 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002901 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002902 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002903 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002904 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002905 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2906 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2907 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2908 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002909 case MP_PARSE_NODE_TOKEN:
2910 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002911 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002912 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002913 // do nothing
2914 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002915 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002916 }
2917 break;
Damien429d7192013-10-04 19:53:11 +01002918 default: assert(0);
2919 }
2920 } else {
Damiend99b0522013-12-21 18:17:45 +00002921 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002922 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002923 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002924 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 +01002925 } else {
Damien George5042bce2014-05-25 22:06:06 +01002926 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2927 if (f == NULL) {
2928 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2929#if MICROPY_DEBUG_PRINTERS
2930 mp_parse_node_print(pn, 0);
2931#endif
2932 compile_syntax_error(comp, pn, "internal compiler error");
2933 } else {
2934 f(comp, pns);
2935 }
Damien429d7192013-10-04 19:53:11 +01002936 }
2937 }
2938}
2939
Damien George2ac4af62014-08-15 16:45:41 +01002940STATIC 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 +01002941 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002942 qstr param_name = MP_QSTR_NULL;
2943 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002944 if (MP_PARSE_NODE_IS_ID(pn)) {
2945 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002946 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002947 // comes after a star, so counts as a keyword-only parameter
2948 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002949 } else {
Damien George2827d622014-04-27 15:50:52 +01002950 // comes before a star, so counts as a positional parameter
2951 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002952 }
Damienb14de212013-10-06 00:28:28 +01002953 } else {
Damiend99b0522013-12-21 18:17:45 +00002954 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2955 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2956 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2957 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002958 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002959 // comes after a star, so counts as a keyword-only parameter
2960 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002961 } else {
Damien George2827d622014-04-27 15:50:52 +01002962 // comes before a star, so counts as a positional parameter
2963 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002964 }
Damiend99b0522013-12-21 18:17:45 +00002965 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002966 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002967 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002968 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002969 // bare star
2970 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002971 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002972 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002973 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002974 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002975 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01002976 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002977 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002978 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002979 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2980 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002981 } else {
2982 // shouldn't happen
2983 assert(0);
2984 }
Damiend99b0522013-12-21 18:17:45 +00002985 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2986 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002987 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002988 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002989 } else {
Damienb14de212013-10-06 00:28:28 +01002990 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002991 assert(0);
2992 }
Damien429d7192013-10-04 19:53:11 +01002993 }
2994
Damien George2827d622014-04-27 15:50:52 +01002995 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002996 bool added;
2997 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2998 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002999 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003000 return;
3001 }
Damien429d7192013-10-04 19:53:11 +01003002 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003003 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003004 }
3005}
3006
Damien George2827d622014-04-27 15:50:52 +01003007STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003008 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003009}
3010
Damien George2827d622014-04-27 15:50:52 +01003011STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003012 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3013}
3014
3015STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3016 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3017 // no annotation
3018 return;
3019 }
3020
3021 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3022 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3023 // named parameter with possible annotation
3024 // fallthrough
3025 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3026 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3027 // named star with possible annotation
3028 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3029 // fallthrough
3030 } else {
3031 // no annotation
3032 return;
3033 }
3034 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3035 // double star with possible annotation
3036 // fallthrough
3037 } else {
3038 // no annotation
3039 return;
3040 }
3041
3042 mp_parse_node_t pn_annotation = pns->nodes[1];
3043
3044 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3045 #if MICROPY_EMIT_NATIVE
3046 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3047 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3048 assert(id_info != NULL);
3049
3050 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3051 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3052 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3053 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3054 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003055 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003056 }
3057 }
3058 #endif // MICROPY_EMIT_NATIVE
3059 }
Damien429d7192013-10-04 19:53:11 +01003060}
3061
Damien George6be0b0a2014-08-15 14:30:52 +01003062STATIC 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 +01003063 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003064 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003065 // no more nested if/for; compile inner expression
3066 compile_node(comp, pn_inner_expr);
3067 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003068 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003069 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003070 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003071 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003072 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003073 } else {
3074 EMIT(yield_value);
3075 EMIT(pop_top);
3076 }
Damiend99b0522013-12-21 18:17:45 +00003077 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003078 // if condition
Damiend99b0522013-12-21 18:17:45 +00003079 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003080 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3081 pn_iter = pns_comp_if->nodes[1];
3082 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003083 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003084 // for loop
Damiend99b0522013-12-21 18:17:45 +00003085 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003086 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003087 uint l_end2 = comp_next_label(comp);
3088 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003089 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003090 EMIT_ARG(label_assign, l_top2);
3091 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003092 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3093 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 +00003094 EMIT_ARG(jump, l_top2);
3095 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003096 EMIT(for_iter_end);
3097 } else {
3098 // shouldn't happen
3099 assert(0);
3100 }
3101}
3102
Damien George1463c1f2014-04-25 23:52:57 +01003103STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3104#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003105 // see http://www.python.org/dev/peps/pep-0257/
3106
3107 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003108 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003109 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003110 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003111 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003112 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3113 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003114 for (int i = 0; i < num_nodes; i++) {
3115 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003116 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 +00003117 // not a newline, so this is the first statement; finish search
3118 break;
3119 }
3120 }
3121 // 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 +00003122 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003123 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003124 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003125 } else {
3126 return;
3127 }
3128
3129 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003130 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3131 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003132 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3133 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3134 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3135 // compile the doc string
3136 compile_node(comp, pns->nodes[0]);
3137 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003138 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003139 }
3140 }
Damien George1463c1f2014-04-25 23:52:57 +01003141#endif
Damien429d7192013-10-04 19:53:11 +01003142}
3143
Damien George1463c1f2014-04-25 23:52:57 +01003144STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003145 comp->pass = pass;
3146 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003147 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003148 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003149
Damien George36db6bc2014-05-07 17:24:22 +01003150 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003151 // reset maximum stack sizes in scope
3152 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003153 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003154 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003155 }
3156
Damien5ac1b2e2013-10-18 19:58:12 +01003157#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003158 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003159 scope_print_info(scope);
3160 }
Damien5ac1b2e2013-10-18 19:58:12 +01003161#endif
Damien429d7192013-10-04 19:53:11 +01003162
3163 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003164 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3165 assert(scope->kind == SCOPE_MODULE);
3166 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3167 compile_node(comp, pns->nodes[0]); // compile the expression
3168 EMIT(return_value);
3169 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003170 if (!comp->is_repl) {
3171 check_for_doc_string(comp, scope->pn);
3172 }
Damien429d7192013-10-04 19:53:11 +01003173 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003174 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003175 EMIT(return_value);
3176 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003177 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3178 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3179 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003180
3181 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003182 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003183 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003184 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003185 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003186 } else {
3187 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003188
Damien George2ac4af62014-08-15 16:45:41 +01003189 // argument annotations
3190 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3191
3192 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003193 mp_parse_node_t pn_annotation = pns->nodes[2];
3194 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3195 #if MICROPY_EMIT_NATIVE
3196 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3197 // nodes[2] can be null or a test-expr
3198 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3199 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3200 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3201 } else {
3202 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3203 }
Damien George2ac4af62014-08-15 16:45:41 +01003204 }
Damien Georgea5190a72014-08-15 22:39:08 +01003205 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003206 }
Damien George2ac4af62014-08-15 16:45:41 +01003207 }
Damien429d7192013-10-04 19:53:11 +01003208
3209 compile_node(comp, pns->nodes[3]); // 3 is function body
3210 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003211 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003212 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003213 EMIT(return_value);
3214 }
3215 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003216 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3217 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3218 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003219
3220 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003221 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003222 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003223 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003224 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3225 }
3226
3227 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003228
3229 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3230 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3231 EMIT(pop_top);
3232 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3233 }
Damien429d7192013-10-04 19:53:11 +01003234 EMIT(return_value);
3235 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3236 // a bit of a hack at the moment
3237
Damiend99b0522013-12-21 18:17:45 +00003238 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3239 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3240 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3241 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3242 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003243
Damien George708c0732014-04-27 19:23:46 +01003244 // We need a unique name for the comprehension argument (the iterator).
3245 // CPython uses .0, but we should be able to use anything that won't
3246 // clash with a user defined variable. Best to use an existing qstr,
3247 // so we use the blank qstr.
3248#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003249 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003250#else
3251 qstr qstr_arg = MP_QSTR_;
3252#endif
Damien George36db6bc2014-05-07 17:24:22 +01003253 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003254 bool added;
3255 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3256 assert(added);
3257 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003258 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003259 }
3260
3261 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003262 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003263 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003264 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003265 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003266 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003267 }
3268
Damien George6f355fd2014-04-10 14:11:31 +01003269 uint l_end = comp_next_label(comp);
3270 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003271 EMIT_ARG(load_id, qstr_arg);
3272 EMIT_ARG(label_assign, l_top);
3273 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003274 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3275 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003276 EMIT_ARG(jump, l_top);
3277 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003278 EMIT(for_iter_end);
3279
3280 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003281 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003282 }
3283 EMIT(return_value);
3284 } else {
3285 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003286 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3287 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3288 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003289
Damien George36db6bc2014-05-07 17:24:22 +01003290 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003291 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003292 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003293 assert(added);
3294 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003295 }
3296
Damien Georgeb9791222014-01-23 00:34:21 +00003297 EMIT_ARG(load_id, MP_QSTR___name__);
3298 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003299 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 +00003300 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003301
3302 check_for_doc_string(comp, pns->nodes[2]);
3303 compile_node(comp, pns->nodes[2]); // 2 is class body
3304
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003305 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003306 assert(id != NULL);
3307 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003308 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003309 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003310#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003311 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003312#else
Damien George2bf7c092014-04-09 15:26:46 +01003313 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003314#endif
Damien429d7192013-10-04 19:53:11 +01003315 }
3316 EMIT(return_value);
3317 }
3318
Damien415eb6f2013-10-05 12:19:06 +01003319 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003320
3321 // make sure we match all the exception levels
3322 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003323}
3324
Damien George094d4502014-04-02 17:31:27 +01003325#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003326// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003327STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003328 comp->pass = pass;
3329 comp->scope_cur = scope;
3330 comp->next_label = 1;
3331
3332 if (scope->kind != SCOPE_FUNCTION) {
3333 printf("Error: inline assembler must be a function\n");
3334 return;
3335 }
3336
Damien George36db6bc2014-05-07 17:24:22 +01003337 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003338 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003339 }
3340
Damien826005c2013-10-05 23:17:28 +01003341 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003342 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3343 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3344 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003345
Damiend99b0522013-12-21 18:17:45 +00003346 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003347
Damiena2f2f7d2013-10-06 00:14:13 +01003348 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003349 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003350 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003351 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003352 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003353 }
3354
Damiend99b0522013-12-21 18:17:45 +00003355 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003356
Damiend99b0522013-12-21 18:17:45 +00003357 mp_parse_node_t pn_body = pns->nodes[3]; // body
3358 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003359 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3360
Damien Georgecbd2f742014-01-19 11:48:48 +00003361 /*
Damien George36db6bc2014-05-07 17:24:22 +01003362 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003363 //printf("----\n");
3364 scope_print_info(scope);
3365 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003366 */
Damien826005c2013-10-05 23:17:28 +01003367
3368 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003369 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3370 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003371 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3372 // no instructions
3373 continue;
3374 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3375 // an instruction; fall through
3376 } else {
3377 // not an instruction; error
3378 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3379 return;
3380 }
Damiend99b0522013-12-21 18:17:45 +00003381 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3382 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3383 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3384 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3385 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3386 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3387 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3388 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3389 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3390 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003391 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3392
3393 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003394 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003395 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003396 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003397 return;
3398 }
Damien George6f355fd2014-04-10 14:11:31 +01003399 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003400 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003401 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003402 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003403 } else if (op == MP_QSTR_align) {
3404 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3405 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3406 return;
3407 }
Damien George36db6bc2014-05-07 17:24:22 +01003408 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003409 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3410 }
3411 } else if (op == MP_QSTR_data) {
3412 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3413 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3414 return;
3415 }
Damien George36db6bc2014-05-07 17:24:22 +01003416 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003417 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003418 for (uint i = 1; i < n_args; i++) {
3419 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3420 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3421 return;
3422 }
3423 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3424 }
3425 }
Damien826005c2013-10-05 23:17:28 +01003426 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003427 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003428 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003429 }
3430 }
3431 }
3432
Damien George36db6bc2014-05-07 17:24:22 +01003433 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003434 bool success = EMIT_INLINE_ASM(end_pass);
3435 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003436 // TODO get proper exception from inline assembler
3437 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003438 }
Damienb05d7072013-10-05 13:37:10 +01003439 }
Damien429d7192013-10-04 19:53:11 +01003440}
Damien George094d4502014-04-02 17:31:27 +01003441#endif
Damien429d7192013-10-04 19:53:11 +01003442
Damien George1463c1f2014-04-25 23:52:57 +01003443STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003444#if !MICROPY_EMIT_CPYTHON
3445 // in Micro Python we put the *x parameter after all other parameters (except **y)
3446 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3447 id_info_t *id_param = NULL;
3448 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3449 id_info_t *id = &scope->id_info[i];
3450 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3451 if (id_param != NULL) {
3452 // swap star param with last param
3453 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3454 }
3455 break;
3456 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3457 id_param = id;
3458 }
3459 }
3460 }
3461#endif
3462
Damien429d7192013-10-04 19:53:11 +01003463 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003464 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003465 scope->num_locals = 0;
3466 for (int i = 0; i < scope->id_info_len; i++) {
3467 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003468 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003469 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3470 continue;
3471 }
3472 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3473 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3474 }
Damien George2827d622014-04-27 15:50:52 +01003475 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003476 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003477 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003478 }
3479 }
3480
3481 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003482#if MICROPY_EMIT_CPYTHON
3483 int num_cell = 0;
3484#endif
Damien9ecbcff2013-12-11 00:41:43 +00003485 for (int i = 0; i < scope->id_info_len; i++) {
3486 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003487#if MICROPY_EMIT_CPYTHON
3488 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003489 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003490 id->local_num = num_cell;
3491 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003492 }
Damien George6baf76e2013-12-30 22:32:17 +00003493#else
3494 // in Micro Python the cells come right after the fast locals
3495 // parameters are not counted here, since they remain at the start
3496 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003497 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003498 id->local_num = scope->num_locals;
3499 scope->num_locals += 1;
3500 }
3501#endif
Damien9ecbcff2013-12-11 00:41:43 +00003502 }
Damien9ecbcff2013-12-11 00:41:43 +00003503
3504 // compute the index of free vars (freevars[idx] in CPython)
3505 // make sure they are in the order of the parent scope
3506 if (scope->parent != NULL) {
3507 int num_free = 0;
3508 for (int i = 0; i < scope->parent->id_info_len; i++) {
3509 id_info_t *id = &scope->parent->id_info[i];
3510 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3511 for (int j = 0; j < scope->id_info_len; j++) {
3512 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003513 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003514 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003515#if MICROPY_EMIT_CPYTHON
3516 // in CPython the frees are numbered after the cells
3517 id2->local_num = num_cell + num_free;
3518#else
3519 // in Micro Python the frees come first, before the params
3520 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003521#endif
3522 num_free += 1;
3523 }
3524 }
3525 }
Damien429d7192013-10-04 19:53:11 +01003526 }
Damien George6baf76e2013-12-30 22:32:17 +00003527#if !MICROPY_EMIT_CPYTHON
3528 // in Micro Python shift all other locals after the free locals
3529 if (num_free > 0) {
3530 for (int i = 0; i < scope->id_info_len; i++) {
3531 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003532 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003533 id->local_num += num_free;
3534 }
3535 }
Damien George2827d622014-04-27 15:50:52 +01003536 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 +00003537 scope->num_locals += num_free;
3538 }
3539#endif
Damien429d7192013-10-04 19:53:11 +01003540 }
3541
Damien George8725f8f2014-02-15 19:33:11 +00003542 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003543
3544#if MICROPY_EMIT_CPYTHON
3545 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003546 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) {
3547 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003548 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003549 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003550 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 +00003551 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003552 }
3553 }
Damien George882b3632014-04-02 15:56:31 +01003554#endif
3555
Damien429d7192013-10-04 19:53:11 +01003556 int num_free = 0;
3557 for (int i = 0; i < scope->id_info_len; i++) {
3558 id_info_t *id = &scope->id_info[i];
3559 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3560 num_free += 1;
3561 }
3562 }
3563 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003564 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003565 }
3566}
3567
Damien George65cad122014-04-06 11:48:15 +01003568mp_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 +01003569 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003570 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003571 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003572 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003573
Damien826005c2013-10-05 23:17:28 +01003574 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003575 mp_map_t consts;
3576 mp_map_init(&consts, 0);
3577 pn = fold_constants(comp, pn, &consts);
3578 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003579
3580 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003581 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003582
Damien826005c2013-10-05 23:17:28 +01003583 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003584 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003585 comp->emit_method_table = &emit_pass1_method_table;
3586 comp->emit_inline_asm = NULL;
3587 comp->emit_inline_asm_method_table = NULL;
3588 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003589 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003590 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003591#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003592 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003593 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003594#endif
Damien826005c2013-10-05 23:17:28 +01003595 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003596 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003597 }
3598
3599 // update maximim number of labels needed
3600 if (comp->next_label > max_num_labels) {
3601 max_num_labels = comp->next_label;
3602 }
Damien429d7192013-10-04 19:53:11 +01003603 }
3604
Damien826005c2013-10-05 23:17:28 +01003605 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003606 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003607 compile_scope_compute_things(comp, s);
3608 }
3609
Damien826005c2013-10-05 23:17:28 +01003610 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003611 emit_pass1_free(comp->emit);
3612
Damien826005c2013-10-05 23:17:28 +01003613 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003614#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003615 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003616#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003617 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003618#endif
Damien3ef4abb2013-10-12 16:53:13 +01003619#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003620 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003621#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003622#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003623 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003624 if (false) {
3625 // dummy
3626
Damien3ef4abb2013-10-12 16:53:13 +01003627#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003628 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003629 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003630 if (emit_inline_thumb == NULL) {
3631 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3632 }
3633 comp->emit = NULL;
3634 comp->emit_method_table = NULL;
3635 comp->emit_inline_asm = emit_inline_thumb;
3636 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003637 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003638 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003639 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003640 }
Damienc025ebb2013-10-12 14:30:21 +01003641#endif
3642
Damien826005c2013-10-05 23:17:28 +01003643 } else {
Damienc025ebb2013-10-12 14:30:21 +01003644
3645 // choose the emit type
3646
Damien3ef4abb2013-10-12 16:53:13 +01003647#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003648 comp->emit = emit_cpython_new(max_num_labels);
3649 comp->emit_method_table = &emit_cpython_method_table;
3650#else
Damien826005c2013-10-05 23:17:28 +01003651 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003652
3653#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003654 case MP_EMIT_OPT_NATIVE_PYTHON:
3655 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003656#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003657 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003658 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003659 }
Damien13ed3a62013-10-08 09:05:10 +01003660 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003661#elif MICROPY_EMIT_X86
3662 if (emit_native == NULL) {
3663 emit_native = emit_native_x86_new(max_num_labels);
3664 }
3665 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003666#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003667 if (emit_native == NULL) {
3668 emit_native = emit_native_thumb_new(max_num_labels);
3669 }
3670 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003671#elif MICROPY_EMIT_ARM
3672 if (emit_native == NULL) {
3673 emit_native = emit_native_arm_new(max_num_labels);
3674 }
3675 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003676#endif
3677 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003678 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 +01003679
3680 // native emitters need an extra pass to compute stack size
3681 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3682
Damien7af3d192013-10-07 00:02:49 +01003683 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003684#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003685
Damien826005c2013-10-05 23:17:28 +01003686 default:
3687 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003688 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003689 }
3690 comp->emit = emit_bc;
3691 comp->emit_method_table = &emit_bc_method_table;
3692 break;
3693 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003694#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003695
Damien George36db6bc2014-05-07 17:24:22 +01003696 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003697 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003698 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3699 }
3700
3701 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003702 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003703 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003704 }
Damien6cdd3af2013-10-05 18:08:26 +01003705 }
Damien429d7192013-10-04 19:53:11 +01003706 }
3707
Damien George41d02b62014-01-24 22:42:28 +00003708 // free the emitters
3709#if !MICROPY_EMIT_CPYTHON
3710 if (emit_bc != NULL) {
3711 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003712 }
Damien George41d02b62014-01-24 22:42:28 +00003713#if MICROPY_EMIT_NATIVE
3714 if (emit_native != NULL) {
3715#if MICROPY_EMIT_X64
3716 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003717#elif MICROPY_EMIT_X86
3718 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003719#elif MICROPY_EMIT_THUMB
3720 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003721#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003722 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003723#endif
3724 }
3725#endif
3726#if MICROPY_EMIT_INLINE_THUMB
3727 if (emit_inline_thumb != NULL) {
3728 emit_inline_thumb_free(emit_inline_thumb);
3729 }
3730#endif
3731#endif // !MICROPY_EMIT_CPYTHON
3732
Damien George52b5d762014-09-23 15:31:56 +00003733 // free the parse tree
3734 mp_parse_node_free(pn);
3735
Damien George41d02b62014-01-24 22:42:28 +00003736 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003737 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003738 for (scope_t *s = module_scope; s;) {
3739 scope_t *next = s->next;
3740 scope_free(s);
3741 s = next;
3742 }
Damien5ac1b2e2013-10-18 19:58:12 +01003743
Damien George41d02b62014-01-24 22:42:28 +00003744 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003745 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003746 m_del_obj(compiler_t, comp);
3747
Damien Georgea91ac202014-10-05 19:01:34 +01003748 if (compile_error != MP_OBJ_NULL) {
3749 return compile_error;
Damien George1fb03172014-01-03 14:22:03 +00003750 } else {
3751#if MICROPY_EMIT_CPYTHON
3752 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003753 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003754 return mp_const_true;
3755#else
3756 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003757 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003758#endif
3759 }
Damien429d7192013-10-04 19:53:11 +01003760}