blob: 25a4c960e7b659602db2fb8083db151ed78b9c70 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
Damiend99b0522013-12-21 18:17:45 +000034#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030035#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010037#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010038#include "parse.h"
Damiend99b0522013-12-21 18:17:45 +000039#include "runtime0.h"
Damien George1fb03172014-01-03 14:22:03 +000040#include "obj.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010041#include "emitglue.h"
42#include "scope.h"
43#include "emit.h"
Damien George1fb03172014-01-03 14:22:03 +000044#include "compile.h"
45#include "runtime.h"
Damien Georgea26dc502014-04-12 17:54:52 +010046#include "builtin.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000047#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010048
49// TODO need to mangle __attr names
50
51typedef enum {
52 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000053#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010054#include "grammar.h"
55#undef DEF_RULE
56 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010057 PN_string, // special node for non-interned string
Damien429d7192013-10-04 19:53:11 +010058} pn_kind_t;
59
Damien Georgeb9791222014-01-23 00:34:21 +000060#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
61#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
62#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
63#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010064
Damien Georgea91ac202014-10-05 19:01:34 +010065// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010066typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000067 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010068
Damien George78035b92014-04-09 12:27:39 +010069 uint8_t is_repl;
70 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010071 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010072 uint8_t have_star;
73
74 // try to keep compiler clean from nlr
75 // this is set to an exception object if we have a compile error
76 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010077
Damien George6f355fd2014-04-10 14:11:31 +010078 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010079
Damien George69b89d22014-04-11 13:38:30 +000080 uint16_t num_dict_params;
81 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010082
Damien Georgea91ac202014-10-05 19:01:34 +010083 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
84 uint16_t continue_label;
85 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000086 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010087
Damien429d7192013-10-04 19:53:11 +010088 scope_t *scope_head;
89 scope_t *scope_cur;
90
Damien6cdd3af2013-10-05 18:08:26 +010091 emit_t *emit; // current emitter
92 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010093
94 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
95 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010096} compiler_t;
97
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010098STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010099 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
100 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100101 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien Georgea91ac202014-10-05 19:01:34 +0100102 mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100103 } else {
Damien Georgea91ac202014-10-05 19:01:34 +0100104 // we don't have a line number, so just pass 0
105 mp_obj_exception_add_traceback(exc, comp->source_file, 0, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100106 }
Damien Georgea91ac202014-10-05 19:01:34 +0100107 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000108}
109
Damien George57e99eb2014-04-10 22:42:11 +0100110STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300111 #if MICROPY_PY_UCTYPES
112 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
113 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100114 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100115 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100116};
117
118STATIC const mp_map_t mp_constants_map = {
119 .all_keys_are_qstrs = 1,
120 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200121 .used = MP_ARRAY_SIZE(mp_constants_table),
122 .alloc = MP_ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +0100123 .table = (mp_map_elem_t*)mp_constants_table,
124};
125
Damien Georgeffae48d2014-05-08 15:58:39 +0000126// this function is essentially a simple preprocessor
127STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
128 if (0) {
129 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100130#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000131 } else if (MP_PARSE_NODE_IS_ID(pn)) {
132 // lookup identifier in table of dynamic constants
133 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
134 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
135 if (elem != NULL) {
136 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
137 }
138#endif
139 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000140 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100141
Damien Georgeffae48d2014-05-08 15:58:39 +0000142 // fold some parse nodes before folding their arguments
143 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100144#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000145 case PN_expr_stmt:
146 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
147 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
148 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
149 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
150 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
151 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
152 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
153 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
154 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
155 ) {
156 // code to assign dynamic constants: id = const(value)
157
158 // get the id
159 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
160
161 // get the value
162 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
163 pn_value = fold_constants(comp, pn_value, consts);
164 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
165 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
166 break;
167 }
Damien George40f3c022014-07-03 13:25:24 +0100168 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000169
170 // store the value in the table of dynamic constants
171 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
172 if (elem->value != MP_OBJ_NULL) {
173 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
174 break;
175 }
176 elem->value = MP_OBJ_NEW_SMALL_INT(value);
177
178 // replace const(value) with value
179 pns1->nodes[0] = pn_value;
180
181 // finished folding this assignment
182 return pn;
183 }
184 }
185 }
186 break;
187#endif
Damien George5042bce2014-05-25 22:06:06 +0100188 case PN_string:
189 return pn;
Damien429d7192013-10-04 19:53:11 +0100190 }
191
Damien Georgeffae48d2014-05-08 15:58:39 +0000192 // fold arguments
193 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
194 for (int i = 0; i < n; i++) {
195 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
196 }
197
198 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000199 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100200 case PN_atom_paren:
201 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
202 // (int)
203 pn = pns->nodes[0];
204 }
205 break;
206
207 case PN_expr:
208 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
209 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100210 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
211 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100212 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
213 }
214 break;
215
216 case PN_and_expr:
217 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
218 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100219 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
220 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100221 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
222 }
223 break;
224
Damien429d7192013-10-04 19:53:11 +0100225 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000226 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100227 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
228 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000229 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100230 // int << int
231 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
232 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
233 }
Damiend99b0522013-12-21 18:17:45 +0000234 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100235 // int >> int
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200236 if (arg1 >= BITS_PER_WORD) {
237 // Shifting to big amounts is underfined behavior
238 // in C and is CPU-dependent; propagate sign bit.
239 arg1 = BITS_PER_WORD - 1;
240 }
Damiend99b0522013-12-21 18:17:45 +0000241 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100242 } else {
243 // shouldn't happen
244 assert(0);
245 }
246 }
247 break;
248
249 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100250 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000251 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100252 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
253 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000254 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100255 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000256 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000257 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100258 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000259 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100260 } else {
261 // shouldn't happen
262 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100263 }
Damien Georged1e355e2014-05-28 14:51:12 +0100264 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100265 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000266 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100267 }
268 }
269 break;
270
271 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000272 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100273 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
274 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000275 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100276 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000277 if (!mp_small_int_mul_overflow(arg0, arg1)) {
278 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100279 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000280 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
281 }
282 }
Damiend99b0522013-12-21 18:17:45 +0000283 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100284 // int / int
285 // pass
Damiend99b0522013-12-21 18:17:45 +0000286 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100287 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000288 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000289 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300290 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100291 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000292 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300293 }
Damien429d7192013-10-04 19:53:11 +0100294 } else {
295 // shouldn't happen
296 assert(0);
297 }
298 }
299 break;
300
301 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000302 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100303 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000304 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100305 // +int
Damiend99b0522013-12-21 18:17:45 +0000306 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
307 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100308 // -int
Damiend99b0522013-12-21 18:17:45 +0000309 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
310 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100311 // ~int
Damiend99b0522013-12-21 18:17:45 +0000312 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100313 } else {
314 // shouldn't happen
315 assert(0);
316 }
317 }
318 break;
319
320 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100321 if (0) {
322#if MICROPY_EMIT_CPYTHON
323 } else if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100324 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100325 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000326 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
327 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200328 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100329 if (power >= 0) {
330 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200331 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100332 for (; power > 0; power--) {
333 ans *= base;
334 }
Damiend99b0522013-12-21 18:17:45 +0000335 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100336 }
337 }
Damien George57e99eb2014-04-10 22:42:11 +0100338#endif
339 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
340 // id.id
341 // look it up in constant table, see if it can be replaced with an integer
342 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
343 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
344 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
345 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
346 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
347 if (elem != NULL) {
348 mp_obj_t dest[2];
349 mp_load_method_maybe(elem->value, q_attr, dest);
350 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100351 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georged1e355e2014-05-28 14:51:12 +0100352 if (MP_SMALL_INT_FITS(val)) {
Damien George57e99eb2014-04-10 22:42:11 +0100353 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
354 }
355 }
356 }
Damien429d7192013-10-04 19:53:11 +0100357 }
358 break;
359 }
360 }
361
362 return pn;
363}
364
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200365STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George6be0b0a2014-08-15 14:30:52 +0100366STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000367STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100368
Damien George6f355fd2014-04-10 14:11:31 +0100369STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100370 return comp->next_label++;
371}
372
Damien George8dcc0c72014-03-27 10:55:21 +0000373STATIC void compile_increase_except_level(compiler_t *comp) {
374 comp->cur_except_level += 1;
375 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
376 comp->scope_cur->exc_stack_size = comp->cur_except_level;
377 }
378}
379
380STATIC void compile_decrease_except_level(compiler_t *comp) {
381 assert(comp->cur_except_level > 0);
382 comp->cur_except_level -= 1;
383}
384
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200385STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgedf8127a2014-04-13 11:04:33 +0100386 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100387 scope->parent = comp->scope_cur;
388 scope->next = NULL;
389 if (comp->scope_head == NULL) {
390 comp->scope_head = scope;
391 } else {
392 scope_t *s = comp->scope_head;
393 while (s->next != NULL) {
394 s = s->next;
395 }
396 s->next = scope;
397 }
398 return scope;
399}
400
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200401STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000402 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
403 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
404 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100405 for (int i = 0; i < num_nodes; i++) {
406 f(comp, pns->nodes[i]);
407 }
Damiend99b0522013-12-21 18:17:45 +0000408 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100409 f(comp, pn);
410 }
411}
412
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200413STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000414 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100415 *nodes = NULL;
416 return 0;
Damiend99b0522013-12-21 18:17:45 +0000417 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100418 *nodes = pn;
419 return 1;
420 } else {
Damiend99b0522013-12-21 18:17:45 +0000421 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
422 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100423 *nodes = pn;
424 return 1;
425 } else {
426 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000427 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100428 }
429 }
430}
431
Damien George969a6b32014-12-10 22:07:04 +0000432STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000433 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100434 for (int i = 0; i < num_nodes; i++) {
435 compile_node(comp, pns->nodes[i]);
436 }
437}
438
Damien3ef4abb2013-10-12 16:53:13 +0100439#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200440STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100441 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
442 return true;
443 }
Damiend99b0522013-12-21 18:17:45 +0000444 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100445 return false;
446 }
Damiend99b0522013-12-21 18:17:45 +0000447 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100448 return false;
449 }
450 return true;
451}
452
Damien George5042bce2014-05-25 22:06:06 +0100453STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000454 bool has_single_quote = false;
455 bool has_double_quote = false;
456 for (int i = 0; i < len; i++) {
457 if (str[i] == '\'') {
458 has_single_quote = true;
459 } else if (str[i] == '"') {
460 has_double_quote = true;
461 }
462 }
463 if (bytes) {
464 vstr_printf(vstr, "b");
465 }
466 bool quote_single = false;
467 if (has_single_quote && !has_double_quote) {
468 vstr_printf(vstr, "\"");
469 } else {
470 quote_single = true;
471 vstr_printf(vstr, "'");
472 }
473 for (int i = 0; i < len; i++) {
474 if (str[i] == '\n') {
475 vstr_printf(vstr, "\\n");
476 } else if (str[i] == '\\') {
477 vstr_printf(vstr, "\\\\");
478 } else if (str[i] == '\'' && quote_single) {
479 vstr_printf(vstr, "\\'");
480 } else {
481 vstr_printf(vstr, "%c", str[i]);
482 }
483 }
484 if (has_single_quote && !has_double_quote) {
485 vstr_printf(vstr, "\"");
486 } else {
487 vstr_printf(vstr, "'");
488 }
489}
490
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200491STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George5042bce2014-05-25 22:06:06 +0100492 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
493 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George40f3c022014-07-03 13:25:24 +0100494 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 +0100495 return;
496 }
497
Damiend99b0522013-12-21 18:17:45 +0000498 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200499 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
500 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
501 return;
502 }
503
Damien George42f3de92014-10-03 17:44:14 +0000504 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000505 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
506 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000507 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
508 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100509 case MP_PARSE_NODE_STRING:
510 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100511 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100512 const byte *str = qstr_data(arg, &len);
513 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
514 break;
515 }
Damiend99b0522013-12-21 18:17:45 +0000516 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100517 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000518 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
519 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
520 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100521 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100522 }
523 break;
524 default: assert(0);
525 }
526}
527
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200528STATIC 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 +0100529 int n = 0;
530 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000531 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100532 }
533 int total = n;
534 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000535 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100536 total += 1;
Damien3a205172013-10-12 15:01:56 +0100537 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100538 is_const = false;
539 }
540 }
541 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100542 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100543 is_const = false;
544 break;
545 }
546 }
547 if (total > 0 && is_const) {
548 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000549 vstr_t *vstr = vstr_new();
550 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000551 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000552 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100553 need_comma = true;
554 }
555 for (int i = 0; i < n; i++) {
556 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000557 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100558 }
Damien02f89412013-12-12 15:13:36 +0000559 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100560 need_comma = true;
561 }
562 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000563 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100564 } else {
Damien02f89412013-12-12 15:13:36 +0000565 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100566 }
Damien Georgeb9791222014-01-23 00:34:21 +0000567 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000568 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100569 } else {
Damiend99b0522013-12-21 18:17:45 +0000570 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100571 compile_node(comp, pn);
572 }
573 for (int i = 0; i < n; i++) {
574 compile_node(comp, pns_list->nodes[i]);
575 }
Damien Georgeb9791222014-01-23 00:34:21 +0000576 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100577 }
578}
Damien3a205172013-10-12 15:01:56 +0100579#endif
580
581// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100582STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100583#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100584 cpython_c_tuple(comp, pn, pns_list);
585#else
586 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000587 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100588 compile_node(comp, pn);
589 total += 1;
590 }
591 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000592 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100593 for (int i = 0; i < n; i++) {
594 compile_node(comp, pns_list->nodes[i]);
595 }
596 total += n;
597 }
Damien Georgeb9791222014-01-23 00:34:21 +0000598 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100599#endif
600}
Damien429d7192013-10-04 19:53:11 +0100601
Damien George969a6b32014-12-10 22:07:04 +0000602STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100603 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000604 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100605}
606
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200607STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000608 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
609 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100610}
611
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200612STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000613 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
614 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100615}
616
Damien3ef4abb2013-10-12 16:53:13 +0100617#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100618// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200619STATIC 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 +0100620 if (node_is_const_false(pn)) {
621 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000622 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100623 }
624 return;
625 } else if (node_is_const_true(pn)) {
626 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000627 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100628 }
629 return;
Damiend99b0522013-12-21 18:17:45 +0000630 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
631 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
632 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
633 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100634 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100635 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100636 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100637 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100638 }
Damien3a205172013-10-12 15:01:56 +0100639 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000640 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100641 } else {
642 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100643 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100644 }
645 }
646 return;
Damiend99b0522013-12-21 18:17:45 +0000647 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100648 if (jump_if == false) {
649 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100650 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100651 }
652 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100653 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100654 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100655 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100656 }
Damien3a205172013-10-12 15:01:56 +0100657 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000658 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100659 }
660 return;
Damiend99b0522013-12-21 18:17:45 +0000661 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100662 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100663 return;
664 }
665 }
666
667 // nothing special, fall back to default compiling for node and jump
668 compile_node(comp, pn);
669 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000670 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100671 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000672 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100673 }
674}
Damien3a205172013-10-12 15:01:56 +0100675#endif
Damien429d7192013-10-04 19:53:11 +0100676
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200677STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100678#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100679 cpython_c_if_cond(comp, pn, jump_if, label, false);
680#else
681 if (node_is_const_false(pn)) {
682 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000683 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100684 }
685 return;
686 } else if (node_is_const_true(pn)) {
687 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000688 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100689 }
690 return;
Damiend99b0522013-12-21 18:17:45 +0000691 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
692 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
693 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
694 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100695 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100696 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100697 for (int i = 0; i < n - 1; i++) {
698 c_if_cond(comp, pns->nodes[i], true, label2);
699 }
700 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000701 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100702 } else {
703 for (int i = 0; i < n; i++) {
704 c_if_cond(comp, pns->nodes[i], true, label);
705 }
706 }
707 return;
Damiend99b0522013-12-21 18:17:45 +0000708 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100709 if (jump_if == false) {
710 for (int i = 0; i < n; i++) {
711 c_if_cond(comp, pns->nodes[i], false, label);
712 }
713 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100714 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100715 for (int i = 0; i < n - 1; i++) {
716 c_if_cond(comp, pns->nodes[i], false, label2);
717 }
718 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000719 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100720 }
721 return;
Damiend99b0522013-12-21 18:17:45 +0000722 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100723 c_if_cond(comp, pns->nodes[0], !jump_if, label);
724 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100725 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
726 // cond is something in parenthesis
727 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
728 // empty tuple, acts as false for the condition
729 if (jump_if == false) {
730 EMIT_ARG(jump, label);
731 }
732 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
733 // non-empty tuple, acts as true for the condition
734 if (jump_if == true) {
735 EMIT_ARG(jump, label);
736 }
737 } else {
738 // parenthesis around 1 item, is just that item
739 c_if_cond(comp, pns->nodes[0], jump_if, label);
740 }
741 return;
Damien3a205172013-10-12 15:01:56 +0100742 }
743 }
744
745 // nothing special, fall back to default compiling for node and jump
746 compile_node(comp, pn);
747 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000748 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100749 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000750 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100751 }
752#endif
Damien429d7192013-10-04 19:53:11 +0100753}
754
755typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100756STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100757
Damien George6be0b0a2014-08-15 14:30:52 +0100758STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100759 if (assign_kind != ASSIGN_AUG_STORE) {
760 compile_node(comp, pns->nodes[0]);
761 }
762
Damiend99b0522013-12-21 18:17:45 +0000763 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
764 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
765 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
766 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100767 if (assign_kind != ASSIGN_AUG_STORE) {
768 for (int i = 0; i < n - 1; i++) {
769 compile_node(comp, pns1->nodes[i]);
770 }
771 }
Damiend99b0522013-12-21 18:17:45 +0000772 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
773 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100774 }
Damiend99b0522013-12-21 18:17:45 +0000775 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100776 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000777 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100778 if (assign_kind == ASSIGN_AUG_STORE) {
779 EMIT(rot_three);
780 EMIT(store_subscr);
781 } else {
782 compile_node(comp, pns1->nodes[0]);
783 if (assign_kind == ASSIGN_AUG_LOAD) {
784 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100785 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100786 } else {
787 EMIT(store_subscr);
788 }
789 }
Damiend99b0522013-12-21 18:17:45 +0000790 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
791 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100792 if (assign_kind == ASSIGN_AUG_LOAD) {
793 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000794 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100795 } else {
796 if (assign_kind == ASSIGN_AUG_STORE) {
797 EMIT(rot_two);
798 }
Damien Georgeb9791222014-01-23 00:34:21 +0000799 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
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 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100805 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100806 }
807
Damiend99b0522013-12-21 18:17:45 +0000808 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100809 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100810 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100811
812 return;
813
814cannot_assign:
815 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100816}
817
Damien George0288cf02014-04-11 11:53:00 +0000818// 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 +0100819STATIC 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 +0000820 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
821
822 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100823 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000824 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
825 EMIT_ARG(unpack_ex, 0, num_tail);
826 have_star_index = 0;
827 }
828 for (int i = 0; i < num_tail; i++) {
829 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100830 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000831 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
832 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100833 } else {
Damien George9d181f62014-04-27 16:55:27 +0100834 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100835 return;
836 }
837 }
838 }
839 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000840 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100841 }
Damien George0288cf02014-04-11 11:53:00 +0000842 if (num_head != 0) {
843 if (0 == have_star_index) {
844 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100845 } else {
Damien George0288cf02014-04-11 11:53:00 +0000846 c_assign(comp, node_head, ASSIGN_STORE);
847 }
848 }
849 for (int i = 0; i < num_tail; i++) {
850 if (num_head + i == have_star_index) {
851 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
852 } else {
853 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100854 }
855 }
856}
857
858// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100859STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100860 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000861 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100862 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000863 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
864 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000865 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100866 switch (assign_kind) {
867 case ASSIGN_STORE:
868 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000869 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100870 break;
871 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000872 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100873 break;
874 }
875 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100876 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100877 return;
878 }
879 } else {
Damiend99b0522013-12-21 18:17:45 +0000880 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
881 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100882 case PN_power:
883 // lhs is an index or attribute
884 c_assign_power(comp, pns, assign_kind);
885 break;
886
887 case PN_testlist_star_expr:
888 case PN_exprlist:
889 // lhs is a tuple
890 if (assign_kind != ASSIGN_STORE) {
891 goto bad_aug;
892 }
Damien George0288cf02014-04-11 11:53:00 +0000893 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100894 break;
895
896 case PN_atom_paren:
897 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000898 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100899 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100900 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000901 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
902 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100903 goto testlist_comp;
904 } else {
905 // parenthesis around 1 item, is just that item
906 pn = pns->nodes[0];
907 goto tail_recursion;
908 }
909 break;
910
911 case PN_atom_bracket:
912 // lhs is something in brackets
913 if (assign_kind != ASSIGN_STORE) {
914 goto bad_aug;
915 }
Damiend99b0522013-12-21 18:17:45 +0000916 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100917 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000918 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000919 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
920 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100921 goto testlist_comp;
922 } else {
923 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000924 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100925 }
926 break;
927
928 default:
Damien George9d181f62014-04-27 16:55:27 +0100929 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100930 }
931 return;
932
933 testlist_comp:
934 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000935 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
936 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
937 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100938 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000939 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000940 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000941 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100942 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000943 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
944 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000945 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100946 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100947 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100948 } else {
949 // sequence with 2 items
950 goto sequence_with_2_items;
951 }
952 } else {
953 // sequence with 2 items
954 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000955 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100956 }
957 return;
958 }
959 return;
960
Damien George9d181f62014-04-27 16:55:27 +0100961 cannot_assign:
962 compile_syntax_error(comp, pn, "can't assign to expression");
963 return;
964
Damien429d7192013-10-04 19:53:11 +0100965 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100966 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100967}
968
969// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100970// if we are not in CPython compatibility mode then:
971// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
972// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
973// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100974STATIC 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 +0100975 assert(n_pos_defaults >= 0);
976 assert(n_kw_defaults >= 0);
977
Damien429d7192013-10-04 19:53:11 +0100978 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000979 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100980 int nfree = 0;
981 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000982 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
983 id_info_t *id = &comp->scope_cur->id_info[i];
984 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
985 for (int j = 0; j < this_scope->id_info_len; j++) {
986 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100987 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000988#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100989 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000990#else
991 // in Micro Python we load closures using LOAD_FAST
Damien George7ff996c2014-09-08 23:05:16 +0100992 EMIT_ARG(load_fast, id->qst, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000993#endif
Damien318aec62013-12-10 18:28:17 +0000994 nfree += 1;
995 }
996 }
Damien429d7192013-10-04 19:53:11 +0100997 }
998 }
999 }
Damien429d7192013-10-04 19:53:11 +01001000
1001 // make the function/closure
1002 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001003 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001004 } else {
Damien George3558f622014-04-20 17:50:40 +01001005 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001006 }
1007}
1008
Damien George6be0b0a2014-08-15 14:30:52 +01001009STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001010 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001011 comp->have_star = true;
1012 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001013 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1014 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001015 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001016 } else {
1017 // named star
Damien429d7192013-10-04 19:53:11 +01001018 }
Damien George8b19db02014-04-11 23:25:34 +01001019 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001020
1021 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001022 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001023 // TODO do we need to do anything with this?
1024
1025 } else {
1026 mp_parse_node_t pn_id;
1027 mp_parse_node_t pn_colon;
1028 mp_parse_node_t pn_equal;
1029 if (MP_PARSE_NODE_IS_ID(pn)) {
1030 // this parameter is just an id
1031
1032 pn_id = pn;
1033 pn_colon = MP_PARSE_NODE_NULL;
1034 pn_equal = MP_PARSE_NODE_NULL;
1035
1036 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1037 // this parameter has a colon and/or equal specifier
1038
1039 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1040 pn_id = pns->nodes[0];
1041 pn_colon = pns->nodes[1];
1042 pn_equal = pns->nodes[2];
1043
1044 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001045 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001046 assert(0);
1047 return;
1048 }
1049
1050 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1051 // this parameter does not have a default value
1052
1053 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001054 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001055 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001056 return;
1057 }
1058
1059 } else {
1060 // this parameter has a default value
1061 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1062
Damien George8b19db02014-04-11 23:25:34 +01001063 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001064 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001065#if MICROPY_EMIT_CPYTHON
1066 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1067 compile_node(comp, pn_equal);
1068#else
Damien George69b89d22014-04-11 13:38:30 +00001069 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1070 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001071 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1072 // we need to do this here before we start building the map for the default keywords
1073 if (comp->num_default_params > 0) {
1074 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001075 } else {
1076 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001077 }
Damien George69b89d22014-04-11 13:38:30 +00001078 // first default dict param, so make the map
1079 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001080 }
Damien Georgef0778a72014-06-07 22:01:00 +01001081
1082 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001083 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001084 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001085 EMIT(store_map);
1086#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001087 } else {
Damien George69b89d22014-04-11 13:38:30 +00001088 comp->num_default_params += 1;
1089 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001090 }
1091 }
1092
1093 // TODO pn_colon not implemented
1094 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001095 }
1096}
1097
1098// leaves function object on stack
1099// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001100STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001101 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001102 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001103 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001104 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001105 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001106 }
1107
1108 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001109 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001110 uint old_num_dict_params = comp->num_dict_params;
1111 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001112
1113 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001114 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001115 comp->num_dict_params = 0;
1116 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001117 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001118
Damien Georgea91ac202014-10-05 19:01:34 +01001119 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001120 return MP_QSTR_NULL;
1121 }
1122
Damien Georgee337f1e2014-03-31 15:18:37 +01001123#if !MICROPY_EMIT_CPYTHON
1124 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001125 // the default keywords args may have already made the tuple; if not, do it now
1126 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001127 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001128 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001129 }
1130#endif
1131
Damien429d7192013-10-04 19:53:11 +01001132 // get the scope for this function
1133 scope_t *fscope = (scope_t*)pns->nodes[4];
1134
1135 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001136 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001137
1138 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001139 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001140 comp->num_dict_params = old_num_dict_params;
1141 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001142
1143 // return its name (the 'f' in "def f(...):")
1144 return fscope->simple_name;
1145}
1146
1147// leaves class object on stack
1148// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001149STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001150 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001151 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001152 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001153 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001154 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001155 }
1156
1157 EMIT(load_build_class);
1158
1159 // scope for this class
1160 scope_t *cscope = (scope_t*)pns->nodes[3];
1161
1162 // compile the class
1163 close_over_variables_etc(comp, cscope, 0, 0);
1164
1165 // get its name
Damien George968bf342014-04-27 19:12:05 +01001166 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001167
1168 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001169 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1170 mp_parse_node_t parents = pns->nodes[1];
1171 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1172 parents = MP_PARSE_NODE_NULL;
1173 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001174 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001175 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001176
1177 // return its name (the 'C' in class C(...):")
1178 return cscope->simple_name;
1179}
1180
Damien6cdd3af2013-10-05 18:08:26 +01001181// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001182STATIC 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 +00001183 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001184 return false;
1185 }
1186
1187 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001188 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001189 return true;
1190 }
1191
Damiend99b0522013-12-21 18:17:45 +00001192 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001193 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001194 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001195#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001196 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001197 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001198 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001199 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001200#endif
Damien3ef4abb2013-10-12 16:53:13 +01001201#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001202 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001203 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001204#endif
Damien6cdd3af2013-10-05 18:08:26 +01001205 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001206 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001207 }
1208
1209 return true;
1210}
1211
Damien George969a6b32014-12-10 22:07:04 +00001212STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001213 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001214 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001215 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1216
Damien6cdd3af2013-10-05 18:08:26 +01001217 // inherit emit options for this function/class definition
1218 uint emit_options = comp->scope_cur->emit_options;
1219
1220 // compile each decorator
1221 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001222 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001223 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1224 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001225
1226 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001227 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001228 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1229
1230 // check for built-in decorators
1231 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1232 // this was a built-in
1233 num_built_in_decorators += 1;
1234
1235 } else {
1236 // not a built-in, compile normally
1237
1238 // compile the decorator function
1239 compile_node(comp, name_nodes[0]);
1240 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001241 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001242 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001243 }
1244
1245 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001246 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001247 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001248 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001249 compile_node(comp, pns_decorator->nodes[1]);
1250 }
Damien429d7192013-10-04 19:53:11 +01001251 }
1252 }
1253
1254 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001255 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001256 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001257 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001258 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001259 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001260 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001261 } else {
1262 // shouldn't happen
1263 assert(0);
1264 }
1265
1266 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001267 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001268 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001269 }
1270
1271 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001272 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001273}
1274
Damien George969a6b32014-12-10 22:07:04 +00001275STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001276 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001277 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001278 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001279}
1280
Damien George6be0b0a2014-08-15 14:30:52 +01001281STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001282 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001283 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001284 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1285 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001286
1287 compile_node(comp, pns->nodes[0]); // base of the power node
1288
Damiend99b0522013-12-21 18:17:45 +00001289 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1290 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1291 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1292 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001293 for (int i = 0; i < n - 1; i++) {
1294 compile_node(comp, pns1->nodes[i]);
1295 }
Damiend99b0522013-12-21 18:17:45 +00001296 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1297 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001298 }
Damiend99b0522013-12-21 18:17:45 +00001299 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001300 // can't delete function calls
1301 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001302 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001303 compile_node(comp, pns1->nodes[0]);
1304 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001305 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1306 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001307 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001308 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001309 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001310 }
1311 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001312 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001313 }
1314
Damiend99b0522013-12-21 18:17:45 +00001315 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001316 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001317 }
Damiend99b0522013-12-21 18:17:45 +00001318 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1319 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1320 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1321 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001322 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1323
Damiend99b0522013-12-21 18:17:45 +00001324 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1325 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1326 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001327 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001328 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001329 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001330 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001331 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001332 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001333 c_del_stmt(comp, pns->nodes[0]);
1334 for (int i = 0; i < n; i++) {
1335 c_del_stmt(comp, pns1->nodes[i]);
1336 }
Damiend99b0522013-12-21 18:17:45 +00001337 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001338 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001339 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001340 } else {
1341 // sequence with 2 items
1342 goto sequence_with_2_items;
1343 }
1344 } else {
1345 // sequence with 2 items
1346 sequence_with_2_items:
1347 c_del_stmt(comp, pns->nodes[0]);
1348 c_del_stmt(comp, pns->nodes[1]);
1349 }
1350 } else {
1351 // tuple with 1 element
1352 c_del_stmt(comp, pn);
1353 }
1354 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001355 // TODO is there anything else to implement?
1356 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001357 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001358
1359 return;
1360
1361cannot_delete:
1362 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001363}
1364
Damien George969a6b32014-12-10 22:07:04 +00001365STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001366 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1367}
1368
Damien George969a6b32014-12-10 22:07:04 +00001369STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001370 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001371 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001372 }
Damien George090c9232014-10-17 14:08:49 +00001373 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001374 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001375}
1376
Damien George969a6b32014-12-10 22:07:04 +00001377STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001378 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001379 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001380 }
Damien George090c9232014-10-17 14:08:49 +00001381 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001382 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001383}
1384
Damien George969a6b32014-12-10 22:07:04 +00001385STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001386 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001387 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001388 return;
1389 }
Damiend99b0522013-12-21 18:17:45 +00001390 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001391 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001392 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001393 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001394 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001395 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1396 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 +01001397
Damien George6f355fd2014-04-10 14:11:31 +01001398 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001399 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1400 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1401 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001402 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001403 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1404 } else {
1405 compile_node(comp, pns->nodes[0]);
1406 }
1407 EMIT(return_value);
1408}
1409
Damien George969a6b32014-12-10 22:07:04 +00001410STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001411 compile_node(comp, pns->nodes[0]);
1412 EMIT(pop_top);
1413}
1414
Damien George969a6b32014-12-10 22:07:04 +00001415STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001416 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001417 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001418 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001419 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001420 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001421 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001422 compile_node(comp, pns->nodes[0]);
1423 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001424 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001425 } else {
1426 // raise x
1427 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001428 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001429 }
1430}
1431
Damien George635543c2014-04-10 12:56:52 +01001432// q_base holds the base of the name
1433// eg a -> q_base=a
1434// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001435STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001436 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001437 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1438 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001439 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001440 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001441 pn = pns->nodes[0];
1442 is_as = true;
1443 }
Damien George635543c2014-04-10 12:56:52 +01001444 if (MP_PARSE_NODE_IS_NULL(pn)) {
1445 // empty name (eg, from . import x)
1446 *q_base = MP_QSTR_;
1447 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1448 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001449 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001450 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001451 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001452 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001453 }
Damien George635543c2014-04-10 12:56:52 +01001454 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001455 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1456 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1457 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001458 // a name of the form a.b.c
1459 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001460 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001461 }
Damiend99b0522013-12-21 18:17:45 +00001462 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001463 int len = n - 1;
1464 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001465 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001466 }
Damien George55baff42014-01-21 21:40:13 +00001467 byte *q_ptr;
1468 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001469 for (int i = 0; i < n; i++) {
1470 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001471 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001472 }
Damien George39dc1452014-10-03 19:52:22 +01001473 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001474 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001475 memcpy(str_dest, str_src, str_src_len);
1476 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001477 }
Damien George635543c2014-04-10 12:56:52 +01001478 qstr q_full = qstr_build_end(q_ptr);
1479 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001480 if (is_as) {
1481 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001482 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001483 }
1484 }
1485 } else {
Damien George635543c2014-04-10 12:56:52 +01001486 // shouldn't happen
1487 assert(0);
Damien429d7192013-10-04 19:53:11 +01001488 }
1489 } else {
Damien George635543c2014-04-10 12:56:52 +01001490 // shouldn't happen
1491 assert(0);
Damien429d7192013-10-04 19:53:11 +01001492 }
1493}
1494
Damien George6be0b0a2014-08-15 14:30:52 +01001495STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001496 EMIT_ARG(load_const_small_int, 0); // level 0 import
1497 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1498 qstr q_base;
1499 do_import_name(comp, pn, &q_base);
1500 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001501}
1502
Damien George969a6b32014-12-10 22:07:04 +00001503STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001504 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1505}
1506
Damien George969a6b32014-12-10 22:07:04 +00001507STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001508 mp_parse_node_t pn_import_source = pns->nodes[0];
1509
1510 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1511 uint import_level = 0;
1512 do {
1513 mp_parse_node_t pn_rel;
1514 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)) {
1515 // This covers relative imports with dots only like "from .. import"
1516 pn_rel = pn_import_source;
1517 pn_import_source = MP_PARSE_NODE_NULL;
1518 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1519 // This covers relative imports starting with dot(s) like "from .foo import"
1520 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1521 pn_rel = pns_2b->nodes[0];
1522 pn_import_source = pns_2b->nodes[1];
1523 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1524 } else {
1525 // Not a relative import
1526 break;
1527 }
1528
1529 // get the list of . and/or ...'s
1530 mp_parse_node_t *nodes;
1531 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1532
1533 // count the total number of .'s
1534 for (int i = 0; i < n; i++) {
1535 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1536 import_level++;
1537 } else {
1538 // should be an MP_TOKEN_ELLIPSIS
1539 import_level += 3;
1540 }
1541 }
1542 } while (0);
1543
Damiend99b0522013-12-21 18:17:45 +00001544 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001545 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001546
1547 // build the "fromlist" tuple
1548#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001549 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001550#else
Damien George708c0732014-04-27 19:23:46 +01001551 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001552 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001553#endif
1554
1555 // do the import
Damien George635543c2014-04-10 12:56:52 +01001556 qstr dummy_q;
1557 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001558 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001559
Damien429d7192013-10-04 19:53:11 +01001560 } else {
Damien George635543c2014-04-10 12:56:52 +01001561 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001562
1563 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001564 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001565 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001566#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001567 {
1568 vstr_t *vstr = vstr_new();
1569 vstr_printf(vstr, "(");
1570 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001571 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1572 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1573 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001574 if (i > 0) {
1575 vstr_printf(vstr, ", ");
1576 }
1577 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001578 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001579 const byte *str = qstr_data(id2, &len);
1580 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001581 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001582 }
Damien02f89412013-12-12 15:13:36 +00001583 if (n == 1) {
1584 vstr_printf(vstr, ",");
1585 }
1586 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001587 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001588 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001589 }
Damiendb4c3612013-12-10 17:27:24 +00001590#else
1591 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001592 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1593 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1594 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001595 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001596 }
Damien Georgeb9791222014-01-23 00:34:21 +00001597 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001598#endif
1599
1600 // do the import
Damien George635543c2014-04-10 12:56:52 +01001601 qstr dummy_q;
1602 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001603 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001604 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1605 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1606 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001607 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001608 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001609 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001610 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001611 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001612 }
1613 }
1614 EMIT(pop_top);
1615 }
1616}
1617
Damien George969a6b32014-12-10 22:07:04 +00001618STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001619 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001620 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1621 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001622 } else {
Damiend99b0522013-12-21 18:17:45 +00001623 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1624 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001625 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001626 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001627 }
Damien429d7192013-10-04 19:53:11 +01001628 }
1629 }
1630}
1631
Damien George969a6b32014-12-10 22:07:04 +00001632STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001633 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001634 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1635 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001636 } else {
Damiend99b0522013-12-21 18:17:45 +00001637 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1638 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001639 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001640 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001641 }
Damien429d7192013-10-04 19:53:11 +01001642 }
1643 }
1644}
1645
Damien George969a6b32014-12-10 22:07:04 +00001646STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001647 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001648 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001649 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 +00001650 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001651 // assertion message
1652 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001653 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001654 }
Damien Georgeb9791222014-01-23 00:34:21 +00001655 EMIT_ARG(raise_varargs, 1);
1656 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001657}
1658
Damien George969a6b32014-12-10 22:07:04 +00001659STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001660 // TODO proper and/or short circuiting
1661
Damien George6f355fd2014-04-10 14:11:31 +01001662 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001663
Damien George391db862014-10-17 17:57:33 +00001664 // optimisation: don't emit anything when "if False" (not in CPython)
1665 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1666 uint l_fail = comp_next_label(comp);
1667 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001668
Damien George391db862014-10-17 17:57:33 +00001669 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001670
Damien George391db862014-10-17 17:57:33 +00001671 // optimisation: skip everything else when "if True" (not in CPython)
1672 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1673 goto done;
1674 }
1675
1676 if (
1677 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1678 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1679 // optimisation: don't jump if last instruction was return
1680 && !EMIT(last_emit_was_return_value)
1681 ) {
1682 // jump over elif/else blocks
1683 EMIT_ARG(jump, l_end);
1684 }
1685
1686 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001687 }
1688
Damien George235f9b32014-10-17 17:30:16 +00001689 // compile elif blocks (if any)
1690 mp_parse_node_t *pn_elif;
1691 int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1692 for (int i = 0; i < n_elif; i++) {
1693 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1694 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001695
Damien George391db862014-10-17 17:57:33 +00001696 // optimisation: don't emit anything when "if False" (not in CPython)
1697 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1698 uint l_fail = comp_next_label(comp);
1699 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001700
Damien George391db862014-10-17 17:57:33 +00001701 compile_node(comp, pns_elif->nodes[1]); // elif block
1702
1703 // optimisation: skip everything else when "elif True" (not in CPython)
1704 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1705 goto done;
1706 }
1707
1708 // optimisation: don't jump if last instruction was return
1709 if (!EMIT(last_emit_was_return_value)) {
1710 EMIT_ARG(jump, l_end);
1711 }
1712 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001713 }
1714 }
1715
1716 // compile else block
1717 compile_node(comp, pns->nodes[3]); // can be null
1718
Damien George391db862014-10-17 17:57:33 +00001719done:
Damien Georgeb9791222014-01-23 00:34:21 +00001720 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001721}
1722
Damien Georgecbddb272014-02-01 20:08:18 +00001723#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001724 uint16_t old_break_label = comp->break_label; \
1725 uint16_t old_continue_label = comp->continue_label; \
1726 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001727 uint break_label = comp_next_label(comp); \
1728 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001729 comp->break_label = break_label; \
1730 comp->continue_label = continue_label; \
1731 comp->break_continue_except_level = comp->cur_except_level;
1732
1733#define END_BREAK_CONTINUE_BLOCK \
1734 comp->break_label = old_break_label; \
1735 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001736 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001737
Damien George969a6b32014-12-10 22:07:04 +00001738STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001739 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001740
Damience89a212013-10-15 22:25:17 +01001741 // compared to CPython, we have an optimised version of while loops
1742#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001743 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001744 EMIT_ARG(setup_loop, break_label);
1745 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001746 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1747 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001748 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001749 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001750 }
Damien Georgeb9791222014-01-23 00:34:21 +00001751 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001752 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1753 // this is a small hack to agree with CPython
1754 if (!node_is_const_true(pns->nodes[0])) {
1755 EMIT(pop_block);
1756 }
Damience89a212013-10-15 22:25:17 +01001757#else
Damien George391db862014-10-17 17:57:33 +00001758 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1759 uint top_label = comp_next_label(comp);
1760 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1761 EMIT_ARG(jump, continue_label);
1762 }
1763 EMIT_ARG(label_assign, top_label);
1764 compile_node(comp, pns->nodes[1]); // body
1765 EMIT_ARG(label_assign, continue_label);
1766 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1767 }
Damience89a212013-10-15 22:25:17 +01001768#endif
1769
1770 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001771 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001772
1773 compile_node(comp, pns->nodes[2]); // else
1774
Damien Georgeb9791222014-01-23 00:34:21 +00001775 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001776}
1777
Damien George2ac4af62014-08-15 16:45:41 +01001778#if !MICROPY_EMIT_CPYTHON
Damienf72fd0e2013-11-06 20:20:49 +00001779// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001780// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1781// And, if the loop never runs, the loop variable should never be assigned
Damien George6be0b0a2014-08-15 14:30:52 +01001782STATIC 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 +00001783 START_BREAK_CONTINUE_BLOCK
Damien George5b5562c2014-05-31 17:59:11 +01001784 // note that we don't need to pop anything when breaking from an optimise for loop
Damienf72fd0e2013-11-06 20:20:49 +00001785
Damien George6f355fd2014-04-10 14:11:31 +01001786 uint top_label = comp_next_label(comp);
1787 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001788
Damien George3ff2d032014-03-31 18:02:22 +01001789 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001790 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001791 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001792
Damien Georgeb9791222014-01-23 00:34:21 +00001793 EMIT_ARG(jump, entry_label);
1794 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001795
Damien George3ff2d032014-03-31 18:02:22 +01001796 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001797 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001798
1799 // store next value to var
1800 c_assign(comp, pn_var, ASSIGN_STORE);
1801
Damienf3822fc2013-11-09 20:12:03 +00001802 // compile body
1803 compile_node(comp, pn_body);
1804
Damien Georgeb9791222014-01-23 00:34:21 +00001805 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001806
Damien George3ff2d032014-03-31 18:02:22 +01001807 // compile: var + step, duplicated on stack
1808 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001809 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001810 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001811 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001812
Damien Georgeb9791222014-01-23 00:34:21 +00001813 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001814
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001815 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001816 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001817 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1818 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001819 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001820 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001821 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001822 }
Damien Georgeb9791222014-01-23 00:34:21 +00001823 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001824
Damien George3ff2d032014-03-31 18:02:22 +01001825 // discard final value of var that failed the loop condition
1826 EMIT(pop_top);
1827
Damienf72fd0e2013-11-06 20:20:49 +00001828 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001829 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001830
1831 compile_node(comp, pn_else);
1832
Damien Georgeb9791222014-01-23 00:34:21 +00001833 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001834}
Damien George2ac4af62014-08-15 16:45:41 +01001835#endif
Damienf72fd0e2013-11-06 20:20:49 +00001836
Damien George969a6b32014-12-10 22:07:04 +00001837STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001838#if !MICROPY_EMIT_CPYTHON
1839 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1840 // this is actually slower, but uses no heap memory
1841 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001842 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 +00001843 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001844 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1845 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1846 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1847 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001848 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1849 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001850 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001851 mp_parse_node_t pn_range_start;
1852 mp_parse_node_t pn_range_end;
1853 mp_parse_node_t pn_range_step;
1854 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001855 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001856 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001857 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001858 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001859 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001860 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001861 } else if (n_args == 2) {
1862 pn_range_start = args[0];
1863 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001864 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001865 } else {
1866 pn_range_start = args[0];
1867 pn_range_end = args[1];
1868 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001869 // We need to know sign of step. This is possible only if it's constant
1870 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1871 optimize = false;
1872 }
Damienf72fd0e2013-11-06 20:20:49 +00001873 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001874 }
1875 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001876 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1877 return;
1878 }
1879 }
1880 }
1881#endif
1882
Damien Georgecbddb272014-02-01 20:08:18 +00001883 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001884 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001885
Damien George6f355fd2014-04-10 14:11:31 +01001886 uint pop_label = comp_next_label(comp);
1887 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001888
Damience89a212013-10-15 22:25:17 +01001889 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1890#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001891 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001892#endif
1893
Damien429d7192013-10-04 19:53:11 +01001894 compile_node(comp, pns->nodes[1]); // iterator
1895 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001896 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001897 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001898 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1899 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001900 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001901 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001902 }
Damien Georgeb9791222014-01-23 00:34:21 +00001903 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001904 EMIT(for_iter_end);
1905
1906 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001907 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001908
Damience89a212013-10-15 22:25:17 +01001909#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001910 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001911#endif
Damien429d7192013-10-04 19:53:11 +01001912
1913 compile_node(comp, pns->nodes[3]); // else (not tested)
1914
Damien Georgeb9791222014-01-23 00:34:21 +00001915 EMIT_ARG(label_assign, break_label);
1916 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001917}
1918
Damien George6be0b0a2014-08-15 14:30:52 +01001919STATIC 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 +01001920 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001921 uint l1 = comp_next_label(comp);
1922 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001923
Damien Georgeb9791222014-01-23 00:34:21 +00001924 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001925 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001926
Damien429d7192013-10-04 19:53:11 +01001927 compile_node(comp, pn_body); // body
1928 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001929 EMIT_ARG(jump, success_label); // jump over exception handler
1930
1931 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001932 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001933
Damien George6f355fd2014-04-10 14:11:31 +01001934 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001935
1936 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001937 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1938 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001939
1940 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001941 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001942
Damiend99b0522013-12-21 18:17:45 +00001943 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001944 // this is a catch all exception handler
1945 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001946 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001947 return;
1948 }
1949 } else {
1950 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001951 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1952 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1953 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1954 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001955 // handler binds the exception to a local
1956 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001957 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001958 }
1959 }
1960 EMIT(dup_top);
1961 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001962 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001963 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001964 }
1965
1966 EMIT(pop_top);
1967
1968 if (qstr_exception_local == 0) {
1969 EMIT(pop_top);
1970 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001971 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001972 }
1973
1974 EMIT(pop_top);
1975
Damien George6f355fd2014-04-10 14:11:31 +01001976 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001977 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001978 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001979 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001980 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001981 }
1982 compile_node(comp, pns_except->nodes[1]);
1983 if (qstr_exception_local != 0) {
1984 EMIT(pop_block);
1985 }
1986 EMIT(pop_except);
1987 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001988 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1989 EMIT_ARG(label_assign, l3);
1990 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1991 EMIT_ARG(store_id, qstr_exception_local);
1992 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001993
Damien George8dcc0c72014-03-27 10:55:21 +00001994 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001995 EMIT(end_finally);
1996 }
Damien Georgeb9791222014-01-23 00:34:21 +00001997 EMIT_ARG(jump, l2);
1998 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001999 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002000 }
2001
Damien George8dcc0c72014-03-27 10:55:21 +00002002 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002003 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002004 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002005
Damien Georgeb9791222014-01-23 00:34:21 +00002006 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002007 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002008 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002009}
2010
Damien George6be0b0a2014-08-15 14:30:52 +01002011STATIC 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 +01002012 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002013
Damien Georgeb9791222014-01-23 00:34:21 +00002014 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002015 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002016
Damien429d7192013-10-04 19:53:11 +01002017 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002018 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002019 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002020 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002021 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002022 } else {
2023 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2024 }
2025 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002026 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2027 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002028 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002029
Damien George8dcc0c72014-03-27 10:55:21 +00002030 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002031 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002032}
2033
Damien George969a6b32014-12-10 22:07:04 +00002034STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002035 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2036 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2037 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002038 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002039 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2040 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002041 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002042 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002043 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002044 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002045 // no finally
2046 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2047 } else {
2048 // have finally
Damiend99b0522013-12-21 18:17:45 +00002049 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 +01002050 }
2051 } else {
2052 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002053 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002054 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002055 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002056 }
2057 } else {
2058 // shouldn't happen
2059 assert(0);
2060 }
2061}
2062
Damien George6be0b0a2014-08-15 14:30:52 +01002063STATIC 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 +01002064 if (n == 0) {
2065 // no more pre-bits, compile the body of the with
2066 compile_node(comp, body);
2067 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002068 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002069 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002070 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002071 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002072 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002073 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002074 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2075 } else {
2076 // this pre-bit is just an expression
2077 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002078 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002079 EMIT(pop_top);
2080 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002081 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002082 // compile additional pre-bits and the body
2083 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2084 // finish this with block
2085 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002086 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2087 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002088 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002089 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002090 EMIT(end_finally);
2091 }
2092}
2093
Damien George969a6b32014-12-10 22:07:04 +00002094STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002095 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002096 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002097 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2098 assert(n > 0);
2099
2100 // compile in a nested fashion
2101 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2102}
2103
Damien George969a6b32014-12-10 22:07:04 +00002104STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002105 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002106 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2107 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002108 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002109 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002110 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002111 EMIT(pop_top);
2112
Damien429d7192013-10-04 19:53:11 +01002113 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002114 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002115 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2116 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002117 // do nothing with a lonely constant
2118 } else {
2119 compile_node(comp, pns->nodes[0]); // just an expression
2120 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2121 }
Damien429d7192013-10-04 19:53:11 +01002122 }
2123 } else {
Damiend99b0522013-12-21 18:17:45 +00002124 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2125 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002126 if (kind == PN_expr_stmt_augassign) {
2127 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2128 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002129 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002130 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002131 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002132 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2133 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2134 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2135 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2136 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2137 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2138 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2139 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2140 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2141 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2142 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2143 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2144 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002145 }
Damien George7e5fb242014-02-01 22:18:47 +00002146 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002147 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2148 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002149 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2150 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002151 // following CPython, we store left-most first
2152 if (rhs > 0) {
2153 EMIT(dup_top);
2154 }
2155 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2156 for (int i = 0; i < rhs; i++) {
2157 if (i + 1 < rhs) {
2158 EMIT(dup_top);
2159 }
Damiend99b0522013-12-21 18:17:45 +00002160 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002161 }
2162 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002163 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002164 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2165 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2166 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002167 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002168 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2169 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002170 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2171 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2172 // can't optimise when it's a star expression on the lhs
2173 goto no_optimisation;
2174 }
Damien429d7192013-10-04 19:53:11 +01002175 compile_node(comp, pns10->nodes[0]); // rhs
2176 compile_node(comp, pns10->nodes[1]); // rhs
2177 EMIT(rot_two);
2178 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2179 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002180 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2181 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2182 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2183 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002184 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002185 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2186 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002187 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2188 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2189 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2190 // can't optimise when it's a star expression on the lhs
2191 goto no_optimisation;
2192 }
Damien429d7192013-10-04 19:53:11 +01002193 compile_node(comp, pns10->nodes[0]); // rhs
2194 compile_node(comp, pns10->nodes[1]); // rhs
2195 compile_node(comp, pns10->nodes[2]); // rhs
2196 EMIT(rot_three);
2197 EMIT(rot_two);
2198 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2199 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2200 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2201 } else {
Damien George495d7812014-04-08 17:51:47 +01002202 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002203 compile_node(comp, pns1->nodes[0]); // rhs
2204 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2205 }
2206 } else {
2207 // shouldn't happen
2208 assert(0);
2209 }
2210 }
2211}
2212
Damien George6be0b0a2014-08-15 14:30:52 +01002213STATIC 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 +00002214 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002215 compile_node(comp, pns->nodes[0]);
2216 for (int i = 1; i < num_nodes; i += 1) {
2217 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002218 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002219 }
2220}
2221
Damien George969a6b32014-12-10 22:07:04 +00002222STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002223 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2224 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002225
Damien George6f355fd2014-04-10 14:11:31 +01002226 uint l_fail = comp_next_label(comp);
2227 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002228 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2229 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002230 EMIT_ARG(jump, l_end);
2231 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002232 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002233 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002234 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002235}
2236
Damien George969a6b32014-12-10 22:07:04 +00002237STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002238 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002239 //mp_parse_node_t pn_params = pns->nodes[0];
2240 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002241
Damien George36db6bc2014-05-07 17:24:22 +01002242 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002243 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002244 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 +01002245 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002246 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002247 }
2248
2249 // get the scope for this lambda
2250 scope_t *this_scope = (scope_t*)pns->nodes[2];
2251
2252 // make the lambda
2253 close_over_variables_etc(comp, this_scope, 0, 0);
2254}
2255
Damien George969a6b32014-12-10 22:07:04 +00002256STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002257 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002258 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002259 for (int i = 0; i < n; i += 1) {
2260 compile_node(comp, pns->nodes[i]);
2261 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002262 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002263 }
2264 }
Damien Georgeb9791222014-01-23 00:34:21 +00002265 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002266}
2267
Damien George969a6b32014-12-10 22:07:04 +00002268STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002269 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002270 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002271 for (int i = 0; i < n; i += 1) {
2272 compile_node(comp, pns->nodes[i]);
2273 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002274 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002275 }
2276 }
Damien Georgeb9791222014-01-23 00:34:21 +00002277 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002278}
2279
Damien George969a6b32014-12-10 22:07:04 +00002280STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002281 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002282 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002283}
2284
Damien George969a6b32014-12-10 22:07:04 +00002285STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002286 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002287 compile_node(comp, pns->nodes[0]);
2288 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002289 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002290 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002291 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002292 }
2293 for (int i = 1; i + 1 < num_nodes; i += 2) {
2294 compile_node(comp, pns->nodes[i + 1]);
2295 if (i + 2 < num_nodes) {
2296 EMIT(dup_top);
2297 EMIT(rot_three);
2298 }
Damien George7e5fb242014-02-01 22:18:47 +00002299 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002300 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002301 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002302 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2303 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2304 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2305 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2306 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2307 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2308 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2309 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002310 }
2311 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002312 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2313 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2314 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002315 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002316 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002317 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002318 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002319 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002320 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002321 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002322 }
2323 } else {
2324 // shouldn't happen
2325 assert(0);
2326 }
2327 } else {
2328 // shouldn't happen
2329 assert(0);
2330 }
2331 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002332 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002333 }
2334 }
2335 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002336 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002337 EMIT_ARG(jump, l_end);
2338 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002339 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002340 EMIT(rot_two);
2341 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002342 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002343 }
2344}
2345
Damien George969a6b32014-12-10 22:07:04 +00002346STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002347 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002348}
2349
Damien George969a6b32014-12-10 22:07:04 +00002350STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002351 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002352}
2353
Damien George969a6b32014-12-10 22:07:04 +00002354STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002355 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002356}
2357
Damien George969a6b32014-12-10 22:07:04 +00002358STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002359 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002360}
2361
Damien George969a6b32014-12-10 22:07:04 +00002362STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002363 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002364 compile_node(comp, pns->nodes[0]);
2365 for (int i = 1; i + 1 < num_nodes; i += 2) {
2366 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002367 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002368 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002369 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002370 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002371 } else {
2372 // shouldn't happen
2373 assert(0);
2374 }
2375 }
2376}
2377
Damien George969a6b32014-12-10 22:07:04 +00002378STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002379 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002380 compile_node(comp, pns->nodes[0]);
2381 for (int i = 1; i + 1 < num_nodes; i += 2) {
2382 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002383 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002384 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002385 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002386 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002387 } else {
2388 // shouldn't happen
2389 assert(0);
2390 }
2391 }
2392}
2393
Damien George969a6b32014-12-10 22:07:04 +00002394STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002395 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002396 compile_node(comp, pns->nodes[0]);
2397 for (int i = 1; i + 1 < num_nodes; i += 2) {
2398 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002399 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002400 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002401 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002402 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002403 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002404 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002405 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002406 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002407 } else {
2408 // shouldn't happen
2409 assert(0);
2410 }
2411 }
2412}
2413
Damien George969a6b32014-12-10 22:07:04 +00002414STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002415 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002416 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002417 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002418 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002419 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002420 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002421 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002422 } else {
2423 // shouldn't happen
2424 assert(0);
2425 }
2426}
2427
Damien George969a6b32014-12-10 22:07:04 +00002428STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002429 // this is to handle special super() call
2430 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2431
2432 compile_generic_all_nodes(comp, pns);
2433}
2434
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002435STATIC 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 +01002436 // function to call is on top of stack
2437
Damien George35e2a4e2014-02-05 00:51:47 +00002438#if !MICROPY_EMIT_CPYTHON
2439 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002440 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 +00002441 EMIT_ARG(load_id, MP_QSTR___class__);
2442 // get first argument to function
2443 bool found = false;
2444 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002445 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2446 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 +00002447 found = true;
2448 break;
2449 }
2450 }
2451 if (!found) {
2452 printf("TypeError: super() call cannot find self\n");
2453 return;
2454 }
Damien George922ddd62014-04-09 12:43:17 +01002455 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002456 return;
2457 }
2458#endif
2459
Damien George2c0842b2014-04-27 16:46:51 +01002460 // get the list of arguments
2461 mp_parse_node_t *args;
2462 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002463
Damien George2c0842b2014-04-27 16:46:51 +01002464 // compile the arguments
2465 // Rather than calling compile_node on the list, we go through the list of args
2466 // explicitly here so that we can count the number of arguments and give sensible
2467 // error messages.
2468 int n_positional = n_positional_extra;
2469 uint n_keyword = 0;
2470 uint star_flags = 0;
2471 for (int i = 0; i < n_args; i++) {
2472 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2473 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2474 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2475 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2476 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2477 return;
2478 }
2479 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2480 compile_node(comp, pns_arg->nodes[0]);
2481 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2482 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2483 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2484 return;
2485 }
2486 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2487 compile_node(comp, pns_arg->nodes[0]);
2488 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2489 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2490 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2491 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2492 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2493 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2494 return;
2495 }
Damien George968bf342014-04-27 19:12:05 +01002496 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002497 compile_node(comp, pns2->nodes[0]);
2498 n_keyword += 1;
2499 } else {
2500 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2501 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2502 n_positional++;
2503 }
2504 } else {
2505 goto normal_argument;
2506 }
2507 } else {
2508 normal_argument:
2509 if (n_keyword > 0) {
2510 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2511 return;
2512 }
2513 compile_node(comp, args[i]);
2514 n_positional++;
2515 }
Damien429d7192013-10-04 19:53:11 +01002516 }
2517
Damien George2c0842b2014-04-27 16:46:51 +01002518 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002519 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002520 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002521 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002522 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002523 }
Damien429d7192013-10-04 19:53:11 +01002524}
2525
Damien George969a6b32014-12-10 22:07:04 +00002526STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002527 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002528 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002529 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 +01002530 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002531 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2532 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002533 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002534 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002535 i += 1;
2536 } else {
2537 compile_node(comp, pns->nodes[i]);
2538 }
Damien George35e2a4e2014-02-05 00:51:47 +00002539 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002540 }
2541}
2542
Damien George969a6b32014-12-10 22:07:04 +00002543STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002544 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002545 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002546}
2547
Damien George969a6b32014-12-10 22:07:04 +00002548STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002549 // a list of strings
Damien63321742013-12-10 17:41:49 +00002550
2551 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002552 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002553 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002554 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002555 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002556 int pn_kind;
2557 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2558 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2559 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2560 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2561 } else {
2562 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2563 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2564 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2565 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002566 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002567 }
Damien63321742013-12-10 17:41:49 +00002568 if (i == 0) {
2569 string_kind = pn_kind;
2570 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002571 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002572 return;
2573 }
Damien429d7192013-10-04 19:53:11 +01002574 }
Damien63321742013-12-10 17:41:49 +00002575
Damien63321742013-12-10 17:41:49 +00002576 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002577 byte *q_ptr;
2578 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002579 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002580 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002581 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002582 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2583 memcpy(s_dest, s, s_len);
2584 s_dest += s_len;
2585 } else {
2586 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002587 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2588 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002589 }
Damien63321742013-12-10 17:41:49 +00002590 }
Damien George55baff42014-01-21 21:40:13 +00002591 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002592
Damien Georgeb9791222014-01-23 00:34:21 +00002593 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002594}
2595
2596// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002597STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002598 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2599 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2600 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002601
Damien George36db6bc2014-05-07 17:24:22 +01002602 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002603 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002604 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 +01002605 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002606 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002607 }
2608
2609 // get the scope for this comprehension
2610 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2611
2612 // compile the comprehension
2613 close_over_variables_etc(comp, this_scope, 0, 0);
2614
2615 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2616 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002617 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002618}
2619
Damien George969a6b32014-12-10 22:07:04 +00002620STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002621 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002622 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002623 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2624 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2625 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2626 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2627 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2628 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2629 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002630 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002631 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002632 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002633 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002634 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002635 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002636 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002637 // generator expression
2638 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2639 } else {
2640 // tuple with 2 items
2641 goto tuple_with_2_items;
2642 }
2643 } else {
2644 // tuple with 2 items
2645 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002646 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002647 }
2648 } else {
2649 // parenthesis around a single item, is just that item
2650 compile_node(comp, pns->nodes[0]);
2651 }
2652}
2653
Damien George969a6b32014-12-10 22:07:04 +00002654STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002655 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002656 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002657 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002658 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2659 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2660 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2661 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2662 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002663 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002664 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002665 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002666 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002667 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002668 // list of many items
2669 compile_node(comp, pns2->nodes[0]);
2670 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002671 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002672 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002673 // list comprehension
2674 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2675 } else {
2676 // list with 2 items
2677 goto list_with_2_items;
2678 }
2679 } else {
2680 // list with 2 items
2681 list_with_2_items:
2682 compile_node(comp, pns2->nodes[0]);
2683 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002684 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002685 }
2686 } else {
2687 // list with 1 item
2688 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002689 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002690 }
2691}
2692
Damien George969a6b32014-12-10 22:07:04 +00002693STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002694 mp_parse_node_t pn = pns->nodes[0];
2695 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002696 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002697 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002698 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2699 pns = (mp_parse_node_struct_t*)pn;
2700 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002701 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002702 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002703 compile_node(comp, pn);
2704 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002705 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2706 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2707 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2708 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002709 // dict/set with multiple elements
2710
2711 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002712 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002713 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2714
2715 // first element sets whether it's a dict or set
2716 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002717 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002718 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002719 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002720 compile_node(comp, pns->nodes[0]);
2721 EMIT(store_map);
2722 is_dict = true;
2723 } else {
2724 // a set
2725 compile_node(comp, pns->nodes[0]); // 1st value of set
2726 is_dict = false;
2727 }
2728
2729 // process rest of elements
2730 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002731 mp_parse_node_t pn = nodes[i];
2732 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002733 compile_node(comp, pn);
2734 if (is_dict) {
2735 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002736 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002737 return;
2738 }
2739 EMIT(store_map);
2740 } else {
2741 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002742 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002743 return;
2744 }
2745 }
2746 }
2747
2748 // if it's a set, build it
2749 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002750 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002751 }
Damiend99b0522013-12-21 18:17:45 +00002752 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002753 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002754 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002755 // a dictionary comprehension
2756 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2757 } else {
2758 // a set comprehension
2759 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2760 }
2761 } else {
2762 // shouldn't happen
2763 assert(0);
2764 }
2765 } else {
2766 // set with one element
2767 goto set_with_one_element;
2768 }
2769 } else {
2770 // set with one element
2771 set_with_one_element:
2772 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002773 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002774 }
2775}
2776
Damien George969a6b32014-12-10 22:07:04 +00002777STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002778 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002779}
2780
Damien George969a6b32014-12-10 22:07:04 +00002781STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002782 // object who's index we want is on top of stack
2783 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002784 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002785}
2786
Damien George969a6b32014-12-10 22:07:04 +00002787STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002788 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002789 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002790}
2791
Damien George969a6b32014-12-10 22:07:04 +00002792STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002793 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2794 mp_parse_node_t pn = pns->nodes[0];
2795 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002796 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002797 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2798 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002799 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2800 pns = (mp_parse_node_struct_t*)pn;
2801 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002802 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002803 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002804 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002805 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002806 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002807 } else {
2808 // [?::x]
2809 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002810 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002811 }
Damiend99b0522013-12-21 18:17:45 +00002812 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002813 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002814 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2815 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2816 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2817 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002818 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002819 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002820 } else {
2821 // [?:x:x]
2822 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002823 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002824 }
2825 } else {
2826 // [?:x]
2827 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002828 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002829 }
2830 } else {
2831 // [?:x]
2832 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002833 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002834 }
2835}
2836
Damien George969a6b32014-12-10 22:07:04 +00002837STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002838 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002839 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2840 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002841}
2842
Damien George969a6b32014-12-10 22:07:04 +00002843STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002844 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002845 compile_subscript_3_helper(comp, pns);
2846}
2847
Damien George969a6b32014-12-10 22:07:04 +00002848STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002849 // if this is called then we are compiling a dict key:value pair
2850 compile_node(comp, pns->nodes[1]); // value
2851 compile_node(comp, pns->nodes[0]); // key
2852}
2853
Damien George969a6b32014-12-10 22:07:04 +00002854STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002855 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002856 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002857 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002858}
2859
Damien George969a6b32014-12-10 22:07:04 +00002860STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002861 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002862 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002863 return;
2864 }
Damiend99b0522013-12-21 18:17:45 +00002865 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002866 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002867 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002868 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2869 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002870 compile_node(comp, pns->nodes[0]);
2871 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002872 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002873 EMIT(yield_from);
2874 } else {
2875 compile_node(comp, pns->nodes[0]);
2876 EMIT(yield_value);
2877 }
2878}
2879
Damiend99b0522013-12-21 18:17:45 +00002880typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002881STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002882 NULL,
2883#define nc NULL
2884#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002885#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002886#include "grammar.h"
2887#undef nc
2888#undef c
2889#undef DEF_RULE
2890};
2891
Damien George6be0b0a2014-08-15 14:30:52 +01002892STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002893 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002894 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002895 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002896 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002897 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002898 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002899 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002900 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002901 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002902 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2903 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2904 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2905 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002906 case MP_PARSE_NODE_TOKEN:
2907 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002908 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002909 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002910 // do nothing
2911 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002912 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002913 }
2914 break;
Damien429d7192013-10-04 19:53:11 +01002915 default: assert(0);
2916 }
2917 } else {
Damiend99b0522013-12-21 18:17:45 +00002918 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002919 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002920 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002921 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 +01002922 } else {
Damien George5042bce2014-05-25 22:06:06 +01002923 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2924 if (f == NULL) {
2925 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2926#if MICROPY_DEBUG_PRINTERS
2927 mp_parse_node_print(pn, 0);
2928#endif
2929 compile_syntax_error(comp, pn, "internal compiler error");
2930 } else {
2931 f(comp, pns);
2932 }
Damien429d7192013-10-04 19:53:11 +01002933 }
2934 }
2935}
2936
Damien George2ac4af62014-08-15 16:45:41 +01002937STATIC 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 +01002938 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002939 qstr param_name = MP_QSTR_NULL;
2940 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002941 if (MP_PARSE_NODE_IS_ID(pn)) {
2942 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002943 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002944 // comes after a star, so counts as a keyword-only parameter
2945 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002946 } else {
Damien George2827d622014-04-27 15:50:52 +01002947 // comes before a star, so counts as a positional parameter
2948 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002949 }
Damienb14de212013-10-06 00:28:28 +01002950 } else {
Damiend99b0522013-12-21 18:17:45 +00002951 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2952 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2953 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2954 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002955 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002956 // comes after a star, so counts as a keyword-only parameter
2957 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002958 } else {
Damien George2827d622014-04-27 15:50:52 +01002959 // comes before a star, so counts as a positional parameter
2960 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002961 }
Damiend99b0522013-12-21 18:17:45 +00002962 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002963 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002964 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002965 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002966 // bare star
2967 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002968 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002969 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002970 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002971 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002972 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01002973 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002974 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002975 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002976 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2977 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002978 } else {
2979 // shouldn't happen
2980 assert(0);
2981 }
Damiend99b0522013-12-21 18:17:45 +00002982 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2983 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002984 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002985 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002986 } else {
Damienb14de212013-10-06 00:28:28 +01002987 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002988 assert(0);
2989 }
Damien429d7192013-10-04 19:53:11 +01002990 }
2991
Damien George2827d622014-04-27 15:50:52 +01002992 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002993 bool added;
2994 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2995 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002996 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002997 return;
2998 }
Damien429d7192013-10-04 19:53:11 +01002999 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003000 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003001 }
3002}
3003
Damien George2827d622014-04-27 15:50:52 +01003004STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003005 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003006}
3007
Damien George2827d622014-04-27 15:50:52 +01003008STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003009 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3010}
3011
3012STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3013 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3014 // no annotation
3015 return;
3016 }
3017
3018 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3019 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3020 // named parameter with possible annotation
3021 // fallthrough
3022 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3023 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3024 // named star with possible annotation
3025 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3026 // fallthrough
3027 } else {
3028 // no annotation
3029 return;
3030 }
3031 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3032 // double star with possible annotation
3033 // fallthrough
3034 } else {
3035 // no annotation
3036 return;
3037 }
3038
3039 mp_parse_node_t pn_annotation = pns->nodes[1];
3040
3041 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3042 #if MICROPY_EMIT_NATIVE
3043 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3044 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3045 assert(id_info != NULL);
3046
3047 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3048 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3049 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3050 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3051 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003052 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003053 }
3054 }
3055 #endif // MICROPY_EMIT_NATIVE
3056 }
Damien429d7192013-10-04 19:53:11 +01003057}
3058
Damien George6be0b0a2014-08-15 14:30:52 +01003059STATIC 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 +01003060 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003061 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003062 // no more nested if/for; compile inner expression
3063 compile_node(comp, pn_inner_expr);
3064 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003065 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003066 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003067 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003068 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003069 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003070 } else {
3071 EMIT(yield_value);
3072 EMIT(pop_top);
3073 }
Damiend99b0522013-12-21 18:17:45 +00003074 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003075 // if condition
Damiend99b0522013-12-21 18:17:45 +00003076 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003077 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3078 pn_iter = pns_comp_if->nodes[1];
3079 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003080 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003081 // for loop
Damiend99b0522013-12-21 18:17:45 +00003082 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003083 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003084 uint l_end2 = comp_next_label(comp);
3085 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003086 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003087 EMIT_ARG(label_assign, l_top2);
3088 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003089 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3090 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 +00003091 EMIT_ARG(jump, l_top2);
3092 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003093 EMIT(for_iter_end);
3094 } else {
3095 // shouldn't happen
3096 assert(0);
3097 }
3098}
3099
Damien George1463c1f2014-04-25 23:52:57 +01003100STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3101#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003102 // see http://www.python.org/dev/peps/pep-0257/
3103
3104 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003105 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003106 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003107 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003108 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003109 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3110 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003111 for (int i = 0; i < num_nodes; i++) {
3112 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003113 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 +00003114 // not a newline, so this is the first statement; finish search
3115 break;
3116 }
3117 }
3118 // 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 +00003119 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003120 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003121 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003122 } else {
3123 return;
3124 }
3125
3126 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003127 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3128 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003129 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3130 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3131 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3132 // compile the doc string
3133 compile_node(comp, pns->nodes[0]);
3134 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003135 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003136 }
3137 }
Damien George1463c1f2014-04-25 23:52:57 +01003138#endif
Damien429d7192013-10-04 19:53:11 +01003139}
3140
Damien George1463c1f2014-04-25 23:52:57 +01003141STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003142 comp->pass = pass;
3143 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003144 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003145 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003146
Damien George36db6bc2014-05-07 17:24:22 +01003147 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003148 // reset maximum stack sizes in scope
3149 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003150 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003151 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003152 }
3153
Damien5ac1b2e2013-10-18 19:58:12 +01003154#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003155 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003156 scope_print_info(scope);
3157 }
Damien5ac1b2e2013-10-18 19:58:12 +01003158#endif
Damien429d7192013-10-04 19:53:11 +01003159
3160 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003161 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3162 assert(scope->kind == SCOPE_MODULE);
3163 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3164 compile_node(comp, pns->nodes[0]); // compile the expression
3165 EMIT(return_value);
3166 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003167 if (!comp->is_repl) {
3168 check_for_doc_string(comp, scope->pn);
3169 }
Damien429d7192013-10-04 19:53:11 +01003170 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003171 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003172 EMIT(return_value);
3173 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003174 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3175 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3176 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003177
3178 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003179 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003180 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003181 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003182 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003183 } else {
3184 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003185
Damien George2ac4af62014-08-15 16:45:41 +01003186 // argument annotations
3187 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3188
3189 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003190 mp_parse_node_t pn_annotation = pns->nodes[2];
3191 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3192 #if MICROPY_EMIT_NATIVE
3193 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3194 // nodes[2] can be null or a test-expr
3195 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3196 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3197 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3198 } else {
3199 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3200 }
Damien George2ac4af62014-08-15 16:45:41 +01003201 }
Damien Georgea5190a72014-08-15 22:39:08 +01003202 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003203 }
Damien George2ac4af62014-08-15 16:45:41 +01003204 }
Damien429d7192013-10-04 19:53:11 +01003205
3206 compile_node(comp, pns->nodes[3]); // 3 is function body
3207 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003208 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003209 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003210 EMIT(return_value);
3211 }
3212 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003213 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3214 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3215 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003216
3217 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003218 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003219 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003220 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003221 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3222 }
3223
3224 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003225
3226 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3227 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3228 EMIT(pop_top);
3229 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3230 }
Damien429d7192013-10-04 19:53:11 +01003231 EMIT(return_value);
3232 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3233 // a bit of a hack at the moment
3234
Damiend99b0522013-12-21 18:17:45 +00003235 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3236 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3237 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3238 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3239 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003240
Damien George708c0732014-04-27 19:23:46 +01003241 // We need a unique name for the comprehension argument (the iterator).
3242 // CPython uses .0, but we should be able to use anything that won't
3243 // clash with a user defined variable. Best to use an existing qstr,
3244 // so we use the blank qstr.
3245#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003246 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003247#else
3248 qstr qstr_arg = MP_QSTR_;
3249#endif
Damien George36db6bc2014-05-07 17:24:22 +01003250 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003251 bool added;
3252 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3253 assert(added);
3254 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003255 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003256 }
3257
3258 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003259 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003260 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003261 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003262 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003263 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003264 }
3265
Damien George6f355fd2014-04-10 14:11:31 +01003266 uint l_end = comp_next_label(comp);
3267 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003268 EMIT_ARG(load_id, qstr_arg);
3269 EMIT_ARG(label_assign, l_top);
3270 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003271 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3272 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003273 EMIT_ARG(jump, l_top);
3274 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003275 EMIT(for_iter_end);
3276
3277 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003278 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003279 }
3280 EMIT(return_value);
3281 } else {
3282 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003283 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3284 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3285 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003286
Damien George36db6bc2014-05-07 17:24:22 +01003287 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003288 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003289 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003290 assert(added);
3291 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003292 }
3293
Damien Georgeb9791222014-01-23 00:34:21 +00003294 EMIT_ARG(load_id, MP_QSTR___name__);
3295 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003296 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 +00003297 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003298
3299 check_for_doc_string(comp, pns->nodes[2]);
3300 compile_node(comp, pns->nodes[2]); // 2 is class body
3301
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003302 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003303 assert(id != NULL);
3304 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003305 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003306 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003307#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003308 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003309#else
Damien George2bf7c092014-04-09 15:26:46 +01003310 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003311#endif
Damien429d7192013-10-04 19:53:11 +01003312 }
3313 EMIT(return_value);
3314 }
3315
Damien415eb6f2013-10-05 12:19:06 +01003316 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003317
3318 // make sure we match all the exception levels
3319 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003320}
3321
Damien George094d4502014-04-02 17:31:27 +01003322#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003323// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003324STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003325 comp->pass = pass;
3326 comp->scope_cur = scope;
3327 comp->next_label = 1;
3328
3329 if (scope->kind != SCOPE_FUNCTION) {
3330 printf("Error: inline assembler must be a function\n");
3331 return;
3332 }
3333
Damien George36db6bc2014-05-07 17:24:22 +01003334 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003335 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003336 }
3337
Damien826005c2013-10-05 23:17:28 +01003338 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003339 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3340 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3341 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003342
Damiend99b0522013-12-21 18:17:45 +00003343 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003344
Damiena2f2f7d2013-10-06 00:14:13 +01003345 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003346 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003347 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003348 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003349 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003350 }
3351
Damiend99b0522013-12-21 18:17:45 +00003352 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003353
Damiend99b0522013-12-21 18:17:45 +00003354 mp_parse_node_t pn_body = pns->nodes[3]; // body
3355 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003356 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3357
Damien Georgecbd2f742014-01-19 11:48:48 +00003358 /*
Damien George36db6bc2014-05-07 17:24:22 +01003359 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003360 //printf("----\n");
3361 scope_print_info(scope);
3362 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003363 */
Damien826005c2013-10-05 23:17:28 +01003364
3365 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003366 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3367 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003368 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3369 // no instructions
3370 continue;
3371 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3372 // an instruction; fall through
3373 } else {
3374 // not an instruction; error
3375 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3376 return;
3377 }
Damiend99b0522013-12-21 18:17:45 +00003378 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3379 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3380 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3381 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3382 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3383 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3384 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3385 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3386 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3387 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003388 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3389
3390 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003391 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003392 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003393 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003394 return;
3395 }
Damien George6f355fd2014-04-10 14:11:31 +01003396 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003397 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003398 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003399 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003400 } else if (op == MP_QSTR_align) {
3401 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3402 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3403 return;
3404 }
Damien George36db6bc2014-05-07 17:24:22 +01003405 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003406 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3407 }
3408 } else if (op == MP_QSTR_data) {
3409 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3410 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3411 return;
3412 }
Damien George36db6bc2014-05-07 17:24:22 +01003413 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003414 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003415 for (uint i = 1; i < n_args; i++) {
3416 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3417 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3418 return;
3419 }
3420 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3421 }
3422 }
Damien826005c2013-10-05 23:17:28 +01003423 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003424 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003425 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003426 }
3427 }
3428 }
3429
Damien George36db6bc2014-05-07 17:24:22 +01003430 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003431 bool success = EMIT_INLINE_ASM(end_pass);
3432 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003433 // TODO get proper exception from inline assembler
3434 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003435 }
Damienb05d7072013-10-05 13:37:10 +01003436 }
Damien429d7192013-10-04 19:53:11 +01003437}
Damien George094d4502014-04-02 17:31:27 +01003438#endif
Damien429d7192013-10-04 19:53:11 +01003439
Damien George1463c1f2014-04-25 23:52:57 +01003440STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003441#if !MICROPY_EMIT_CPYTHON
3442 // in Micro Python we put the *x parameter after all other parameters (except **y)
3443 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3444 id_info_t *id_param = NULL;
3445 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3446 id_info_t *id = &scope->id_info[i];
3447 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3448 if (id_param != NULL) {
3449 // swap star param with last param
3450 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3451 }
3452 break;
3453 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3454 id_param = id;
3455 }
3456 }
3457 }
3458#endif
3459
Damien429d7192013-10-04 19:53:11 +01003460 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003461 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003462 scope->num_locals = 0;
3463 for (int i = 0; i < scope->id_info_len; i++) {
3464 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003465 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003466 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3467 continue;
3468 }
3469 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3470 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3471 }
Damien George2827d622014-04-27 15:50:52 +01003472 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003473 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003474 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003475 }
3476 }
3477
3478 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003479#if MICROPY_EMIT_CPYTHON
3480 int num_cell = 0;
3481#endif
Damien9ecbcff2013-12-11 00:41:43 +00003482 for (int i = 0; i < scope->id_info_len; i++) {
3483 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003484#if MICROPY_EMIT_CPYTHON
3485 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003486 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003487 id->local_num = num_cell;
3488 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003489 }
Damien George6baf76e2013-12-30 22:32:17 +00003490#else
3491 // in Micro Python the cells come right after the fast locals
3492 // parameters are not counted here, since they remain at the start
3493 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003494 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003495 id->local_num = scope->num_locals;
3496 scope->num_locals += 1;
3497 }
3498#endif
Damien9ecbcff2013-12-11 00:41:43 +00003499 }
Damien9ecbcff2013-12-11 00:41:43 +00003500
3501 // compute the index of free vars (freevars[idx] in CPython)
3502 // make sure they are in the order of the parent scope
3503 if (scope->parent != NULL) {
3504 int num_free = 0;
3505 for (int i = 0; i < scope->parent->id_info_len; i++) {
3506 id_info_t *id = &scope->parent->id_info[i];
3507 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3508 for (int j = 0; j < scope->id_info_len; j++) {
3509 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003510 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003511 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003512#if MICROPY_EMIT_CPYTHON
3513 // in CPython the frees are numbered after the cells
3514 id2->local_num = num_cell + num_free;
3515#else
3516 // in Micro Python the frees come first, before the params
3517 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003518#endif
3519 num_free += 1;
3520 }
3521 }
3522 }
Damien429d7192013-10-04 19:53:11 +01003523 }
Damien George6baf76e2013-12-30 22:32:17 +00003524#if !MICROPY_EMIT_CPYTHON
3525 // in Micro Python shift all other locals after the free locals
3526 if (num_free > 0) {
3527 for (int i = 0; i < scope->id_info_len; i++) {
3528 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003529 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003530 id->local_num += num_free;
3531 }
3532 }
Damien George2827d622014-04-27 15:50:52 +01003533 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 +00003534 scope->num_locals += num_free;
3535 }
3536#endif
Damien429d7192013-10-04 19:53:11 +01003537 }
3538
Damien George8725f8f2014-02-15 19:33:11 +00003539 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003540
3541#if MICROPY_EMIT_CPYTHON
3542 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003543 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) {
3544 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003545 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003546 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003547 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 +00003548 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003549 }
3550 }
Damien George882b3632014-04-02 15:56:31 +01003551#endif
3552
Damien429d7192013-10-04 19:53:11 +01003553 int num_free = 0;
3554 for (int i = 0; i < scope->id_info_len; i++) {
3555 id_info_t *id = &scope->id_info[i];
3556 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3557 num_free += 1;
3558 }
3559 }
3560 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003561 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003562 }
3563}
3564
Damien George65cad122014-04-06 11:48:15 +01003565mp_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 +01003566 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003567 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003568 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003569 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003570
Damien826005c2013-10-05 23:17:28 +01003571 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003572 mp_map_t consts;
3573 mp_map_init(&consts, 0);
3574 pn = fold_constants(comp, pn, &consts);
3575 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003576
3577 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003578 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003579
Damien826005c2013-10-05 23:17:28 +01003580 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003581 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003582 comp->emit_method_table = &emit_pass1_method_table;
3583 comp->emit_inline_asm = NULL;
3584 comp->emit_inline_asm_method_table = NULL;
3585 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003586 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003587 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003588#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003589 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003590 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003591#endif
Damien826005c2013-10-05 23:17:28 +01003592 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003593 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003594 }
3595
3596 // update maximim number of labels needed
3597 if (comp->next_label > max_num_labels) {
3598 max_num_labels = comp->next_label;
3599 }
Damien429d7192013-10-04 19:53:11 +01003600 }
3601
Damien826005c2013-10-05 23:17:28 +01003602 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003603 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003604 compile_scope_compute_things(comp, s);
3605 }
3606
Damien826005c2013-10-05 23:17:28 +01003607 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003608 emit_pass1_free(comp->emit);
3609
Damien826005c2013-10-05 23:17:28 +01003610 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003611#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003612 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003613#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003614 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003615#endif
Damien3ef4abb2013-10-12 16:53:13 +01003616#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003617 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003618#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003619#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003620 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003621 if (false) {
3622 // dummy
3623
Damien3ef4abb2013-10-12 16:53:13 +01003624#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003625 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003626 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003627 if (emit_inline_thumb == NULL) {
3628 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3629 }
3630 comp->emit = NULL;
3631 comp->emit_method_table = NULL;
3632 comp->emit_inline_asm = emit_inline_thumb;
3633 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003634 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003635 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003636 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003637 }
Damienc025ebb2013-10-12 14:30:21 +01003638#endif
3639
Damien826005c2013-10-05 23:17:28 +01003640 } else {
Damienc025ebb2013-10-12 14:30:21 +01003641
3642 // choose the emit type
3643
Damien3ef4abb2013-10-12 16:53:13 +01003644#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003645 comp->emit = emit_cpython_new(max_num_labels);
3646 comp->emit_method_table = &emit_cpython_method_table;
3647#else
Damien826005c2013-10-05 23:17:28 +01003648 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003649
3650#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003651 case MP_EMIT_OPT_NATIVE_PYTHON:
3652 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003653#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003654 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003655 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003656 }
Damien13ed3a62013-10-08 09:05:10 +01003657 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003658#elif MICROPY_EMIT_X86
3659 if (emit_native == NULL) {
3660 emit_native = emit_native_x86_new(max_num_labels);
3661 }
3662 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003663#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003664 if (emit_native == NULL) {
3665 emit_native = emit_native_thumb_new(max_num_labels);
3666 }
3667 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003668#elif MICROPY_EMIT_ARM
3669 if (emit_native == NULL) {
3670 emit_native = emit_native_arm_new(max_num_labels);
3671 }
3672 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003673#endif
3674 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003675 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 +01003676
3677 // native emitters need an extra pass to compute stack size
3678 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3679
Damien7af3d192013-10-07 00:02:49 +01003680 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003681#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003682
Damien826005c2013-10-05 23:17:28 +01003683 default:
3684 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003685 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003686 }
3687 comp->emit = emit_bc;
3688 comp->emit_method_table = &emit_bc_method_table;
3689 break;
3690 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003691#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003692
Damien George36db6bc2014-05-07 17:24:22 +01003693 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003694 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003695 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3696 }
3697
3698 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003699 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003700 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003701 }
Damien6cdd3af2013-10-05 18:08:26 +01003702 }
Damien429d7192013-10-04 19:53:11 +01003703 }
3704
Damien George41d02b62014-01-24 22:42:28 +00003705 // free the emitters
3706#if !MICROPY_EMIT_CPYTHON
3707 if (emit_bc != NULL) {
3708 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003709 }
Damien George41d02b62014-01-24 22:42:28 +00003710#if MICROPY_EMIT_NATIVE
3711 if (emit_native != NULL) {
3712#if MICROPY_EMIT_X64
3713 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003714#elif MICROPY_EMIT_X86
3715 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003716#elif MICROPY_EMIT_THUMB
3717 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003718#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003719 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003720#endif
3721 }
3722#endif
3723#if MICROPY_EMIT_INLINE_THUMB
3724 if (emit_inline_thumb != NULL) {
3725 emit_inline_thumb_free(emit_inline_thumb);
3726 }
3727#endif
3728#endif // !MICROPY_EMIT_CPYTHON
3729
Damien George52b5d762014-09-23 15:31:56 +00003730 // free the parse tree
3731 mp_parse_node_free(pn);
3732
Damien George41d02b62014-01-24 22:42:28 +00003733 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003734 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003735 for (scope_t *s = module_scope; s;) {
3736 scope_t *next = s->next;
3737 scope_free(s);
3738 s = next;
3739 }
Damien5ac1b2e2013-10-18 19:58:12 +01003740
Damien George41d02b62014-01-24 22:42:28 +00003741 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003742 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003743 m_del_obj(compiler_t, comp);
3744
Damien Georgea91ac202014-10-05 19:01:34 +01003745 if (compile_error != MP_OBJ_NULL) {
3746 return compile_error;
Damien George1fb03172014-01-03 14:22:03 +00003747 } else {
3748#if MICROPY_EMIT_CPYTHON
3749 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003750 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003751 return mp_const_true;
3752#else
3753 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003754 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003755#endif
3756 }
Damien429d7192013-10-04 19:53:11 +01003757}