blob: 2c44fc452cbe2c04f66c7fcfe6942d54727ca0cc [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
Damiend99b0522013-12-21 18:17:45 +000034#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030035#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010037#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010038#include "parse.h"
Damiend99b0522013-12-21 18:17:45 +000039#include "runtime0.h"
Damien George1fb03172014-01-03 14:22:03 +000040#include "obj.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010041#include "emitglue.h"
42#include "scope.h"
43#include "emit.h"
Damien George1fb03172014-01-03 14:22:03 +000044#include "compile.h"
45#include "runtime.h"
Damien Georgea26dc502014-04-12 17:54:52 +010046#include "builtin.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000047#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010048
49// TODO need to mangle __attr names
50
51typedef enum {
52 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000053#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010054#include "grammar.h"
55#undef DEF_RULE
56 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010057 PN_string, // special node for non-interned string
Damien429d7192013-10-04 19:53:11 +010058} pn_kind_t;
59
Damien Georgeb9791222014-01-23 00:34:21 +000060#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
61#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
62#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
63#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010064
Damien Georgea91ac202014-10-05 19:01:34 +010065// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010066typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000067 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010068
Damien George78035b92014-04-09 12:27:39 +010069 uint8_t is_repl;
70 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010071 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010072 uint8_t have_star;
73
74 // try to keep compiler clean from nlr
75 // this is set to an exception object if we have a compile error
76 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010077
Damien George6f355fd2014-04-10 14:11:31 +010078 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010079
Damien George69b89d22014-04-11 13:38:30 +000080 uint16_t num_dict_params;
81 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010082
Damien Georgea91ac202014-10-05 19:01:34 +010083 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
84 uint16_t continue_label;
85 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000086 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010087
Damien429d7192013-10-04 19:53:11 +010088 scope_t *scope_head;
89 scope_t *scope_cur;
90
Damien6cdd3af2013-10-05 18:08:26 +010091 emit_t *emit; // current emitter
92 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010093
94 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
95 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010096} compiler_t;
97
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010098STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010099 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
100 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100101 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien Georgea91ac202014-10-05 19:01:34 +0100102 mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100103 } else {
Damien Georgea91ac202014-10-05 19:01:34 +0100104 // we don't have a line number, so just pass 0
105 mp_obj_exception_add_traceback(exc, comp->source_file, 0, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100106 }
Damien Georgea91ac202014-10-05 19:01:34 +0100107 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000108}
109
Damien George57e99eb2014-04-10 22:42:11 +0100110STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300111 #if MICROPY_PY_UCTYPES
112 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
113 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100114 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100115 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100116};
117
118STATIC const mp_map_t mp_constants_map = {
119 .all_keys_are_qstrs = 1,
120 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200121 .used = MP_ARRAY_SIZE(mp_constants_table),
122 .alloc = MP_ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +0100123 .table = (mp_map_elem_t*)mp_constants_table,
124};
125
Damien Georgeffae48d2014-05-08 15:58:39 +0000126// this function is essentially a simple preprocessor
127STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
128 if (0) {
129 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100130#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000131 } else if (MP_PARSE_NODE_IS_ID(pn)) {
132 // lookup identifier in table of dynamic constants
133 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
134 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
135 if (elem != NULL) {
136 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
137 }
138#endif
139 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000140 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100141
Damien Georgeffae48d2014-05-08 15:58:39 +0000142 // fold some parse nodes before folding their arguments
143 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100144#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000145 case PN_expr_stmt:
146 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
147 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
148 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
149 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
150 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
151 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
152 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
153 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
154 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
155 ) {
156 // code to assign dynamic constants: id = const(value)
157
158 // get the id
159 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
160
161 // get the value
162 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
163 pn_value = fold_constants(comp, pn_value, consts);
164 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
165 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
166 break;
167 }
Damien George40f3c022014-07-03 13:25:24 +0100168 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000169
170 // store the value in the table of dynamic constants
171 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
172 if (elem->value != MP_OBJ_NULL) {
173 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
174 break;
175 }
176 elem->value = MP_OBJ_NEW_SMALL_INT(value);
177
178 // replace const(value) with value
179 pns1->nodes[0] = pn_value;
180
181 // finished folding this assignment
182 return pn;
183 }
184 }
185 }
186 break;
187#endif
Damien George5042bce2014-05-25 22:06:06 +0100188 case PN_string:
189 return pn;
Damien429d7192013-10-04 19:53:11 +0100190 }
191
Damien Georgeffae48d2014-05-08 15:58:39 +0000192 // fold arguments
193 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
194 for (int i = 0; i < n; i++) {
195 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
196 }
197
198 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000199 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100200 case PN_atom_paren:
201 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
202 // (int)
203 pn = pns->nodes[0];
204 }
205 break;
206
207 case PN_expr:
208 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
209 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100210 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
211 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100212 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
213 }
214 break;
215
216 case PN_and_expr:
217 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
218 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100219 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
220 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100221 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
222 }
223 break;
224
Damien429d7192013-10-04 19:53:11 +0100225 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000226 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100227 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
228 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000229 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100230 // int << int
231 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
232 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
233 }
Damiend99b0522013-12-21 18:17:45 +0000234 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100235 // int >> int
Damiend99b0522013-12-21 18:17:45 +0000236 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100237 } else {
238 // shouldn't happen
239 assert(0);
240 }
241 }
242 break;
243
244 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100245 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000246 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100247 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
248 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000249 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100250 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000251 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000252 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100253 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000254 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100255 } else {
256 // shouldn't happen
257 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100258 }
Damien Georged1e355e2014-05-28 14:51:12 +0100259 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100260 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000261 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100262 }
263 }
264 break;
265
266 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000267 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100268 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
269 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000270 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100271 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000272 if (!mp_small_int_mul_overflow(arg0, arg1)) {
273 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100274 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000275 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
276 }
277 }
Damiend99b0522013-12-21 18:17:45 +0000278 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100279 // int / int
280 // pass
Damiend99b0522013-12-21 18:17:45 +0000281 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100282 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000283 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000284 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300285 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100286 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000287 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300288 }
Damien429d7192013-10-04 19:53:11 +0100289 } else {
290 // shouldn't happen
291 assert(0);
292 }
293 }
294 break;
295
296 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000297 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100298 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000299 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100300 // +int
Damiend99b0522013-12-21 18:17:45 +0000301 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
302 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100303 // -int
Damiend99b0522013-12-21 18:17:45 +0000304 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
305 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100306 // ~int
Damiend99b0522013-12-21 18:17:45 +0000307 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100308 } else {
309 // shouldn't happen
310 assert(0);
311 }
312 }
313 break;
314
315 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100316 if (0) {
317#if MICROPY_EMIT_CPYTHON
318 } else if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100319 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100320 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000321 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
322 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200323 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100324 if (power >= 0) {
325 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200326 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100327 for (; power > 0; power--) {
328 ans *= base;
329 }
Damiend99b0522013-12-21 18:17:45 +0000330 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100331 }
332 }
Damien George57e99eb2014-04-10 22:42:11 +0100333#endif
334 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
335 // id.id
336 // look it up in constant table, see if it can be replaced with an integer
337 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
338 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
339 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
340 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
341 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
342 if (elem != NULL) {
343 mp_obj_t dest[2];
344 mp_load_method_maybe(elem->value, q_attr, dest);
345 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100346 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georged1e355e2014-05-28 14:51:12 +0100347 if (MP_SMALL_INT_FITS(val)) {
Damien George57e99eb2014-04-10 22:42:11 +0100348 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
349 }
350 }
351 }
Damien429d7192013-10-04 19:53:11 +0100352 }
353 break;
354 }
355 }
356
357 return pn;
358}
359
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200360STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George6be0b0a2014-08-15 14:30:52 +0100361STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000362STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100363
Damien George6f355fd2014-04-10 14:11:31 +0100364STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100365 return comp->next_label++;
366}
367
Damien George8dcc0c72014-03-27 10:55:21 +0000368STATIC void compile_increase_except_level(compiler_t *comp) {
369 comp->cur_except_level += 1;
370 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
371 comp->scope_cur->exc_stack_size = comp->cur_except_level;
372 }
373}
374
375STATIC void compile_decrease_except_level(compiler_t *comp) {
376 assert(comp->cur_except_level > 0);
377 comp->cur_except_level -= 1;
378}
379
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200380STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgedf8127a2014-04-13 11:04:33 +0100381 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100382 scope->parent = comp->scope_cur;
383 scope->next = NULL;
384 if (comp->scope_head == NULL) {
385 comp->scope_head = scope;
386 } else {
387 scope_t *s = comp->scope_head;
388 while (s->next != NULL) {
389 s = s->next;
390 }
391 s->next = scope;
392 }
393 return scope;
394}
395
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200396STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000397 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
398 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
399 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100400 for (int i = 0; i < num_nodes; i++) {
401 f(comp, pns->nodes[i]);
402 }
Damiend99b0522013-12-21 18:17:45 +0000403 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100404 f(comp, pn);
405 }
406}
407
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200408STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000409 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100410 *nodes = NULL;
411 return 0;
Damiend99b0522013-12-21 18:17:45 +0000412 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100413 *nodes = pn;
414 return 1;
415 } else {
Damiend99b0522013-12-21 18:17:45 +0000416 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
417 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100418 *nodes = pn;
419 return 1;
420 } else {
421 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000422 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100423 }
424 }
425}
426
Damiend99b0522013-12-21 18:17:45 +0000427void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100428}
429
Damiend99b0522013-12-21 18:17:45 +0000430void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
431 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100432 for (int i = 0; i < num_nodes; i++) {
433 compile_node(comp, pns->nodes[i]);
434 }
435}
436
Damien3ef4abb2013-10-12 16:53:13 +0100437#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200438STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100439 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
440 return true;
441 }
Damiend99b0522013-12-21 18:17:45 +0000442 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100443 return false;
444 }
Damiend99b0522013-12-21 18:17:45 +0000445 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100446 return false;
447 }
448 return true;
449}
450
Damien George5042bce2014-05-25 22:06:06 +0100451STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000452 bool has_single_quote = false;
453 bool has_double_quote = false;
454 for (int i = 0; i < len; i++) {
455 if (str[i] == '\'') {
456 has_single_quote = true;
457 } else if (str[i] == '"') {
458 has_double_quote = true;
459 }
460 }
461 if (bytes) {
462 vstr_printf(vstr, "b");
463 }
464 bool quote_single = false;
465 if (has_single_quote && !has_double_quote) {
466 vstr_printf(vstr, "\"");
467 } else {
468 quote_single = true;
469 vstr_printf(vstr, "'");
470 }
471 for (int i = 0; i < len; i++) {
472 if (str[i] == '\n') {
473 vstr_printf(vstr, "\\n");
474 } else if (str[i] == '\\') {
475 vstr_printf(vstr, "\\\\");
476 } else if (str[i] == '\'' && quote_single) {
477 vstr_printf(vstr, "\\'");
478 } else {
479 vstr_printf(vstr, "%c", str[i]);
480 }
481 }
482 if (has_single_quote && !has_double_quote) {
483 vstr_printf(vstr, "\"");
484 } else {
485 vstr_printf(vstr, "'");
486 }
487}
488
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200489STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George5042bce2014-05-25 22:06:06 +0100490 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
491 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George40f3c022014-07-03 13:25:24 +0100492 cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false);
Damien George5042bce2014-05-25 22:06:06 +0100493 return;
494 }
495
Damiend99b0522013-12-21 18:17:45 +0000496 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200497 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
498 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
499 return;
500 }
501
Damien George42f3de92014-10-03 17:44:14 +0000502 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000503 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
504 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000505 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
506 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100507 case MP_PARSE_NODE_STRING:
508 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100509 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100510 const byte *str = qstr_data(arg, &len);
511 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
512 break;
513 }
Damiend99b0522013-12-21 18:17:45 +0000514 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100515 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000516 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
517 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
518 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100519 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100520 }
521 break;
522 default: assert(0);
523 }
524}
525
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200526STATIC void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100527 int n = 0;
528 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000529 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100530 }
531 int total = n;
532 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000533 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100534 total += 1;
Damien3a205172013-10-12 15:01:56 +0100535 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100536 is_const = false;
537 }
538 }
539 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100540 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100541 is_const = false;
542 break;
543 }
544 }
545 if (total > 0 && is_const) {
546 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000547 vstr_t *vstr = vstr_new();
548 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000549 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000550 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100551 need_comma = true;
552 }
553 for (int i = 0; i < n; i++) {
554 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000555 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100556 }
Damien02f89412013-12-12 15:13:36 +0000557 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100558 need_comma = true;
559 }
560 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000561 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100562 } else {
Damien02f89412013-12-12 15:13:36 +0000563 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100564 }
Damien Georgeb9791222014-01-23 00:34:21 +0000565 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000566 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100567 } else {
Damiend99b0522013-12-21 18:17:45 +0000568 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100569 compile_node(comp, pn);
570 }
571 for (int i = 0; i < n; i++) {
572 compile_node(comp, pns_list->nodes[i]);
573 }
Damien Georgeb9791222014-01-23 00:34:21 +0000574 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100575 }
576}
Damien3a205172013-10-12 15:01:56 +0100577#endif
578
579// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100580STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100581#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100582 cpython_c_tuple(comp, pn, pns_list);
583#else
584 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000585 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100586 compile_node(comp, pn);
587 total += 1;
588 }
589 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000590 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100591 for (int i = 0; i < n; i++) {
592 compile_node(comp, pns_list->nodes[i]);
593 }
594 total += n;
595 }
Damien Georgeb9791222014-01-23 00:34:21 +0000596 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100597#endif
598}
Damien429d7192013-10-04 19:53:11 +0100599
Damiend99b0522013-12-21 18:17:45 +0000600void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100601 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000602 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100603}
604
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200605STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000606 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200607 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100608}
609
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200610STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200611 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100612}
613
Damien3ef4abb2013-10-12 16:53:13 +0100614#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100615// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200616STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100617 if (node_is_const_false(pn)) {
618 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000619 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100620 }
621 return;
622 } else if (node_is_const_true(pn)) {
623 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000624 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100625 }
626 return;
Damiend99b0522013-12-21 18:17:45 +0000627 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
628 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
629 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
630 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100631 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100632 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100633 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100634 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100635 }
Damien3a205172013-10-12 15:01:56 +0100636 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000637 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100638 } else {
639 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100640 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100641 }
642 }
643 return;
Damiend99b0522013-12-21 18:17:45 +0000644 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100645 if (jump_if == false) {
646 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100647 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100648 }
649 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100650 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100651 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100652 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100653 }
Damien3a205172013-10-12 15:01:56 +0100654 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000655 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100656 }
657 return;
Damiend99b0522013-12-21 18:17:45 +0000658 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100659 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100660 return;
661 }
662 }
663
664 // nothing special, fall back to default compiling for node and jump
665 compile_node(comp, pn);
666 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000667 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100668 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000669 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100670 }
671}
Damien3a205172013-10-12 15:01:56 +0100672#endif
Damien429d7192013-10-04 19:53:11 +0100673
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200674STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100675#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100676 cpython_c_if_cond(comp, pn, jump_if, label, false);
677#else
678 if (node_is_const_false(pn)) {
679 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000680 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100681 }
682 return;
683 } else if (node_is_const_true(pn)) {
684 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000685 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100686 }
687 return;
Damiend99b0522013-12-21 18:17:45 +0000688 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
689 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
690 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
691 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100692 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100693 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100694 for (int i = 0; i < n - 1; i++) {
695 c_if_cond(comp, pns->nodes[i], true, label2);
696 }
697 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000698 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100699 } else {
700 for (int i = 0; i < n; i++) {
701 c_if_cond(comp, pns->nodes[i], true, label);
702 }
703 }
704 return;
Damiend99b0522013-12-21 18:17:45 +0000705 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100706 if (jump_if == false) {
707 for (int i = 0; i < n; i++) {
708 c_if_cond(comp, pns->nodes[i], false, label);
709 }
710 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100711 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100712 for (int i = 0; i < n - 1; i++) {
713 c_if_cond(comp, pns->nodes[i], false, label2);
714 }
715 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000716 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100717 }
718 return;
Damiend99b0522013-12-21 18:17:45 +0000719 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100720 c_if_cond(comp, pns->nodes[0], !jump_if, label);
721 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100722 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
723 // cond is something in parenthesis
724 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
725 // empty tuple, acts as false for the condition
726 if (jump_if == false) {
727 EMIT_ARG(jump, label);
728 }
729 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
730 // non-empty tuple, acts as true for the condition
731 if (jump_if == true) {
732 EMIT_ARG(jump, label);
733 }
734 } else {
735 // parenthesis around 1 item, is just that item
736 c_if_cond(comp, pns->nodes[0], jump_if, label);
737 }
738 return;
Damien3a205172013-10-12 15:01:56 +0100739 }
740 }
741
742 // nothing special, fall back to default compiling for node and jump
743 compile_node(comp, pn);
744 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000745 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100746 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000747 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100748 }
749#endif
Damien429d7192013-10-04 19:53:11 +0100750}
751
752typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100753STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100754
Damien George6be0b0a2014-08-15 14:30:52 +0100755STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100756 if (assign_kind != ASSIGN_AUG_STORE) {
757 compile_node(comp, pns->nodes[0]);
758 }
759
Damiend99b0522013-12-21 18:17:45 +0000760 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
761 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
762 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
763 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100764 if (assign_kind != ASSIGN_AUG_STORE) {
765 for (int i = 0; i < n - 1; i++) {
766 compile_node(comp, pns1->nodes[i]);
767 }
768 }
Damiend99b0522013-12-21 18:17:45 +0000769 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
770 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100771 }
Damiend99b0522013-12-21 18:17:45 +0000772 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100773 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000774 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100775 if (assign_kind == ASSIGN_AUG_STORE) {
776 EMIT(rot_three);
777 EMIT(store_subscr);
778 } else {
779 compile_node(comp, pns1->nodes[0]);
780 if (assign_kind == ASSIGN_AUG_LOAD) {
781 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100782 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100783 } else {
784 EMIT(store_subscr);
785 }
786 }
Damiend99b0522013-12-21 18:17:45 +0000787 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
788 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100789 if (assign_kind == ASSIGN_AUG_LOAD) {
790 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000791 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100792 } else {
793 if (assign_kind == ASSIGN_AUG_STORE) {
794 EMIT(rot_two);
795 }
Damien Georgeb9791222014-01-23 00:34:21 +0000796 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100797 }
798 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100799 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100800 }
801 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100802 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100803 }
804
Damiend99b0522013-12-21 18:17:45 +0000805 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100806 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100807 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100808
809 return;
810
811cannot_assign:
812 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100813}
814
Damien George0288cf02014-04-11 11:53:00 +0000815// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
Damien George6be0b0a2014-08-15 14:30:52 +0100816STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
Damien George0288cf02014-04-11 11:53:00 +0000817 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
818
819 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100820 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000821 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
822 EMIT_ARG(unpack_ex, 0, num_tail);
823 have_star_index = 0;
824 }
825 for (int i = 0; i < num_tail; i++) {
826 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100827 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000828 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
829 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100830 } else {
Damien George9d181f62014-04-27 16:55:27 +0100831 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100832 return;
833 }
834 }
835 }
836 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000837 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100838 }
Damien George0288cf02014-04-11 11:53:00 +0000839 if (num_head != 0) {
840 if (0 == have_star_index) {
841 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100842 } else {
Damien George0288cf02014-04-11 11:53:00 +0000843 c_assign(comp, node_head, ASSIGN_STORE);
844 }
845 }
846 for (int i = 0; i < num_tail; i++) {
847 if (num_head + i == have_star_index) {
848 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
849 } else {
850 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100851 }
852 }
853}
854
855// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100856STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100857 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000858 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100859 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000860 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
861 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000862 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100863 switch (assign_kind) {
864 case ASSIGN_STORE:
865 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000866 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100867 break;
868 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000869 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100870 break;
871 }
872 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100873 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100874 return;
875 }
876 } else {
Damiend99b0522013-12-21 18:17:45 +0000877 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
878 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100879 case PN_power:
880 // lhs is an index or attribute
881 c_assign_power(comp, pns, assign_kind);
882 break;
883
884 case PN_testlist_star_expr:
885 case PN_exprlist:
886 // lhs is a tuple
887 if (assign_kind != ASSIGN_STORE) {
888 goto bad_aug;
889 }
Damien George0288cf02014-04-11 11:53:00 +0000890 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100891 break;
892
893 case PN_atom_paren:
894 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000895 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100896 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100897 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000898 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
899 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100900 goto testlist_comp;
901 } else {
902 // parenthesis around 1 item, is just that item
903 pn = pns->nodes[0];
904 goto tail_recursion;
905 }
906 break;
907
908 case PN_atom_bracket:
909 // lhs is something in brackets
910 if (assign_kind != ASSIGN_STORE) {
911 goto bad_aug;
912 }
Damiend99b0522013-12-21 18:17:45 +0000913 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100914 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000915 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000916 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
917 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100918 goto testlist_comp;
919 } else {
920 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000921 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100922 }
923 break;
924
925 default:
Damien George9d181f62014-04-27 16:55:27 +0100926 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100927 }
928 return;
929
930 testlist_comp:
931 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000932 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
933 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
934 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100935 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000936 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000937 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000938 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100939 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000940 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
941 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000942 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100943 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100944 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100945 } else {
946 // sequence with 2 items
947 goto sequence_with_2_items;
948 }
949 } else {
950 // sequence with 2 items
951 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000952 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100953 }
954 return;
955 }
956 return;
957
Damien George9d181f62014-04-27 16:55:27 +0100958 cannot_assign:
959 compile_syntax_error(comp, pn, "can't assign to expression");
960 return;
961
Damien429d7192013-10-04 19:53:11 +0100962 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100963 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100964}
965
966// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100967// if we are not in CPython compatibility mode then:
968// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
969// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
970// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100971STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
Damien George30565092014-03-31 11:30:17 +0100972 assert(n_pos_defaults >= 0);
973 assert(n_kw_defaults >= 0);
974
Damien429d7192013-10-04 19:53:11 +0100975 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000976 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100977 int nfree = 0;
978 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000979 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
980 id_info_t *id = &comp->scope_cur->id_info[i];
981 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
982 for (int j = 0; j < this_scope->id_info_len; j++) {
983 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100984 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000985#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100986 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000987#else
988 // in Micro Python we load closures using LOAD_FAST
Damien George7ff996c2014-09-08 23:05:16 +0100989 EMIT_ARG(load_fast, id->qst, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000990#endif
Damien318aec62013-12-10 18:28:17 +0000991 nfree += 1;
992 }
993 }
Damien429d7192013-10-04 19:53:11 +0100994 }
995 }
996 }
Damien429d7192013-10-04 19:53:11 +0100997
998 // make the function/closure
999 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001000 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001001 } else {
Damien George3558f622014-04-20 17:50:40 +01001002 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001003 }
1004}
1005
Damien George6be0b0a2014-08-15 14:30:52 +01001006STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001007 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001008 comp->have_star = true;
1009 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001010 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1011 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001012 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001013 } else {
1014 // named star
Damien429d7192013-10-04 19:53:11 +01001015 }
Damien George8b19db02014-04-11 23:25:34 +01001016 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001017
1018 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001019 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001020 // TODO do we need to do anything with this?
1021
1022 } else {
1023 mp_parse_node_t pn_id;
1024 mp_parse_node_t pn_colon;
1025 mp_parse_node_t pn_equal;
1026 if (MP_PARSE_NODE_IS_ID(pn)) {
1027 // this parameter is just an id
1028
1029 pn_id = pn;
1030 pn_colon = MP_PARSE_NODE_NULL;
1031 pn_equal = MP_PARSE_NODE_NULL;
1032
1033 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1034 // this parameter has a colon and/or equal specifier
1035
1036 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1037 pn_id = pns->nodes[0];
1038 pn_colon = pns->nodes[1];
1039 pn_equal = pns->nodes[2];
1040
1041 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001042 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001043 assert(0);
1044 return;
1045 }
1046
1047 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1048 // this parameter does not have a default value
1049
1050 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001051 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001052 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001053 return;
1054 }
1055
1056 } else {
1057 // this parameter has a default value
1058 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1059
Damien George8b19db02014-04-11 23:25:34 +01001060 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001061 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001062#if MICROPY_EMIT_CPYTHON
1063 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1064 compile_node(comp, pn_equal);
1065#else
Damien George69b89d22014-04-11 13:38:30 +00001066 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1067 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001068 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1069 // we need to do this here before we start building the map for the default keywords
1070 if (comp->num_default_params > 0) {
1071 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001072 } else {
1073 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001074 }
Damien George69b89d22014-04-11 13:38:30 +00001075 // first default dict param, so make the map
1076 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001077 }
Damien Georgef0778a72014-06-07 22:01:00 +01001078
1079 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001080 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001081 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001082 EMIT(store_map);
1083#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001084 } else {
Damien George69b89d22014-04-11 13:38:30 +00001085 comp->num_default_params += 1;
1086 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001087 }
1088 }
1089
1090 // TODO pn_colon not implemented
1091 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001092 }
1093}
1094
1095// leaves function object on stack
1096// returns function name
Damiend99b0522013-12-21 18:17:45 +00001097qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001098 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001099 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001100 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001101 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001102 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001103 }
1104
1105 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001106 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001107 uint old_num_dict_params = comp->num_dict_params;
1108 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001109
1110 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001111 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001112 comp->num_dict_params = 0;
1113 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001114 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001115
Damien Georgea91ac202014-10-05 19:01:34 +01001116 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001117 return MP_QSTR_NULL;
1118 }
1119
Damien Georgee337f1e2014-03-31 15:18:37 +01001120#if !MICROPY_EMIT_CPYTHON
1121 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001122 // the default keywords args may have already made the tuple; if not, do it now
1123 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001124 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001125 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001126 }
1127#endif
1128
Damien429d7192013-10-04 19:53:11 +01001129 // get the scope for this function
1130 scope_t *fscope = (scope_t*)pns->nodes[4];
1131
1132 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001133 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001134
1135 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001136 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001137 comp->num_dict_params = old_num_dict_params;
1138 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001139
1140 // return its name (the 'f' in "def f(...):")
1141 return fscope->simple_name;
1142}
1143
1144// leaves class object on stack
1145// returns class name
Damiend99b0522013-12-21 18:17:45 +00001146qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001147 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001148 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001149 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001150 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001151 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001152 }
1153
1154 EMIT(load_build_class);
1155
1156 // scope for this class
1157 scope_t *cscope = (scope_t*)pns->nodes[3];
1158
1159 // compile the class
1160 close_over_variables_etc(comp, cscope, 0, 0);
1161
1162 // get its name
Damien George968bf342014-04-27 19:12:05 +01001163 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001164
1165 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001166 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1167 mp_parse_node_t parents = pns->nodes[1];
1168 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1169 parents = MP_PARSE_NODE_NULL;
1170 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001171 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001172 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001173
1174 // return its name (the 'C' in class C(...):")
1175 return cscope->simple_name;
1176}
1177
Damien6cdd3af2013-10-05 18:08:26 +01001178// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001179STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001180 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001181 return false;
1182 }
1183
1184 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001185 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001186 return true;
1187 }
1188
Damiend99b0522013-12-21 18:17:45 +00001189 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001190 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001191 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001192#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001193 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001194 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001195 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001196 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001197#endif
Damien3ef4abb2013-10-12 16:53:13 +01001198#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001199 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001200 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001201#endif
Damien6cdd3af2013-10-05 18:08:26 +01001202 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001203 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001204 }
1205
1206 return true;
1207}
1208
Damiend99b0522013-12-21 18:17:45 +00001209void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001210 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001211 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001212 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1213
Damien6cdd3af2013-10-05 18:08:26 +01001214 // inherit emit options for this function/class definition
1215 uint emit_options = comp->scope_cur->emit_options;
1216
1217 // compile each decorator
1218 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001219 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001220 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1221 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001222
1223 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001224 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001225 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1226
1227 // check for built-in decorators
1228 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1229 // this was a built-in
1230 num_built_in_decorators += 1;
1231
1232 } else {
1233 // not a built-in, compile normally
1234
1235 // compile the decorator function
1236 compile_node(comp, name_nodes[0]);
1237 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001238 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001239 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001240 }
1241
1242 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001243 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001244 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001245 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001246 compile_node(comp, pns_decorator->nodes[1]);
1247 }
Damien429d7192013-10-04 19:53:11 +01001248 }
1249 }
1250
1251 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001252 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001253 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001254 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001255 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001256 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001257 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001258 } else {
1259 // shouldn't happen
1260 assert(0);
1261 }
1262
1263 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001264 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001265 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001266 }
1267
1268 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001269 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001270}
1271
Damiend99b0522013-12-21 18:17:45 +00001272void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001273 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001274 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001275 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001276}
1277
Damien George6be0b0a2014-08-15 14:30:52 +01001278STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001279 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001280 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001281 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1282 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001283
1284 compile_node(comp, pns->nodes[0]); // base of the power node
1285
Damiend99b0522013-12-21 18:17:45 +00001286 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1287 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1288 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1289 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001290 for (int i = 0; i < n - 1; i++) {
1291 compile_node(comp, pns1->nodes[i]);
1292 }
Damiend99b0522013-12-21 18:17:45 +00001293 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1294 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001295 }
Damiend99b0522013-12-21 18:17:45 +00001296 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001297 // can't delete function calls
1298 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001299 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001300 compile_node(comp, pns1->nodes[0]);
1301 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001302 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1303 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001304 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001305 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001306 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001307 }
1308 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001309 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001310 }
1311
Damiend99b0522013-12-21 18:17:45 +00001312 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001313 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001314 }
Damiend99b0522013-12-21 18:17:45 +00001315 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1316 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1317 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1318 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001319 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1320
Damiend99b0522013-12-21 18:17:45 +00001321 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1322 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1323 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001324 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001325 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001326 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001327 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001328 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001329 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001330 c_del_stmt(comp, pns->nodes[0]);
1331 for (int i = 0; i < n; i++) {
1332 c_del_stmt(comp, pns1->nodes[i]);
1333 }
Damiend99b0522013-12-21 18:17:45 +00001334 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001335 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001336 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001337 } else {
1338 // sequence with 2 items
1339 goto sequence_with_2_items;
1340 }
1341 } else {
1342 // sequence with 2 items
1343 sequence_with_2_items:
1344 c_del_stmt(comp, pns->nodes[0]);
1345 c_del_stmt(comp, pns->nodes[1]);
1346 }
1347 } else {
1348 // tuple with 1 element
1349 c_del_stmt(comp, pn);
1350 }
1351 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001352 // TODO is there anything else to implement?
1353 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001354 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001355
1356 return;
1357
1358cannot_delete:
1359 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001360}
1361
Damiend99b0522013-12-21 18:17:45 +00001362void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001363 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1364}
1365
Damiend99b0522013-12-21 18:17:45 +00001366void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001367 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001368 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001369 }
Damien George090c9232014-10-17 14:08:49 +00001370 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001371 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001372}
1373
Damiend99b0522013-12-21 18:17:45 +00001374void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001375 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001376 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001377 }
Damien George090c9232014-10-17 14:08:49 +00001378 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001379 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001380}
1381
Damiend99b0522013-12-21 18:17:45 +00001382void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001383 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001384 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001385 return;
1386 }
Damiend99b0522013-12-21 18:17:45 +00001387 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001388 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001389 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001390 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001391 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001392 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1393 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001394
Damien George6f355fd2014-04-10 14:11:31 +01001395 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001396 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1397 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1398 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001399 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001400 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1401 } else {
1402 compile_node(comp, pns->nodes[0]);
1403 }
1404 EMIT(return_value);
1405}
1406
Damiend99b0522013-12-21 18:17:45 +00001407void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001408 compile_node(comp, pns->nodes[0]);
1409 EMIT(pop_top);
1410}
1411
Damiend99b0522013-12-21 18:17:45 +00001412void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1413 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001414 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001415 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001416 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001417 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001418 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001419 compile_node(comp, pns->nodes[0]);
1420 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001421 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001422 } else {
1423 // raise x
1424 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001425 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001426 }
1427}
1428
Damien George635543c2014-04-10 12:56:52 +01001429// q_base holds the base of the name
1430// eg a -> q_base=a
1431// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001432STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001433 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001434 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1435 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001436 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001437 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001438 pn = pns->nodes[0];
1439 is_as = true;
1440 }
Damien George635543c2014-04-10 12:56:52 +01001441 if (MP_PARSE_NODE_IS_NULL(pn)) {
1442 // empty name (eg, from . import x)
1443 *q_base = MP_QSTR_;
1444 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1445 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001446 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001447 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001448 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001449 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001450 }
Damien George635543c2014-04-10 12:56:52 +01001451 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001452 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1453 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1454 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001455 // a name of the form a.b.c
1456 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001457 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001458 }
Damiend99b0522013-12-21 18:17:45 +00001459 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001460 int len = n - 1;
1461 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001462 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001463 }
Damien George55baff42014-01-21 21:40:13 +00001464 byte *q_ptr;
1465 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001466 for (int i = 0; i < n; i++) {
1467 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001468 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001469 }
Damien George39dc1452014-10-03 19:52:22 +01001470 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001471 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001472 memcpy(str_dest, str_src, str_src_len);
1473 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001474 }
Damien George635543c2014-04-10 12:56:52 +01001475 qstr q_full = qstr_build_end(q_ptr);
1476 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001477 if (is_as) {
1478 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001479 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001480 }
1481 }
1482 } else {
Damien George635543c2014-04-10 12:56:52 +01001483 // shouldn't happen
1484 assert(0);
Damien429d7192013-10-04 19:53:11 +01001485 }
1486 } else {
Damien George635543c2014-04-10 12:56:52 +01001487 // shouldn't happen
1488 assert(0);
Damien429d7192013-10-04 19:53:11 +01001489 }
1490}
1491
Damien George6be0b0a2014-08-15 14:30:52 +01001492STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001493 EMIT_ARG(load_const_small_int, 0); // level 0 import
1494 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1495 qstr q_base;
1496 do_import_name(comp, pn, &q_base);
1497 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001498}
1499
Damiend99b0522013-12-21 18:17:45 +00001500void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001501 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1502}
1503
Damiend99b0522013-12-21 18:17:45 +00001504void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001505 mp_parse_node_t pn_import_source = pns->nodes[0];
1506
1507 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1508 uint import_level = 0;
1509 do {
1510 mp_parse_node_t pn_rel;
1511 if (MP_PARSE_NODE_IS_TOKEN(pn_import_source) || MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_one_or_more_period_or_ellipsis)) {
1512 // This covers relative imports with dots only like "from .. import"
1513 pn_rel = pn_import_source;
1514 pn_import_source = MP_PARSE_NODE_NULL;
1515 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1516 // This covers relative imports starting with dot(s) like "from .foo import"
1517 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1518 pn_rel = pns_2b->nodes[0];
1519 pn_import_source = pns_2b->nodes[1];
1520 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1521 } else {
1522 // Not a relative import
1523 break;
1524 }
1525
1526 // get the list of . and/or ...'s
1527 mp_parse_node_t *nodes;
1528 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1529
1530 // count the total number of .'s
1531 for (int i = 0; i < n; i++) {
1532 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1533 import_level++;
1534 } else {
1535 // should be an MP_TOKEN_ELLIPSIS
1536 import_level += 3;
1537 }
1538 }
1539 } while (0);
1540
Damiend99b0522013-12-21 18:17:45 +00001541 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001542 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001543
1544 // build the "fromlist" tuple
1545#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001546 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001547#else
Damien George708c0732014-04-27 19:23:46 +01001548 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001549 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001550#endif
1551
1552 // do the import
Damien George635543c2014-04-10 12:56:52 +01001553 qstr dummy_q;
1554 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001555 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001556
Damien429d7192013-10-04 19:53:11 +01001557 } else {
Damien George635543c2014-04-10 12:56:52 +01001558 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001559
1560 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001561 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001562 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001563#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001564 {
1565 vstr_t *vstr = vstr_new();
1566 vstr_printf(vstr, "(");
1567 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001568 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1569 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1570 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001571 if (i > 0) {
1572 vstr_printf(vstr, ", ");
1573 }
1574 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001575 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001576 const byte *str = qstr_data(id2, &len);
1577 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001578 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001579 }
Damien02f89412013-12-12 15:13:36 +00001580 if (n == 1) {
1581 vstr_printf(vstr, ",");
1582 }
1583 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001584 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001585 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001586 }
Damiendb4c3612013-12-10 17:27:24 +00001587#else
1588 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001589 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1590 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1591 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001592 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001593 }
Damien Georgeb9791222014-01-23 00:34:21 +00001594 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001595#endif
1596
1597 // do the import
Damien George635543c2014-04-10 12:56:52 +01001598 qstr dummy_q;
1599 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001600 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001601 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1602 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1603 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001604 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001605 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001606 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001607 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001608 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001609 }
1610 }
1611 EMIT(pop_top);
1612 }
1613}
1614
Damiend99b0522013-12-21 18:17:45 +00001615void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001616 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001617 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1618 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001619 } else {
Damiend99b0522013-12-21 18:17:45 +00001620 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1621 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001622 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001623 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001624 }
Damien429d7192013-10-04 19:53:11 +01001625 }
1626 }
1627}
1628
Damiend99b0522013-12-21 18:17:45 +00001629void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001630 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001631 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1632 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001633 } else {
Damiend99b0522013-12-21 18:17:45 +00001634 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1635 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001636 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001637 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001638 }
Damien429d7192013-10-04 19:53:11 +01001639 }
1640 }
1641}
1642
Damiend99b0522013-12-21 18:17:45 +00001643void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001644 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001645 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001646 EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001647 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001648 // assertion message
1649 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001650 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001651 }
Damien Georgeb9791222014-01-23 00:34:21 +00001652 EMIT_ARG(raise_varargs, 1);
1653 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001654}
1655
Damiend99b0522013-12-21 18:17:45 +00001656void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001657 // TODO proper and/or short circuiting
1658
Damien George6f355fd2014-04-10 14:11:31 +01001659 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001660
Damien George6f355fd2014-04-10 14:11:31 +01001661 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001662 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1663
1664 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001665
1666 if (
1667#if !MICROPY_EMIT_CPYTHON
1668 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1669 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1670#endif
1671 // optimisation to not jump if last instruction was return
1672 !EMIT(last_emit_was_return_value)
1673 ) {
1674 // jump over elif/else blocks
1675 EMIT_ARG(jump, l_end);
1676 }
1677
Damien Georgeb9791222014-01-23 00:34:21 +00001678 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001679
Damien George235f9b32014-10-17 17:30:16 +00001680 // compile elif blocks (if any)
1681 mp_parse_node_t *pn_elif;
1682 int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1683 for (int i = 0; i < n_elif; i++) {
1684 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1685 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001686
Damien George235f9b32014-10-17 17:30:16 +00001687 l_fail = comp_next_label(comp);
1688 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001689
Damien George235f9b32014-10-17 17:30:16 +00001690 compile_node(comp, pns_elif->nodes[1]); // elif block
1691 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
1692 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001693 }
Damien George235f9b32014-10-17 17:30:16 +00001694 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001695 }
1696
1697 // compile else block
1698 compile_node(comp, pns->nodes[3]); // can be null
1699
Damien Georgeb9791222014-01-23 00:34:21 +00001700 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001701}
1702
Damien Georgecbddb272014-02-01 20:08:18 +00001703#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001704 uint16_t old_break_label = comp->break_label; \
1705 uint16_t old_continue_label = comp->continue_label; \
1706 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001707 uint break_label = comp_next_label(comp); \
1708 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001709 comp->break_label = break_label; \
1710 comp->continue_label = continue_label; \
1711 comp->break_continue_except_level = comp->cur_except_level;
1712
1713#define END_BREAK_CONTINUE_BLOCK \
1714 comp->break_label = old_break_label; \
1715 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001716 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001717
Damiend99b0522013-12-21 18:17:45 +00001718void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001719 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001720
Damience89a212013-10-15 22:25:17 +01001721 // compared to CPython, we have an optimised version of while loops
1722#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001723 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001724 EMIT_ARG(setup_loop, break_label);
1725 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001726 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1727 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001728 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001729 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001730 }
Damien Georgeb9791222014-01-23 00:34:21 +00001731 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001732 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1733 // this is a small hack to agree with CPython
1734 if (!node_is_const_true(pns->nodes[0])) {
1735 EMIT(pop_block);
1736 }
Damience89a212013-10-15 22:25:17 +01001737#else
Damien George6f355fd2014-04-10 14:11:31 +01001738 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001739 EMIT_ARG(jump, continue_label);
1740 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001741 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001742 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001743 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1744#endif
1745
1746 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001747 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001748
1749 compile_node(comp, pns->nodes[2]); // else
1750
Damien Georgeb9791222014-01-23 00:34:21 +00001751 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001752}
1753
Damien George2ac4af62014-08-15 16:45:41 +01001754#if !MICROPY_EMIT_CPYTHON
Damienf72fd0e2013-11-06 20:20:49 +00001755// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001756// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1757// And, if the loop never runs, the loop variable should never be assigned
Damien George6be0b0a2014-08-15 14:30:52 +01001758STATIC 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 +00001759 START_BREAK_CONTINUE_BLOCK
Damien George5b5562c2014-05-31 17:59:11 +01001760 // note that we don't need to pop anything when breaking from an optimise for loop
Damienf72fd0e2013-11-06 20:20:49 +00001761
Damien George6f355fd2014-04-10 14:11:31 +01001762 uint top_label = comp_next_label(comp);
1763 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001764
Damien George3ff2d032014-03-31 18:02:22 +01001765 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001766 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001767 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001768
Damien Georgeb9791222014-01-23 00:34:21 +00001769 EMIT_ARG(jump, entry_label);
1770 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001771
Damien George3ff2d032014-03-31 18:02:22 +01001772 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001773 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001774
1775 // store next value to var
1776 c_assign(comp, pn_var, ASSIGN_STORE);
1777
Damienf3822fc2013-11-09 20:12:03 +00001778 // compile body
1779 compile_node(comp, pn_body);
1780
Damien Georgeb9791222014-01-23 00:34:21 +00001781 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001782
Damien George3ff2d032014-03-31 18:02:22 +01001783 // compile: var + step, duplicated on stack
1784 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001785 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001786 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001787 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001788
Damien Georgeb9791222014-01-23 00:34:21 +00001789 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001790
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001791 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001792 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001793 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1794 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001795 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001796 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001797 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001798 }
Damien Georgeb9791222014-01-23 00:34:21 +00001799 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001800
Damien George3ff2d032014-03-31 18:02:22 +01001801 // discard final value of var that failed the loop condition
1802 EMIT(pop_top);
1803
Damienf72fd0e2013-11-06 20:20:49 +00001804 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001805 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001806
1807 compile_node(comp, pn_else);
1808
Damien Georgeb9791222014-01-23 00:34:21 +00001809 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001810}
Damien George2ac4af62014-08-15 16:45:41 +01001811#endif
Damienf72fd0e2013-11-06 20:20:49 +00001812
Damiend99b0522013-12-21 18:17:45 +00001813void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001814#if !MICROPY_EMIT_CPYTHON
1815 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1816 // this is actually slower, but uses no heap memory
1817 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001818 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 +00001819 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001820 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1821 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1822 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1823 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001824 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1825 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001826 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001827 mp_parse_node_t pn_range_start;
1828 mp_parse_node_t pn_range_end;
1829 mp_parse_node_t pn_range_step;
1830 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001831 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001832 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001833 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001834 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001835 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001836 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001837 } else if (n_args == 2) {
1838 pn_range_start = args[0];
1839 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001840 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001841 } else {
1842 pn_range_start = args[0];
1843 pn_range_end = args[1];
1844 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001845 // We need to know sign of step. This is possible only if it's constant
1846 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1847 optimize = false;
1848 }
Damienf72fd0e2013-11-06 20:20:49 +00001849 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001850 }
1851 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001852 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1853 return;
1854 }
1855 }
1856 }
1857#endif
1858
Damien Georgecbddb272014-02-01 20:08:18 +00001859 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001860 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001861
Damien George6f355fd2014-04-10 14:11:31 +01001862 uint pop_label = comp_next_label(comp);
1863 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001864
Damience89a212013-10-15 22:25:17 +01001865 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1866#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001867 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001868#endif
1869
Damien429d7192013-10-04 19:53:11 +01001870 compile_node(comp, pns->nodes[1]); // iterator
1871 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001872 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001873 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001874 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1875 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001876 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001877 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001878 }
Damien Georgeb9791222014-01-23 00:34:21 +00001879 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001880 EMIT(for_iter_end);
1881
1882 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001883 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001884
Damience89a212013-10-15 22:25:17 +01001885#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001886 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001887#endif
Damien429d7192013-10-04 19:53:11 +01001888
1889 compile_node(comp, pns->nodes[3]); // else (not tested)
1890
Damien Georgeb9791222014-01-23 00:34:21 +00001891 EMIT_ARG(label_assign, break_label);
1892 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001893}
1894
Damien George6be0b0a2014-08-15 14:30:52 +01001895STATIC 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 +01001896 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001897 uint l1 = comp_next_label(comp);
1898 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001899
Damien Georgeb9791222014-01-23 00:34:21 +00001900 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001901 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001902
Damien429d7192013-10-04 19:53:11 +01001903 compile_node(comp, pn_body); // body
1904 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001905 EMIT_ARG(jump, success_label); // jump over exception handler
1906
1907 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001908 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001909
Damien George6f355fd2014-04-10 14:11:31 +01001910 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001911
1912 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001913 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1914 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001915
1916 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001917 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001918
Damiend99b0522013-12-21 18:17:45 +00001919 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001920 // this is a catch all exception handler
1921 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001922 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001923 return;
1924 }
1925 } else {
1926 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001927 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1928 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1929 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1930 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001931 // handler binds the exception to a local
1932 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001933 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001934 }
1935 }
1936 EMIT(dup_top);
1937 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001938 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001939 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001940 }
1941
1942 EMIT(pop_top);
1943
1944 if (qstr_exception_local == 0) {
1945 EMIT(pop_top);
1946 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001947 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001948 }
1949
1950 EMIT(pop_top);
1951
Damien George6f355fd2014-04-10 14:11:31 +01001952 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001953 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001954 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001955 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001956 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001957 }
1958 compile_node(comp, pns_except->nodes[1]);
1959 if (qstr_exception_local != 0) {
1960 EMIT(pop_block);
1961 }
1962 EMIT(pop_except);
1963 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001964 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1965 EMIT_ARG(label_assign, l3);
1966 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1967 EMIT_ARG(store_id, qstr_exception_local);
1968 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001969
Damien George8dcc0c72014-03-27 10:55:21 +00001970 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001971 EMIT(end_finally);
1972 }
Damien Georgeb9791222014-01-23 00:34:21 +00001973 EMIT_ARG(jump, l2);
1974 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001975 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001976 }
1977
Damien George8dcc0c72014-03-27 10:55:21 +00001978 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001979 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001980 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001981
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001983 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001984 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001985}
1986
Damien George6be0b0a2014-08-15 14:30:52 +01001987STATIC 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 +01001988 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001989
Damien Georgeb9791222014-01-23 00:34:21 +00001990 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001991 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001992
Damien429d7192013-10-04 19:53:11 +01001993 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001994 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001995 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001996 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001997 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001998 } else {
1999 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2000 }
2001 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002002 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2003 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002004 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002005
Damien George8dcc0c72014-03-27 10:55:21 +00002006 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002007 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002008}
2009
Damiend99b0522013-12-21 18:17:45 +00002010void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2011 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2012 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2013 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002014 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002015 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2016 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002017 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002018 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002019 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002020 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002021 // no finally
2022 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2023 } else {
2024 // have finally
Damiend99b0522013-12-21 18:17:45 +00002025 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 +01002026 }
2027 } else {
2028 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002029 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002030 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002031 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002032 }
2033 } else {
2034 // shouldn't happen
2035 assert(0);
2036 }
2037}
2038
Damien George6be0b0a2014-08-15 14:30:52 +01002039STATIC 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 +01002040 if (n == 0) {
2041 // no more pre-bits, compile the body of the with
2042 compile_node(comp, body);
2043 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002044 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002045 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002046 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002047 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002048 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002049 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002050 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2051 } else {
2052 // this pre-bit is just an expression
2053 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002054 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002055 EMIT(pop_top);
2056 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002057 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002058 // compile additional pre-bits and the body
2059 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2060 // finish this with block
2061 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002062 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2063 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002064 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002065 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002066 EMIT(end_finally);
2067 }
2068}
2069
Damiend99b0522013-12-21 18:17:45 +00002070void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002071 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002072 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002073 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2074 assert(n > 0);
2075
2076 // compile in a nested fashion
2077 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2078}
2079
Damiend99b0522013-12-21 18:17:45 +00002080void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2081 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002082 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2083 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002084 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002085 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002086 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002087 EMIT(pop_top);
2088
Damien429d7192013-10-04 19:53:11 +01002089 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002090 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002091 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2092 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002093 // do nothing with a lonely constant
2094 } else {
2095 compile_node(comp, pns->nodes[0]); // just an expression
2096 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2097 }
Damien429d7192013-10-04 19:53:11 +01002098 }
2099 } else {
Damiend99b0522013-12-21 18:17:45 +00002100 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2101 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002102 if (kind == PN_expr_stmt_augassign) {
2103 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2104 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002105 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002106 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002107 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002108 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2109 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2110 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2111 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2112 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2113 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2114 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2115 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2116 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2117 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2118 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2119 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2120 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002121 }
Damien George7e5fb242014-02-01 22:18:47 +00002122 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002123 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2124 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002125 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2126 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002127 // following CPython, we store left-most first
2128 if (rhs > 0) {
2129 EMIT(dup_top);
2130 }
2131 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2132 for (int i = 0; i < rhs; i++) {
2133 if (i + 1 < rhs) {
2134 EMIT(dup_top);
2135 }
Damiend99b0522013-12-21 18:17:45 +00002136 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002137 }
2138 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002139 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002140 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2141 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2142 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002143 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002144 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2145 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002146 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2147 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2148 // can't optimise when it's a star expression on the lhs
2149 goto no_optimisation;
2150 }
Damien429d7192013-10-04 19:53:11 +01002151 compile_node(comp, pns10->nodes[0]); // rhs
2152 compile_node(comp, pns10->nodes[1]); // rhs
2153 EMIT(rot_two);
2154 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2155 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002156 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2157 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2158 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2159 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002160 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002161 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2162 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002163 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2164 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2165 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2166 // can't optimise when it's a star expression on the lhs
2167 goto no_optimisation;
2168 }
Damien429d7192013-10-04 19:53:11 +01002169 compile_node(comp, pns10->nodes[0]); // rhs
2170 compile_node(comp, pns10->nodes[1]); // rhs
2171 compile_node(comp, pns10->nodes[2]); // rhs
2172 EMIT(rot_three);
2173 EMIT(rot_two);
2174 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2175 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2176 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2177 } else {
Damien George495d7812014-04-08 17:51:47 +01002178 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002179 compile_node(comp, pns1->nodes[0]); // rhs
2180 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2181 }
2182 } else {
2183 // shouldn't happen
2184 assert(0);
2185 }
2186 }
2187}
2188
Damien George6be0b0a2014-08-15 14:30:52 +01002189STATIC 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 +00002190 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002191 compile_node(comp, pns->nodes[0]);
2192 for (int i = 1; i < num_nodes; i += 1) {
2193 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002194 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002195 }
2196}
2197
Damiend99b0522013-12-21 18:17:45 +00002198void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2199 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2200 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002201
Damien George6f355fd2014-04-10 14:11:31 +01002202 uint l_fail = comp_next_label(comp);
2203 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002204 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2205 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002206 EMIT_ARG(jump, l_end);
2207 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002208 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002209 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002210 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002211}
2212
Damiend99b0522013-12-21 18:17:45 +00002213void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002214 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002215 //mp_parse_node_t pn_params = pns->nodes[0];
2216 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002217
Damien George36db6bc2014-05-07 17:24:22 +01002218 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002219 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002220 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 +01002221 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002222 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002223 }
2224
2225 // get the scope for this lambda
2226 scope_t *this_scope = (scope_t*)pns->nodes[2];
2227
2228 // make the lambda
2229 close_over_variables_etc(comp, this_scope, 0, 0);
2230}
2231
Damiend99b0522013-12-21 18:17:45 +00002232void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002233 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002234 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002235 for (int i = 0; i < n; i += 1) {
2236 compile_node(comp, pns->nodes[i]);
2237 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002238 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002239 }
2240 }
Damien Georgeb9791222014-01-23 00:34:21 +00002241 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002242}
2243
Damiend99b0522013-12-21 18:17:45 +00002244void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002245 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002246 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002247 for (int i = 0; i < n; i += 1) {
2248 compile_node(comp, pns->nodes[i]);
2249 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002250 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002251 }
2252 }
Damien Georgeb9791222014-01-23 00:34:21 +00002253 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002254}
2255
Damiend99b0522013-12-21 18:17:45 +00002256void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002257 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002258 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002259}
2260
Damiend99b0522013-12-21 18:17:45 +00002261void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002262 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002263 compile_node(comp, pns->nodes[0]);
2264 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002265 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002266 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002267 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002268 }
2269 for (int i = 1; i + 1 < num_nodes; i += 2) {
2270 compile_node(comp, pns->nodes[i + 1]);
2271 if (i + 2 < num_nodes) {
2272 EMIT(dup_top);
2273 EMIT(rot_three);
2274 }
Damien George7e5fb242014-02-01 22:18:47 +00002275 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002276 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002277 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002278 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2279 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2280 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2281 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2282 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2283 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2284 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2285 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002286 }
2287 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002288 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2289 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2290 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002291 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002292 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002293 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002294 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002295 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002296 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002297 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002298 }
2299 } else {
2300 // shouldn't happen
2301 assert(0);
2302 }
2303 } else {
2304 // shouldn't happen
2305 assert(0);
2306 }
2307 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002308 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002309 }
2310 }
2311 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002312 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002313 EMIT_ARG(jump, l_end);
2314 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002315 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002316 EMIT(rot_two);
2317 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002318 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002319 }
2320}
2321
Damiend99b0522013-12-21 18:17:45 +00002322void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002323 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002324}
2325
Damiend99b0522013-12-21 18:17:45 +00002326void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002327 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002328}
2329
Damiend99b0522013-12-21 18:17:45 +00002330void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002331 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002332}
2333
Damiend99b0522013-12-21 18:17:45 +00002334void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002335 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002336}
2337
Damiend99b0522013-12-21 18:17:45 +00002338void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2339 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002340 compile_node(comp, pns->nodes[0]);
2341 for (int i = 1; i + 1 < num_nodes; i += 2) {
2342 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002343 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002344 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002345 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002346 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002347 } else {
2348 // shouldn't happen
2349 assert(0);
2350 }
2351 }
2352}
2353
Damiend99b0522013-12-21 18:17:45 +00002354void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2355 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002356 compile_node(comp, pns->nodes[0]);
2357 for (int i = 1; i + 1 < num_nodes; i += 2) {
2358 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002359 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002360 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002361 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002362 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002363 } else {
2364 // shouldn't happen
2365 assert(0);
2366 }
2367 }
2368}
2369
Damiend99b0522013-12-21 18:17:45 +00002370void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2371 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002372 compile_node(comp, pns->nodes[0]);
2373 for (int i = 1; i + 1 < num_nodes; i += 2) {
2374 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002375 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002376 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002377 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002378 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002379 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002380 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002381 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002382 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002383 } else {
2384 // shouldn't happen
2385 assert(0);
2386 }
2387 }
2388}
2389
Damiend99b0522013-12-21 18:17:45 +00002390void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002391 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002392 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002393 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002394 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002395 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002396 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002397 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002398 } else {
2399 // shouldn't happen
2400 assert(0);
2401 }
2402}
2403
Damien George35e2a4e2014-02-05 00:51:47 +00002404void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2405 // this is to handle special super() call
2406 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2407
2408 compile_generic_all_nodes(comp, pns);
2409}
2410
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002411STATIC 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 +01002412 // function to call is on top of stack
2413
Damien George35e2a4e2014-02-05 00:51:47 +00002414#if !MICROPY_EMIT_CPYTHON
2415 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002416 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 +00002417 EMIT_ARG(load_id, MP_QSTR___class__);
2418 // get first argument to function
2419 bool found = false;
2420 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002421 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2422 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 +00002423 found = true;
2424 break;
2425 }
2426 }
2427 if (!found) {
2428 printf("TypeError: super() call cannot find self\n");
2429 return;
2430 }
Damien George922ddd62014-04-09 12:43:17 +01002431 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002432 return;
2433 }
2434#endif
2435
Damien George2c0842b2014-04-27 16:46:51 +01002436 // get the list of arguments
2437 mp_parse_node_t *args;
2438 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002439
Damien George2c0842b2014-04-27 16:46:51 +01002440 // compile the arguments
2441 // Rather than calling compile_node on the list, we go through the list of args
2442 // explicitly here so that we can count the number of arguments and give sensible
2443 // error messages.
2444 int n_positional = n_positional_extra;
2445 uint n_keyword = 0;
2446 uint star_flags = 0;
2447 for (int i = 0; i < n_args; i++) {
2448 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2449 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2450 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2451 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2452 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2453 return;
2454 }
2455 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2456 compile_node(comp, pns_arg->nodes[0]);
2457 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2458 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2459 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2460 return;
2461 }
2462 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2463 compile_node(comp, pns_arg->nodes[0]);
2464 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2465 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2466 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2467 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2468 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2469 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2470 return;
2471 }
Damien George968bf342014-04-27 19:12:05 +01002472 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002473 compile_node(comp, pns2->nodes[0]);
2474 n_keyword += 1;
2475 } else {
2476 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2477 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2478 n_positional++;
2479 }
2480 } else {
2481 goto normal_argument;
2482 }
2483 } else {
2484 normal_argument:
2485 if (n_keyword > 0) {
2486 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2487 return;
2488 }
2489 compile_node(comp, args[i]);
2490 n_positional++;
2491 }
Damien429d7192013-10-04 19:53:11 +01002492 }
2493
Damien George2c0842b2014-04-27 16:46:51 +01002494 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002495 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002496 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002497 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002498 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002499 }
Damien429d7192013-10-04 19:53:11 +01002500}
2501
Damiend99b0522013-12-21 18:17:45 +00002502void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2503 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002504 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002505 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 +01002506 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002507 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2508 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002509 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002510 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002511 i += 1;
2512 } else {
2513 compile_node(comp, pns->nodes[i]);
2514 }
Damien George35e2a4e2014-02-05 00:51:47 +00002515 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002516 }
2517}
2518
Damiend99b0522013-12-21 18:17:45 +00002519void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002520 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002521 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002522}
2523
Damiend99b0522013-12-21 18:17:45 +00002524void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002525 // a list of strings
Damien63321742013-12-10 17:41:49 +00002526
2527 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002528 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002529 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002530 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002531 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002532 int pn_kind;
2533 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2534 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2535 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2536 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2537 } else {
2538 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2539 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2540 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2541 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002542 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002543 }
Damien63321742013-12-10 17:41:49 +00002544 if (i == 0) {
2545 string_kind = pn_kind;
2546 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002547 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002548 return;
2549 }
Damien429d7192013-10-04 19:53:11 +01002550 }
Damien63321742013-12-10 17:41:49 +00002551
Damien63321742013-12-10 17:41:49 +00002552 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002553 byte *q_ptr;
2554 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002555 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002556 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002557 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002558 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2559 memcpy(s_dest, s, s_len);
2560 s_dest += s_len;
2561 } else {
2562 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002563 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2564 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002565 }
Damien63321742013-12-10 17:41:49 +00002566 }
Damien George55baff42014-01-21 21:40:13 +00002567 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002568
Damien Georgeb9791222014-01-23 00:34:21 +00002569 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002570}
2571
2572// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002573STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002574 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2575 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2576 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002577
Damien George36db6bc2014-05-07 17:24:22 +01002578 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002579 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002580 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 +01002581 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002582 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002583 }
2584
2585 // get the scope for this comprehension
2586 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2587
2588 // compile the comprehension
2589 close_over_variables_etc(comp, this_scope, 0, 0);
2590
2591 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2592 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002593 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002594}
2595
Damiend99b0522013-12-21 18:17:45 +00002596void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2597 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002598 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002599 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2600 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2601 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2602 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2603 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2604 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2605 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002606 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002607 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002608 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002609 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002610 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002611 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002612 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002613 // generator expression
2614 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2615 } else {
2616 // tuple with 2 items
2617 goto tuple_with_2_items;
2618 }
2619 } else {
2620 // tuple with 2 items
2621 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002622 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002623 }
2624 } else {
2625 // parenthesis around a single item, is just that item
2626 compile_node(comp, pns->nodes[0]);
2627 }
2628}
2629
Damiend99b0522013-12-21 18:17:45 +00002630void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2631 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002632 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002633 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002634 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2635 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2636 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2637 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2638 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002639 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002640 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002641 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002642 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002643 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002644 // list of many items
2645 compile_node(comp, pns2->nodes[0]);
2646 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002647 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002648 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002649 // list comprehension
2650 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2651 } else {
2652 // list with 2 items
2653 goto list_with_2_items;
2654 }
2655 } else {
2656 // list with 2 items
2657 list_with_2_items:
2658 compile_node(comp, pns2->nodes[0]);
2659 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002660 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002661 }
2662 } else {
2663 // list with 1 item
2664 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002665 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002666 }
2667}
2668
Damiend99b0522013-12-21 18:17:45 +00002669void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2670 mp_parse_node_t pn = pns->nodes[0];
2671 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002672 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002673 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002674 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2675 pns = (mp_parse_node_struct_t*)pn;
2676 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002677 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002678 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002679 compile_node(comp, pn);
2680 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002681 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2682 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2683 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2684 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002685 // dict/set with multiple elements
2686
2687 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002688 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002689 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2690
2691 // first element sets whether it's a dict or set
2692 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002693 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002694 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002695 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002696 compile_node(comp, pns->nodes[0]);
2697 EMIT(store_map);
2698 is_dict = true;
2699 } else {
2700 // a set
2701 compile_node(comp, pns->nodes[0]); // 1st value of set
2702 is_dict = false;
2703 }
2704
2705 // process rest of elements
2706 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002707 mp_parse_node_t pn = nodes[i];
2708 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002709 compile_node(comp, pn);
2710 if (is_dict) {
2711 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002712 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002713 return;
2714 }
2715 EMIT(store_map);
2716 } else {
2717 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002718 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002719 return;
2720 }
2721 }
2722 }
2723
2724 // if it's a set, build it
2725 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002726 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002727 }
Damiend99b0522013-12-21 18:17:45 +00002728 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002729 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002730 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002731 // a dictionary comprehension
2732 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2733 } else {
2734 // a set comprehension
2735 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2736 }
2737 } else {
2738 // shouldn't happen
2739 assert(0);
2740 }
2741 } else {
2742 // set with one element
2743 goto set_with_one_element;
2744 }
2745 } else {
2746 // set with one element
2747 set_with_one_element:
2748 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002749 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002750 }
2751}
2752
Damiend99b0522013-12-21 18:17:45 +00002753void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002754 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002755}
2756
Damiend99b0522013-12-21 18:17:45 +00002757void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002758 // object who's index we want is on top of stack
2759 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002760 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002761}
2762
Damiend99b0522013-12-21 18:17:45 +00002763void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002764 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002765 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002766}
2767
Damiend99b0522013-12-21 18:17:45 +00002768void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2769 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2770 mp_parse_node_t pn = pns->nodes[0];
2771 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002772 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002773 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2774 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002775 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2776 pns = (mp_parse_node_struct_t*)pn;
2777 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002778 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002779 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002780 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002781 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002782 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002783 } else {
2784 // [?::x]
2785 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002786 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002787 }
Damiend99b0522013-12-21 18:17:45 +00002788 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002789 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002790 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2791 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2792 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2793 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002794 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002795 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002796 } else {
2797 // [?:x:x]
2798 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002799 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002800 }
2801 } else {
2802 // [?:x]
2803 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002804 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002805 }
2806 } else {
2807 // [?:x]
2808 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002809 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002810 }
2811}
2812
Damiend99b0522013-12-21 18:17:45 +00002813void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002814 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002815 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2816 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002817}
2818
Damiend99b0522013-12-21 18:17:45 +00002819void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002820 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002821 compile_subscript_3_helper(comp, pns);
2822}
2823
Damiend99b0522013-12-21 18:17:45 +00002824void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002825 // if this is called then we are compiling a dict key:value pair
2826 compile_node(comp, pns->nodes[1]); // value
2827 compile_node(comp, pns->nodes[0]); // key
2828}
2829
Damiend99b0522013-12-21 18:17:45 +00002830void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002831 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002832 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002833 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002834}
2835
Damiend99b0522013-12-21 18:17:45 +00002836void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002837 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002838 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002839 return;
2840 }
Damiend99b0522013-12-21 18:17:45 +00002841 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002842 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002843 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002844 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2845 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002846 compile_node(comp, pns->nodes[0]);
2847 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002848 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002849 EMIT(yield_from);
2850 } else {
2851 compile_node(comp, pns->nodes[0]);
2852 EMIT(yield_value);
2853 }
2854}
2855
Damiend99b0522013-12-21 18:17:45 +00002856typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002857STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002858 NULL,
2859#define nc NULL
2860#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002861#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002862#include "grammar.h"
2863#undef nc
2864#undef c
2865#undef DEF_RULE
2866};
2867
Damien George6be0b0a2014-08-15 14:30:52 +01002868STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002869 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002870 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002871 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002872 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002873 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002874 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002875 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002876 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002877 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002878 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2879 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2880 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2881 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002882 case MP_PARSE_NODE_TOKEN:
2883 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002884 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002885 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002886 // do nothing
2887 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002888 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002889 }
2890 break;
Damien429d7192013-10-04 19:53:11 +01002891 default: assert(0);
2892 }
2893 } else {
Damiend99b0522013-12-21 18:17:45 +00002894 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002895 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002896 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002897 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 +01002898 } else {
Damien George5042bce2014-05-25 22:06:06 +01002899 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2900 if (f == NULL) {
2901 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2902#if MICROPY_DEBUG_PRINTERS
2903 mp_parse_node_print(pn, 0);
2904#endif
2905 compile_syntax_error(comp, pn, "internal compiler error");
2906 } else {
2907 f(comp, pns);
2908 }
Damien429d7192013-10-04 19:53:11 +01002909 }
2910 }
2911}
2912
Damien George2ac4af62014-08-15 16:45:41 +01002913STATIC 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 +01002914 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002915 qstr param_name = MP_QSTR_NULL;
2916 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002917 if (MP_PARSE_NODE_IS_ID(pn)) {
2918 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002919 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002920 // comes after a star, so counts as a keyword-only parameter
2921 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002922 } else {
Damien George2827d622014-04-27 15:50:52 +01002923 // comes before a star, so counts as a positional parameter
2924 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002925 }
Damienb14de212013-10-06 00:28:28 +01002926 } else {
Damiend99b0522013-12-21 18:17:45 +00002927 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2928 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2929 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2930 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002931 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002932 // comes after a star, so counts as a keyword-only parameter
2933 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002934 } else {
Damien George2827d622014-04-27 15:50:52 +01002935 // comes before a star, so counts as a positional parameter
2936 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002937 }
Damiend99b0522013-12-21 18:17:45 +00002938 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002939 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002940 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002941 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002942 // bare star
2943 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002944 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002945 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002946 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002947 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002948 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01002949 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002950 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002951 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002952 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2953 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002954 } else {
2955 // shouldn't happen
2956 assert(0);
2957 }
Damiend99b0522013-12-21 18:17:45 +00002958 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2959 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002960 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002961 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002962 } else {
Damienb14de212013-10-06 00:28:28 +01002963 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002964 assert(0);
2965 }
Damien429d7192013-10-04 19:53:11 +01002966 }
2967
Damien George2827d622014-04-27 15:50:52 +01002968 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002969 bool added;
2970 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2971 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002972 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002973 return;
2974 }
Damien429d7192013-10-04 19:53:11 +01002975 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002976 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002977 }
2978}
2979
Damien George2827d622014-04-27 15:50:52 +01002980STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002981 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002982}
2983
Damien George2827d622014-04-27 15:50:52 +01002984STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002985 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2986}
2987
2988STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2989 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2990 // no annotation
2991 return;
2992 }
2993
2994 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2995 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2996 // named parameter with possible annotation
2997 // fallthrough
2998 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2999 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3000 // named star with possible annotation
3001 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3002 // fallthrough
3003 } else {
3004 // no annotation
3005 return;
3006 }
3007 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3008 // double star with possible annotation
3009 // fallthrough
3010 } else {
3011 // no annotation
3012 return;
3013 }
3014
3015 mp_parse_node_t pn_annotation = pns->nodes[1];
3016
3017 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3018 #if MICROPY_EMIT_NATIVE
3019 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3020 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3021 assert(id_info != NULL);
3022
3023 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3024 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3025 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3026 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3027 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003028 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003029 }
3030 }
3031 #endif // MICROPY_EMIT_NATIVE
3032 }
Damien429d7192013-10-04 19:53:11 +01003033}
3034
Damien George6be0b0a2014-08-15 14:30:52 +01003035STATIC 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 +01003036 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003037 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003038 // no more nested if/for; compile inner expression
3039 compile_node(comp, pn_inner_expr);
3040 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003041 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003042 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003043 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003044 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003045 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003046 } else {
3047 EMIT(yield_value);
3048 EMIT(pop_top);
3049 }
Damiend99b0522013-12-21 18:17:45 +00003050 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003051 // if condition
Damiend99b0522013-12-21 18:17:45 +00003052 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003053 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3054 pn_iter = pns_comp_if->nodes[1];
3055 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003056 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003057 // for loop
Damiend99b0522013-12-21 18:17:45 +00003058 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003059 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003060 uint l_end2 = comp_next_label(comp);
3061 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003062 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003063 EMIT_ARG(label_assign, l_top2);
3064 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003065 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3066 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 +00003067 EMIT_ARG(jump, l_top2);
3068 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003069 EMIT(for_iter_end);
3070 } else {
3071 // shouldn't happen
3072 assert(0);
3073 }
3074}
3075
Damien George1463c1f2014-04-25 23:52:57 +01003076STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3077#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003078 // see http://www.python.org/dev/peps/pep-0257/
3079
3080 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003081 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003082 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003083 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003084 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003085 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3086 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003087 for (int i = 0; i < num_nodes; i++) {
3088 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003089 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 +00003090 // not a newline, so this is the first statement; finish search
3091 break;
3092 }
3093 }
3094 // 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 +00003095 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003096 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003097 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003098 } else {
3099 return;
3100 }
3101
3102 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003103 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3104 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003105 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3106 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3107 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3108 // compile the doc string
3109 compile_node(comp, pns->nodes[0]);
3110 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003111 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003112 }
3113 }
Damien George1463c1f2014-04-25 23:52:57 +01003114#endif
Damien429d7192013-10-04 19:53:11 +01003115}
3116
Damien George1463c1f2014-04-25 23:52:57 +01003117STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003118 comp->pass = pass;
3119 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003120 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003121 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003122
Damien George36db6bc2014-05-07 17:24:22 +01003123 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003124 // reset maximum stack sizes in scope
3125 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003126 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003127 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003128 }
3129
Damien5ac1b2e2013-10-18 19:58:12 +01003130#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003131 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003132 scope_print_info(scope);
3133 }
Damien5ac1b2e2013-10-18 19:58:12 +01003134#endif
Damien429d7192013-10-04 19:53:11 +01003135
3136 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003137 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3138 assert(scope->kind == SCOPE_MODULE);
3139 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3140 compile_node(comp, pns->nodes[0]); // compile the expression
3141 EMIT(return_value);
3142 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003143 if (!comp->is_repl) {
3144 check_for_doc_string(comp, scope->pn);
3145 }
Damien429d7192013-10-04 19:53:11 +01003146 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003147 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003148 EMIT(return_value);
3149 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003150 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3151 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3152 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003153
3154 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003155 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003156 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003157 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003158 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003159 } else {
3160 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003161
Damien George2ac4af62014-08-15 16:45:41 +01003162 // argument annotations
3163 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3164
3165 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003166 mp_parse_node_t pn_annotation = pns->nodes[2];
3167 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3168 #if MICROPY_EMIT_NATIVE
3169 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3170 // nodes[2] can be null or a test-expr
3171 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3172 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3173 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3174 } else {
3175 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3176 }
Damien George2ac4af62014-08-15 16:45:41 +01003177 }
Damien Georgea5190a72014-08-15 22:39:08 +01003178 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003179 }
Damien George2ac4af62014-08-15 16:45:41 +01003180 }
Damien429d7192013-10-04 19:53:11 +01003181
3182 compile_node(comp, pns->nodes[3]); // 3 is function body
3183 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003184 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003185 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003186 EMIT(return_value);
3187 }
3188 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003189 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3190 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3191 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003192
3193 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003194 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003195 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003196 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003197 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3198 }
3199
3200 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003201
3202 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3203 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3204 EMIT(pop_top);
3205 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3206 }
Damien429d7192013-10-04 19:53:11 +01003207 EMIT(return_value);
3208 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3209 // a bit of a hack at the moment
3210
Damiend99b0522013-12-21 18:17:45 +00003211 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3212 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3213 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3214 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3215 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003216
Damien George708c0732014-04-27 19:23:46 +01003217 // We need a unique name for the comprehension argument (the iterator).
3218 // CPython uses .0, but we should be able to use anything that won't
3219 // clash with a user defined variable. Best to use an existing qstr,
3220 // so we use the blank qstr.
3221#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003222 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003223#else
3224 qstr qstr_arg = MP_QSTR_;
3225#endif
Damien George36db6bc2014-05-07 17:24:22 +01003226 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003227 bool added;
3228 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3229 assert(added);
3230 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003231 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003232 }
3233
3234 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003235 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003236 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003237 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003238 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003239 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003240 }
3241
Damien George6f355fd2014-04-10 14:11:31 +01003242 uint l_end = comp_next_label(comp);
3243 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003244 EMIT_ARG(load_id, qstr_arg);
3245 EMIT_ARG(label_assign, l_top);
3246 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003247 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3248 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003249 EMIT_ARG(jump, l_top);
3250 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003251 EMIT(for_iter_end);
3252
3253 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003254 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003255 }
3256 EMIT(return_value);
3257 } else {
3258 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003259 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3260 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3261 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003262
Damien George36db6bc2014-05-07 17:24:22 +01003263 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003264 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003265 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003266 assert(added);
3267 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003268 }
3269
Damien Georgeb9791222014-01-23 00:34:21 +00003270 EMIT_ARG(load_id, MP_QSTR___name__);
3271 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003272 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 +00003273 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003274
3275 check_for_doc_string(comp, pns->nodes[2]);
3276 compile_node(comp, pns->nodes[2]); // 2 is class body
3277
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003278 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003279 assert(id != NULL);
3280 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003281 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003282 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003283#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003284 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003285#else
Damien George2bf7c092014-04-09 15:26:46 +01003286 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003287#endif
Damien429d7192013-10-04 19:53:11 +01003288 }
3289 EMIT(return_value);
3290 }
3291
Damien415eb6f2013-10-05 12:19:06 +01003292 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003293
3294 // make sure we match all the exception levels
3295 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003296}
3297
Damien George094d4502014-04-02 17:31:27 +01003298#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003299// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003300STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003301 comp->pass = pass;
3302 comp->scope_cur = scope;
3303 comp->next_label = 1;
3304
3305 if (scope->kind != SCOPE_FUNCTION) {
3306 printf("Error: inline assembler must be a function\n");
3307 return;
3308 }
3309
Damien George36db6bc2014-05-07 17:24:22 +01003310 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003311 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003312 }
3313
Damien826005c2013-10-05 23:17:28 +01003314 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003315 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3316 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3317 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003318
Damiend99b0522013-12-21 18:17:45 +00003319 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003320
Damiena2f2f7d2013-10-06 00:14:13 +01003321 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003322 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003323 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003324 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003325 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003326 }
3327
Damiend99b0522013-12-21 18:17:45 +00003328 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003329
Damiend99b0522013-12-21 18:17:45 +00003330 mp_parse_node_t pn_body = pns->nodes[3]; // body
3331 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003332 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3333
Damien Georgecbd2f742014-01-19 11:48:48 +00003334 /*
Damien George36db6bc2014-05-07 17:24:22 +01003335 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003336 //printf("----\n");
3337 scope_print_info(scope);
3338 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003339 */
Damien826005c2013-10-05 23:17:28 +01003340
3341 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003342 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3343 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003344 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3345 // no instructions
3346 continue;
3347 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3348 // an instruction; fall through
3349 } else {
3350 // not an instruction; error
3351 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3352 return;
3353 }
Damiend99b0522013-12-21 18:17:45 +00003354 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3355 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3356 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3357 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3358 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3359 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3360 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3361 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3362 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3363 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003364 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3365
3366 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003367 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003368 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003369 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003370 return;
3371 }
Damien George6f355fd2014-04-10 14:11:31 +01003372 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003373 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003374 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003375 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003376 } else if (op == MP_QSTR_align) {
3377 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3378 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3379 return;
3380 }
Damien George36db6bc2014-05-07 17:24:22 +01003381 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003382 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3383 }
3384 } else if (op == MP_QSTR_data) {
3385 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3386 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3387 return;
3388 }
Damien George36db6bc2014-05-07 17:24:22 +01003389 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003390 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003391 for (uint i = 1; i < n_args; i++) {
3392 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3393 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3394 return;
3395 }
3396 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3397 }
3398 }
Damien826005c2013-10-05 23:17:28 +01003399 } else {
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(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003402 }
3403 }
3404 }
3405
Damien George36db6bc2014-05-07 17:24:22 +01003406 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003407 bool success = EMIT_INLINE_ASM(end_pass);
3408 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003409 // TODO get proper exception from inline assembler
3410 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003411 }
Damienb05d7072013-10-05 13:37:10 +01003412 }
Damien429d7192013-10-04 19:53:11 +01003413}
Damien George094d4502014-04-02 17:31:27 +01003414#endif
Damien429d7192013-10-04 19:53:11 +01003415
Damien George1463c1f2014-04-25 23:52:57 +01003416STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003417#if !MICROPY_EMIT_CPYTHON
3418 // in Micro Python we put the *x parameter after all other parameters (except **y)
3419 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3420 id_info_t *id_param = NULL;
3421 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3422 id_info_t *id = &scope->id_info[i];
3423 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3424 if (id_param != NULL) {
3425 // swap star param with last param
3426 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3427 }
3428 break;
3429 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3430 id_param = id;
3431 }
3432 }
3433 }
3434#endif
3435
Damien429d7192013-10-04 19:53:11 +01003436 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003437 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003438 scope->num_locals = 0;
3439 for (int i = 0; i < scope->id_info_len; i++) {
3440 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003441 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003442 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3443 continue;
3444 }
3445 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3446 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3447 }
Damien George2827d622014-04-27 15:50:52 +01003448 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003449 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003450 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003451 }
3452 }
3453
3454 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003455#if MICROPY_EMIT_CPYTHON
3456 int num_cell = 0;
3457#endif
Damien9ecbcff2013-12-11 00:41:43 +00003458 for (int i = 0; i < scope->id_info_len; i++) {
3459 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003460#if MICROPY_EMIT_CPYTHON
3461 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003462 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003463 id->local_num = num_cell;
3464 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003465 }
Damien George6baf76e2013-12-30 22:32:17 +00003466#else
3467 // in Micro Python the cells come right after the fast locals
3468 // parameters are not counted here, since they remain at the start
3469 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003470 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003471 id->local_num = scope->num_locals;
3472 scope->num_locals += 1;
3473 }
3474#endif
Damien9ecbcff2013-12-11 00:41:43 +00003475 }
Damien9ecbcff2013-12-11 00:41:43 +00003476
3477 // compute the index of free vars (freevars[idx] in CPython)
3478 // make sure they are in the order of the parent scope
3479 if (scope->parent != NULL) {
3480 int num_free = 0;
3481 for (int i = 0; i < scope->parent->id_info_len; i++) {
3482 id_info_t *id = &scope->parent->id_info[i];
3483 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3484 for (int j = 0; j < scope->id_info_len; j++) {
3485 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003486 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003487 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003488#if MICROPY_EMIT_CPYTHON
3489 // in CPython the frees are numbered after the cells
3490 id2->local_num = num_cell + num_free;
3491#else
3492 // in Micro Python the frees come first, before the params
3493 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003494#endif
3495 num_free += 1;
3496 }
3497 }
3498 }
Damien429d7192013-10-04 19:53:11 +01003499 }
Damien George6baf76e2013-12-30 22:32:17 +00003500#if !MICROPY_EMIT_CPYTHON
3501 // in Micro Python shift all other locals after the free locals
3502 if (num_free > 0) {
3503 for (int i = 0; i < scope->id_info_len; i++) {
3504 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003505 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003506 id->local_num += num_free;
3507 }
3508 }
Damien George2827d622014-04-27 15:50:52 +01003509 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 +00003510 scope->num_locals += num_free;
3511 }
3512#endif
Damien429d7192013-10-04 19:53:11 +01003513 }
3514
Damien George8725f8f2014-02-15 19:33:11 +00003515 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003516
3517#if MICROPY_EMIT_CPYTHON
3518 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003519 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) {
3520 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003521 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003522 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003523 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 +00003524 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003525 }
3526 }
Damien George882b3632014-04-02 15:56:31 +01003527#endif
3528
Damien429d7192013-10-04 19:53:11 +01003529 int num_free = 0;
3530 for (int i = 0; i < scope->id_info_len; i++) {
3531 id_info_t *id = &scope->id_info[i];
3532 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3533 num_free += 1;
3534 }
3535 }
3536 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003537 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003538 }
3539}
3540
Damien George65cad122014-04-06 11:48:15 +01003541mp_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 +01003542 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003543 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003544 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003545 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003546
Damien826005c2013-10-05 23:17:28 +01003547 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003548 mp_map_t consts;
3549 mp_map_init(&consts, 0);
3550 pn = fold_constants(comp, pn, &consts);
3551 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003552
3553 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003554 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003555
Damien826005c2013-10-05 23:17:28 +01003556 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003557 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003558 comp->emit_method_table = &emit_pass1_method_table;
3559 comp->emit_inline_asm = NULL;
3560 comp->emit_inline_asm_method_table = NULL;
3561 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003562 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003563 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003564#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003565 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003566 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003567#endif
Damien826005c2013-10-05 23:17:28 +01003568 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003569 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003570 }
3571
3572 // update maximim number of labels needed
3573 if (comp->next_label > max_num_labels) {
3574 max_num_labels = comp->next_label;
3575 }
Damien429d7192013-10-04 19:53:11 +01003576 }
3577
Damien826005c2013-10-05 23:17:28 +01003578 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003579 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003580 compile_scope_compute_things(comp, s);
3581 }
3582
Damien826005c2013-10-05 23:17:28 +01003583 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003584 emit_pass1_free(comp->emit);
3585
Damien826005c2013-10-05 23:17:28 +01003586 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003587#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003588 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003589#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003590 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003591#endif
Damien3ef4abb2013-10-12 16:53:13 +01003592#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003593 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003594#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003595#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003596 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003597 if (false) {
3598 // dummy
3599
Damien3ef4abb2013-10-12 16:53:13 +01003600#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003601 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003602 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003603 if (emit_inline_thumb == NULL) {
3604 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3605 }
3606 comp->emit = NULL;
3607 comp->emit_method_table = NULL;
3608 comp->emit_inline_asm = emit_inline_thumb;
3609 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003610 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003611 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003612 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003613 }
Damienc025ebb2013-10-12 14:30:21 +01003614#endif
3615
Damien826005c2013-10-05 23:17:28 +01003616 } else {
Damienc025ebb2013-10-12 14:30:21 +01003617
3618 // choose the emit type
3619
Damien3ef4abb2013-10-12 16:53:13 +01003620#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003621 comp->emit = emit_cpython_new(max_num_labels);
3622 comp->emit_method_table = &emit_cpython_method_table;
3623#else
Damien826005c2013-10-05 23:17:28 +01003624 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003625
3626#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003627 case MP_EMIT_OPT_NATIVE_PYTHON:
3628 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003629#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003630 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003631 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003632 }
Damien13ed3a62013-10-08 09:05:10 +01003633 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003634#elif MICROPY_EMIT_X86
3635 if (emit_native == NULL) {
3636 emit_native = emit_native_x86_new(max_num_labels);
3637 }
3638 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003639#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003640 if (emit_native == NULL) {
3641 emit_native = emit_native_thumb_new(max_num_labels);
3642 }
3643 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003644#elif MICROPY_EMIT_ARM
3645 if (emit_native == NULL) {
3646 emit_native = emit_native_arm_new(max_num_labels);
3647 }
3648 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003649#endif
3650 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003651 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 +01003652
3653 // native emitters need an extra pass to compute stack size
3654 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3655
Damien7af3d192013-10-07 00:02:49 +01003656 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003657#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003658
Damien826005c2013-10-05 23:17:28 +01003659 default:
3660 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003661 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003662 }
3663 comp->emit = emit_bc;
3664 comp->emit_method_table = &emit_bc_method_table;
3665 break;
3666 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003667#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003668
Damien George36db6bc2014-05-07 17:24:22 +01003669 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003670 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003671 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3672 }
3673
3674 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003675 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003676 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003677 }
Damien6cdd3af2013-10-05 18:08:26 +01003678 }
Damien429d7192013-10-04 19:53:11 +01003679 }
3680
Damien George41d02b62014-01-24 22:42:28 +00003681 // free the emitters
3682#if !MICROPY_EMIT_CPYTHON
3683 if (emit_bc != NULL) {
3684 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003685 }
Damien George41d02b62014-01-24 22:42:28 +00003686#if MICROPY_EMIT_NATIVE
3687 if (emit_native != NULL) {
3688#if MICROPY_EMIT_X64
3689 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003690#elif MICROPY_EMIT_X86
3691 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003692#elif MICROPY_EMIT_THUMB
3693 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003694#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003695 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003696#endif
3697 }
3698#endif
3699#if MICROPY_EMIT_INLINE_THUMB
3700 if (emit_inline_thumb != NULL) {
3701 emit_inline_thumb_free(emit_inline_thumb);
3702 }
3703#endif
3704#endif // !MICROPY_EMIT_CPYTHON
3705
Damien George52b5d762014-09-23 15:31:56 +00003706 // free the parse tree
3707 mp_parse_node_free(pn);
3708
Damien George41d02b62014-01-24 22:42:28 +00003709 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003710 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003711 for (scope_t *s = module_scope; s;) {
3712 scope_t *next = s->next;
3713 scope_free(s);
3714 s = next;
3715 }
Damien5ac1b2e2013-10-18 19:58:12 +01003716
Damien George41d02b62014-01-24 22:42:28 +00003717 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003718 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003719 m_del_obj(compiler_t, comp);
3720
Damien Georgea91ac202014-10-05 19:01:34 +01003721 if (compile_error != MP_OBJ_NULL) {
3722 return compile_error;
Damien George1fb03172014-01-03 14:22:03 +00003723 } else {
3724#if MICROPY_EMIT_CPYTHON
3725 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003726 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003727 return mp_const_true;
3728#else
3729 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003730 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003731#endif
3732 }
Damien429d7192013-10-04 19:53:11 +01003733}