blob: 3ee78bbfc7b1950369be7b218a8076438d17c1ba [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
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/scope.h"
35#include "py/emit.h"
36#include "py/compile.h"
37#include "py/smallint.h"
38#include "py/runtime.h"
39#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010040
41// TODO need to mangle __attr names
42
43typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000044#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000045#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010046#undef DEF_RULE
47 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010048 PN_string, // special node for non-interned string
Damien429d7192013-10-04 19:53:11 +010049} pn_kind_t;
50
Damien Georgeb9791222014-01-23 00:34:21 +000051#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
52#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
53#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
54#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 +010055
Damien Georgea91ac202014-10-05 19:01:34 +010056// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010057typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000058 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010059
Damien George78035b92014-04-09 12:27:39 +010060 uint8_t is_repl;
61 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010062 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010063 uint8_t have_star;
64
65 // try to keep compiler clean from nlr
66 // this is set to an exception object if we have a compile error
67 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010068
Damien George6f355fd2014-04-10 14:11:31 +010069 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010070
Damien George69b89d22014-04-11 13:38:30 +000071 uint16_t num_dict_params;
72 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010073
Damien Georgea91ac202014-10-05 19:01:34 +010074 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
75 uint16_t continue_label;
76 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000077 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010078
Damien429d7192013-10-04 19:53:11 +010079 scope_t *scope_head;
80 scope_t *scope_cur;
81
Damien6cdd3af2013-10-05 18:08:26 +010082 emit_t *emit; // current emitter
83 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010084
85 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
86 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 +010087} compiler_t;
88
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010089STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010090 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
91 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010092 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien Georgea91ac202014-10-05 19:01:34 +010093 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 +010094 } else {
Damien Georgea91ac202014-10-05 19:01:34 +010095 // we don't have a line number, so just pass 0
96 mp_obj_exception_add_traceback(exc, comp->source_file, 0, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010097 }
Damien Georgea91ac202014-10-05 19:01:34 +010098 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +000099}
100
Damien Georgeddd1e182015-01-10 14:07:24 +0000101#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100102STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300103 #if MICROPY_PY_UCTYPES
104 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
105 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100106 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100107 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100108};
Damien Georgeddd1e182015-01-10 14:07:24 +0000109STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
110#endif
Damien George57e99eb2014-04-10 22:42:11 +0100111
Damien Georgeffae48d2014-05-08 15:58:39 +0000112// this function is essentially a simple preprocessor
113STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
114 if (0) {
115 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100116#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000117 } else if (MP_PARSE_NODE_IS_ID(pn)) {
118 // lookup identifier in table of dynamic constants
119 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
120 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
121 if (elem != NULL) {
122 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
123 }
124#endif
125 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000126 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100127
Damien Georgeffae48d2014-05-08 15:58:39 +0000128 // fold some parse nodes before folding their arguments
129 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100130#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000131 case PN_expr_stmt:
132 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
133 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
134 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
135 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
136 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
137 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
138 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
139 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
140 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
141 ) {
142 // code to assign dynamic constants: id = const(value)
143
144 // get the id
145 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
146
147 // get the value
148 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
149 pn_value = fold_constants(comp, pn_value, consts);
150 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
151 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
152 break;
153 }
Damien George40f3c022014-07-03 13:25:24 +0100154 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000155
156 // store the value in the table of dynamic constants
157 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
158 if (elem->value != MP_OBJ_NULL) {
159 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
160 break;
161 }
162 elem->value = MP_OBJ_NEW_SMALL_INT(value);
163
164 // replace const(value) with value
165 pns1->nodes[0] = pn_value;
166
167 // finished folding this assignment
168 return pn;
169 }
170 }
171 }
172 break;
173#endif
Damien George5042bce2014-05-25 22:06:06 +0100174 case PN_string:
175 return pn;
Damien429d7192013-10-04 19:53:11 +0100176 }
177
Damien Georgeffae48d2014-05-08 15:58:39 +0000178 // fold arguments
179 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
180 for (int i = 0; i < n; i++) {
181 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
182 }
183
184 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000185 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100186 case PN_atom_paren:
187 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
188 // (int)
189 pn = pns->nodes[0];
190 }
191 break;
192
193 case PN_expr:
194 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
195 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100196 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
197 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100198 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
199 }
200 break;
201
202 case PN_and_expr:
203 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
204 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100205 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
206 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100207 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
208 }
209 break;
210
Damien429d7192013-10-04 19:53:11 +0100211 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000212 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 +0100213 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
214 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000215 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100216 // int << int
217 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
218 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
219 }
Damiend99b0522013-12-21 18:17:45 +0000220 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100221 // int >> int
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200222 if (arg1 >= BITS_PER_WORD) {
223 // Shifting to big amounts is underfined behavior
224 // in C and is CPU-dependent; propagate sign bit.
225 arg1 = BITS_PER_WORD - 1;
226 }
Damiend99b0522013-12-21 18:17:45 +0000227 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100228 } else {
229 // shouldn't happen
230 assert(0);
231 }
232 }
233 break;
234
235 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100236 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000237 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 +0100238 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
239 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000240 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100241 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000242 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000243 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100244 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000245 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100246 } else {
247 // shouldn't happen
248 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100249 }
Damien Georged1e355e2014-05-28 14:51:12 +0100250 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100251 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000252 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100253 }
254 }
255 break;
256
257 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000258 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 +0100259 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
260 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000261 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100262 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000263 if (!mp_small_int_mul_overflow(arg0, arg1)) {
264 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100265 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000266 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
267 }
268 }
Damiend99b0522013-12-21 18:17:45 +0000269 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100270 // int / int
271 // pass
Damiend99b0522013-12-21 18:17:45 +0000272 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100273 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000274 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000275 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300276 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100277 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000278 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 +0300279 }
Damien429d7192013-10-04 19:53:11 +0100280 } else {
281 // shouldn't happen
282 assert(0);
283 }
284 }
285 break;
286
287 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000288 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100289 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000290 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100291 // +int
Damiend99b0522013-12-21 18:17:45 +0000292 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
293 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100294 // -int
Damiend99b0522013-12-21 18:17:45 +0000295 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
296 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100297 // ~int
Damiend99b0522013-12-21 18:17:45 +0000298 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100299 } else {
300 // shouldn't happen
301 assert(0);
302 }
303 }
304 break;
305
306 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100307 if (0) {
308#if MICROPY_EMIT_CPYTHON
309 } 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 +0100310 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100311 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000312 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
313 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200314 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100315 if (power >= 0) {
316 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200317 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100318 for (; power > 0; power--) {
319 ans *= base;
320 }
Damiend99b0522013-12-21 18:17:45 +0000321 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100322 }
323 }
Damien George57e99eb2014-04-10 22:42:11 +0100324#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000325#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100326 } 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])) {
327 // id.id
328 // look it up in constant table, see if it can be replaced with an integer
329 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
330 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
331 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
332 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
333 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
334 if (elem != NULL) {
335 mp_obj_t dest[2];
336 mp_load_method_maybe(elem->value, q_attr, dest);
337 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100338 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000339 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100340 }
341 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000342#endif
Damien429d7192013-10-04 19:53:11 +0100343 }
344 break;
345 }
346 }
347
348 return pn;
349}
350
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200351STATIC 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 +0100352STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000353STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100354
Damien George6f355fd2014-04-10 14:11:31 +0100355STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100356 return comp->next_label++;
357}
358
Damien George8dcc0c72014-03-27 10:55:21 +0000359STATIC void compile_increase_except_level(compiler_t *comp) {
360 comp->cur_except_level += 1;
361 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
362 comp->scope_cur->exc_stack_size = comp->cur_except_level;
363 }
364}
365
366STATIC void compile_decrease_except_level(compiler_t *comp) {
367 assert(comp->cur_except_level > 0);
368 comp->cur_except_level -= 1;
369}
370
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200371STATIC 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 +0100372 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100373 scope->parent = comp->scope_cur;
374 scope->next = NULL;
375 if (comp->scope_head == NULL) {
376 comp->scope_head = scope;
377 } else {
378 scope_t *s = comp->scope_head;
379 while (s->next != NULL) {
380 s = s->next;
381 }
382 s->next = scope;
383 }
384 return scope;
385}
386
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200387STATIC 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 +0000388 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
389 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
390 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100391 for (int i = 0; i < num_nodes; i++) {
392 f(comp, pns->nodes[i]);
393 }
Damiend99b0522013-12-21 18:17:45 +0000394 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100395 f(comp, pn);
396 }
397}
398
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200399STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000400 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100401 *nodes = NULL;
402 return 0;
Damiend99b0522013-12-21 18:17:45 +0000403 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100404 *nodes = pn;
405 return 1;
406 } else {
Damiend99b0522013-12-21 18:17:45 +0000407 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
408 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100409 *nodes = pn;
410 return 1;
411 } else {
412 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000413 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100414 }
415 }
416}
417
Damien George969a6b32014-12-10 22:07:04 +0000418STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000419 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100420 for (int i = 0; i < num_nodes; i++) {
421 compile_node(comp, pns->nodes[i]);
422 }
423}
424
Damien3ef4abb2013-10-12 16:53:13 +0100425#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200426STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100427 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
428 return true;
429 }
Damiend99b0522013-12-21 18:17:45 +0000430 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100431 return false;
432 }
Damiend99b0522013-12-21 18:17:45 +0000433 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100434 return false;
435 }
436 return true;
437}
438
Damien George5042bce2014-05-25 22:06:06 +0100439STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000440 bool has_single_quote = false;
441 bool has_double_quote = false;
442 for (int i = 0; i < len; i++) {
443 if (str[i] == '\'') {
444 has_single_quote = true;
445 } else if (str[i] == '"') {
446 has_double_quote = true;
447 }
448 }
449 if (bytes) {
450 vstr_printf(vstr, "b");
451 }
452 bool quote_single = false;
453 if (has_single_quote && !has_double_quote) {
454 vstr_printf(vstr, "\"");
455 } else {
456 quote_single = true;
457 vstr_printf(vstr, "'");
458 }
459 for (int i = 0; i < len; i++) {
460 if (str[i] == '\n') {
461 vstr_printf(vstr, "\\n");
462 } else if (str[i] == '\\') {
463 vstr_printf(vstr, "\\\\");
464 } else if (str[i] == '\'' && quote_single) {
465 vstr_printf(vstr, "\\'");
466 } else {
467 vstr_printf(vstr, "%c", str[i]);
468 }
469 }
470 if (has_single_quote && !has_double_quote) {
471 vstr_printf(vstr, "\"");
472 } else {
473 vstr_printf(vstr, "'");
474 }
475}
476
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200477STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George5042bce2014-05-25 22:06:06 +0100478 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
479 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George40f3c022014-07-03 13:25:24 +0100480 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 +0100481 return;
482 }
483
Damiend99b0522013-12-21 18:17:45 +0000484 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200485 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
486 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
487 return;
488 }
489
Damien George42f3de92014-10-03 17:44:14 +0000490 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000491 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
492 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000493 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
494 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100495 case MP_PARSE_NODE_STRING:
496 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100497 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100498 const byte *str = qstr_data(arg, &len);
499 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
500 break;
501 }
Damiend99b0522013-12-21 18:17:45 +0000502 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100503 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000504 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
505 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
506 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100507 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100508 }
509 break;
510 default: assert(0);
511 }
512}
513
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200514STATIC 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 +0100515 int n = 0;
516 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000517 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100518 }
519 int total = n;
520 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000521 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100522 total += 1;
Damien3a205172013-10-12 15:01:56 +0100523 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100524 is_const = false;
525 }
526 }
527 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100528 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100529 is_const = false;
530 break;
531 }
532 }
533 if (total > 0 && is_const) {
534 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000535 vstr_t *vstr = vstr_new();
536 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000537 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000538 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100539 need_comma = true;
540 }
541 for (int i = 0; i < n; i++) {
542 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000543 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100544 }
Damien02f89412013-12-12 15:13:36 +0000545 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100546 need_comma = true;
547 }
548 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000549 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100550 } else {
Damien02f89412013-12-12 15:13:36 +0000551 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100552 }
Damien Georgeb9791222014-01-23 00:34:21 +0000553 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000554 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100555 } else {
Damiend99b0522013-12-21 18:17:45 +0000556 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100557 compile_node(comp, pn);
558 }
559 for (int i = 0; i < n; i++) {
560 compile_node(comp, pns_list->nodes[i]);
561 }
Damien Georgeb9791222014-01-23 00:34:21 +0000562 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100563 }
564}
Damien3a205172013-10-12 15:01:56 +0100565#endif
566
567// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100568STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100569#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100570 cpython_c_tuple(comp, pn, pns_list);
571#else
572 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000573 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100574 compile_node(comp, pn);
575 total += 1;
576 }
577 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000578 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100579 for (int i = 0; i < n; i++) {
580 compile_node(comp, pns_list->nodes[i]);
581 }
582 total += n;
583 }
Damien Georgeb9791222014-01-23 00:34:21 +0000584 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100585#endif
586}
Damien429d7192013-10-04 19:53:11 +0100587
Damien George969a6b32014-12-10 22:07:04 +0000588STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100589 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000590 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100591}
592
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200593STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000594 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
595 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100596}
597
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200598STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000599 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
600 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100601}
602
Damien3ef4abb2013-10-12 16:53:13 +0100603#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100604// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200605STATIC 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 +0100606 if (node_is_const_false(pn)) {
607 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000608 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100609 }
610 return;
611 } else if (node_is_const_true(pn)) {
612 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000613 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100614 }
615 return;
Damiend99b0522013-12-21 18:17:45 +0000616 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
617 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
618 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
619 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100620 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100621 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100622 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100623 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100624 }
Damien3a205172013-10-12 15:01:56 +0100625 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000626 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100627 } else {
628 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100629 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100630 }
631 }
632 return;
Damiend99b0522013-12-21 18:17:45 +0000633 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100634 if (jump_if == false) {
635 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100636 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100637 }
638 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100639 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100640 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100641 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100642 }
Damien3a205172013-10-12 15:01:56 +0100643 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000644 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100645 }
646 return;
Damiend99b0522013-12-21 18:17:45 +0000647 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100648 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100649 return;
650 }
651 }
652
653 // nothing special, fall back to default compiling for node and jump
654 compile_node(comp, pn);
655 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000656 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100657 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000658 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100659 }
660}
Damien3a205172013-10-12 15:01:56 +0100661#endif
Damien429d7192013-10-04 19:53:11 +0100662
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200663STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100664#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100665 cpython_c_if_cond(comp, pn, jump_if, label, false);
666#else
667 if (node_is_const_false(pn)) {
668 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000669 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100670 }
671 return;
672 } else if (node_is_const_true(pn)) {
673 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000674 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100675 }
676 return;
Damiend99b0522013-12-21 18:17:45 +0000677 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
678 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
679 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
680 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100681 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100682 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100683 for (int i = 0; i < n - 1; i++) {
684 c_if_cond(comp, pns->nodes[i], true, label2);
685 }
686 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000687 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100688 } else {
689 for (int i = 0; i < n; i++) {
690 c_if_cond(comp, pns->nodes[i], true, label);
691 }
692 }
693 return;
Damiend99b0522013-12-21 18:17:45 +0000694 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100695 if (jump_if == false) {
696 for (int i = 0; i < n; i++) {
697 c_if_cond(comp, pns->nodes[i], false, label);
698 }
699 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100700 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100701 for (int i = 0; i < n - 1; i++) {
702 c_if_cond(comp, pns->nodes[i], false, label2);
703 }
704 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000705 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100706 }
707 return;
Damiend99b0522013-12-21 18:17:45 +0000708 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100709 c_if_cond(comp, pns->nodes[0], !jump_if, label);
710 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100711 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
712 // cond is something in parenthesis
713 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
714 // empty tuple, acts as false for the condition
715 if (jump_if == false) {
716 EMIT_ARG(jump, label);
717 }
718 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
719 // non-empty tuple, acts as true for the condition
720 if (jump_if == true) {
721 EMIT_ARG(jump, label);
722 }
723 } else {
724 // parenthesis around 1 item, is just that item
725 c_if_cond(comp, pns->nodes[0], jump_if, label);
726 }
727 return;
Damien3a205172013-10-12 15:01:56 +0100728 }
729 }
730
731 // nothing special, fall back to default compiling for node and jump
732 compile_node(comp, pn);
733 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000734 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100735 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000736 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100737 }
738#endif
Damien429d7192013-10-04 19:53:11 +0100739}
740
741typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100742STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100743
Damien George6be0b0a2014-08-15 14:30:52 +0100744STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100745 if (assign_kind != ASSIGN_AUG_STORE) {
746 compile_node(comp, pns->nodes[0]);
747 }
748
Damiend99b0522013-12-21 18:17:45 +0000749 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
750 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
751 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
752 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100753 if (assign_kind != ASSIGN_AUG_STORE) {
754 for (int i = 0; i < n - 1; i++) {
755 compile_node(comp, pns1->nodes[i]);
756 }
757 }
Damiend99b0522013-12-21 18:17:45 +0000758 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
759 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100760 }
Damiend99b0522013-12-21 18:17:45 +0000761 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100762 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000763 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100764 if (assign_kind == ASSIGN_AUG_STORE) {
765 EMIT(rot_three);
766 EMIT(store_subscr);
767 } else {
768 compile_node(comp, pns1->nodes[0]);
769 if (assign_kind == ASSIGN_AUG_LOAD) {
770 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100771 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100772 } else {
773 EMIT(store_subscr);
774 }
775 }
Damiend99b0522013-12-21 18:17:45 +0000776 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
777 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100778 if (assign_kind == ASSIGN_AUG_LOAD) {
779 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000780 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100781 } else {
782 if (assign_kind == ASSIGN_AUG_STORE) {
783 EMIT(rot_two);
784 }
Damien Georgeb9791222014-01-23 00:34:21 +0000785 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100786 }
787 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100788 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100789 }
790 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100791 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100792 }
793
Damiend99b0522013-12-21 18:17:45 +0000794 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100795 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100796 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100797
798 return;
799
800cannot_assign:
801 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100802}
803
Damien George0288cf02014-04-11 11:53:00 +0000804// 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 +0100805STATIC 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 +0000806 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
807
808 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100809 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000810 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
811 EMIT_ARG(unpack_ex, 0, num_tail);
812 have_star_index = 0;
813 }
814 for (int i = 0; i < num_tail; i++) {
815 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100816 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000817 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
818 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100819 } else {
Damien George9d181f62014-04-27 16:55:27 +0100820 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100821 return;
822 }
823 }
824 }
825 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000826 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100827 }
Damien George0288cf02014-04-11 11:53:00 +0000828 if (num_head != 0) {
829 if (0 == have_star_index) {
830 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100831 } else {
Damien George0288cf02014-04-11 11:53:00 +0000832 c_assign(comp, node_head, ASSIGN_STORE);
833 }
834 }
835 for (int i = 0; i < num_tail; i++) {
836 if (num_head + i == have_star_index) {
837 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
838 } else {
839 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100840 }
841 }
842}
843
844// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100845STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100846 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000847 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100848 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000849 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
850 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000851 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100852 switch (assign_kind) {
853 case ASSIGN_STORE:
854 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000855 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100856 break;
857 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000858 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100859 break;
860 }
861 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100862 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100863 return;
864 }
865 } else {
Damiend99b0522013-12-21 18:17:45 +0000866 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
867 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100868 case PN_power:
869 // lhs is an index or attribute
870 c_assign_power(comp, pns, assign_kind);
871 break;
872
873 case PN_testlist_star_expr:
874 case PN_exprlist:
875 // lhs is a tuple
876 if (assign_kind != ASSIGN_STORE) {
877 goto bad_aug;
878 }
Damien George0288cf02014-04-11 11:53:00 +0000879 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100880 break;
881
882 case PN_atom_paren:
883 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000884 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100885 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100886 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000887 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
888 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100889 goto testlist_comp;
890 } else {
891 // parenthesis around 1 item, is just that item
892 pn = pns->nodes[0];
893 goto tail_recursion;
894 }
895 break;
896
897 case PN_atom_bracket:
898 // lhs is something in brackets
899 if (assign_kind != ASSIGN_STORE) {
900 goto bad_aug;
901 }
Damiend99b0522013-12-21 18:17:45 +0000902 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100903 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000904 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000905 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
906 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100907 goto testlist_comp;
908 } else {
909 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000910 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100911 }
912 break;
913
914 default:
Damien George9d181f62014-04-27 16:55:27 +0100915 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100916 }
917 return;
918
919 testlist_comp:
920 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000921 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
922 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
923 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100924 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000925 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000926 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000927 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100928 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000929 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
930 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000931 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100932 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100933 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100934 } else {
935 // sequence with 2 items
936 goto sequence_with_2_items;
937 }
938 } else {
939 // sequence with 2 items
940 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000941 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100942 }
943 return;
944 }
945 return;
946
Damien George9d181f62014-04-27 16:55:27 +0100947 cannot_assign:
948 compile_syntax_error(comp, pn, "can't assign to expression");
949 return;
950
Damien429d7192013-10-04 19:53:11 +0100951 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100952 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100953}
954
955// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100956// if we are not in CPython compatibility mode then:
957// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
958// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
959// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100960STATIC 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 +0100961 assert(n_pos_defaults >= 0);
962 assert(n_kw_defaults >= 0);
963
Damien429d7192013-10-04 19:53:11 +0100964 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000965 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100966 int nfree = 0;
967 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000968 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
969 id_info_t *id = &comp->scope_cur->id_info[i];
970 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
971 for (int j = 0; j < this_scope->id_info_len; j++) {
972 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100973 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000974#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100975 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000976#else
977 // in Micro Python we load closures using LOAD_FAST
Damien George7ff996c2014-09-08 23:05:16 +0100978 EMIT_ARG(load_fast, id->qst, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000979#endif
Damien318aec62013-12-10 18:28:17 +0000980 nfree += 1;
981 }
982 }
Damien429d7192013-10-04 19:53:11 +0100983 }
984 }
985 }
Damien429d7192013-10-04 19:53:11 +0100986
987 // make the function/closure
988 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100989 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100990 } else {
Damien George3558f622014-04-20 17:50:40 +0100991 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100992 }
993}
994
Damien George6be0b0a2014-08-15 14:30:52 +0100995STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000996 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100997 comp->have_star = true;
998 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000999 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1000 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001001 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001002 } else {
1003 // named star
Damien429d7192013-10-04 19:53:11 +01001004 }
Damien George8b19db02014-04-11 23:25:34 +01001005 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001006
1007 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001008 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001009 // TODO do we need to do anything with this?
1010
1011 } else {
1012 mp_parse_node_t pn_id;
1013 mp_parse_node_t pn_colon;
1014 mp_parse_node_t pn_equal;
1015 if (MP_PARSE_NODE_IS_ID(pn)) {
1016 // this parameter is just an id
1017
1018 pn_id = pn;
1019 pn_colon = MP_PARSE_NODE_NULL;
1020 pn_equal = MP_PARSE_NODE_NULL;
1021
1022 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1023 // this parameter has a colon and/or equal specifier
1024
1025 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1026 pn_id = pns->nodes[0];
1027 pn_colon = pns->nodes[1];
1028 pn_equal = pns->nodes[2];
1029
1030 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001031 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001032 assert(0);
1033 return;
1034 }
1035
1036 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1037 // this parameter does not have a default value
1038
1039 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001040 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001041 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001042 return;
1043 }
1044
1045 } else {
1046 // this parameter has a default value
1047 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1048
Damien George8b19db02014-04-11 23:25:34 +01001049 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001050 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001051#if MICROPY_EMIT_CPYTHON
1052 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1053 compile_node(comp, pn_equal);
1054#else
Damien George69b89d22014-04-11 13:38:30 +00001055 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1056 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001057 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1058 // we need to do this here before we start building the map for the default keywords
1059 if (comp->num_default_params > 0) {
1060 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001061 } else {
1062 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001063 }
Damien George69b89d22014-04-11 13:38:30 +00001064 // first default dict param, so make the map
1065 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001066 }
Damien Georgef0778a72014-06-07 22:01:00 +01001067
1068 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001069 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001070 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001071 EMIT(store_map);
1072#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001073 } else {
Damien George69b89d22014-04-11 13:38:30 +00001074 comp->num_default_params += 1;
1075 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001076 }
1077 }
1078
1079 // TODO pn_colon not implemented
1080 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001081 }
1082}
1083
1084// leaves function object on stack
1085// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001086STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001087 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001088 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001089 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001090 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001091 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001092 }
1093
1094 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001095 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001096 uint old_num_dict_params = comp->num_dict_params;
1097 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001098
1099 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001100 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001101 comp->num_dict_params = 0;
1102 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001103 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001104
Damien Georgea91ac202014-10-05 19:01:34 +01001105 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001106 return MP_QSTR_NULL;
1107 }
1108
Damien Georgee337f1e2014-03-31 15:18:37 +01001109#if !MICROPY_EMIT_CPYTHON
1110 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001111 // the default keywords args may have already made the tuple; if not, do it now
1112 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001113 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001114 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001115 }
1116#endif
1117
Damien429d7192013-10-04 19:53:11 +01001118 // get the scope for this function
1119 scope_t *fscope = (scope_t*)pns->nodes[4];
1120
1121 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001122 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001123
1124 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001125 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001126 comp->num_dict_params = old_num_dict_params;
1127 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001128
1129 // return its name (the 'f' in "def f(...):")
1130 return fscope->simple_name;
1131}
1132
1133// leaves class object on stack
1134// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001135STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001136 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001137 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001138 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001139 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001140 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001141 }
1142
1143 EMIT(load_build_class);
1144
1145 // scope for this class
1146 scope_t *cscope = (scope_t*)pns->nodes[3];
1147
1148 // compile the class
1149 close_over_variables_etc(comp, cscope, 0, 0);
1150
1151 // get its name
Damien George968bf342014-04-27 19:12:05 +01001152 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001153
1154 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001155 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1156 mp_parse_node_t parents = pns->nodes[1];
1157 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1158 parents = MP_PARSE_NODE_NULL;
1159 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001160 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001161 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001162
1163 // return its name (the 'C' in class C(...):")
1164 return cscope->simple_name;
1165}
1166
Damien6cdd3af2013-10-05 18:08:26 +01001167// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001168STATIC 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 +00001169 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001170 return false;
1171 }
1172
1173 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001174 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001175 return true;
1176 }
1177
Damiend99b0522013-12-21 18:17:45 +00001178 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001179 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001180 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001181#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001182 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001183 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001184 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001185 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001186#endif
Damien3ef4abb2013-10-12 16:53:13 +01001187#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001188 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001189 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001190#endif
Damien6cdd3af2013-10-05 18:08:26 +01001191 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001192 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001193 }
1194
1195 return true;
1196}
1197
Damien George969a6b32014-12-10 22:07:04 +00001198STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001199 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001200 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001201 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1202
Damien6cdd3af2013-10-05 18:08:26 +01001203 // inherit emit options for this function/class definition
1204 uint emit_options = comp->scope_cur->emit_options;
1205
1206 // compile each decorator
1207 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001208 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001209 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1210 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001211
1212 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001213 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001214 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1215
1216 // check for built-in decorators
1217 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1218 // this was a built-in
1219 num_built_in_decorators += 1;
1220
1221 } else {
1222 // not a built-in, compile normally
1223
1224 // compile the decorator function
1225 compile_node(comp, name_nodes[0]);
1226 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001227 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001228 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001229 }
1230
1231 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001232 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001233 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001234 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001235 compile_node(comp, pns_decorator->nodes[1]);
1236 }
Damien429d7192013-10-04 19:53:11 +01001237 }
1238 }
1239
1240 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001241 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001242 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001243 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001244 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001245 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001246 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001247 } else {
1248 // shouldn't happen
1249 assert(0);
1250 }
1251
1252 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001253 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001254 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001255 }
1256
1257 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001258 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001259}
1260
Damien George969a6b32014-12-10 22:07:04 +00001261STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001262 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001263 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001264 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001265}
1266
Damien George6be0b0a2014-08-15 14:30:52 +01001267STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001268 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001269 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001270 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1271 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001272
1273 compile_node(comp, pns->nodes[0]); // base of the power node
1274
Damiend99b0522013-12-21 18:17:45 +00001275 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1276 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1277 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1278 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001279 for (int i = 0; i < n - 1; i++) {
1280 compile_node(comp, pns1->nodes[i]);
1281 }
Damiend99b0522013-12-21 18:17:45 +00001282 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1283 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001284 }
Damiend99b0522013-12-21 18:17:45 +00001285 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001286 // can't delete function calls
1287 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001288 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001289 compile_node(comp, pns1->nodes[0]);
1290 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001291 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1292 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001293 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001294 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001295 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001296 }
1297 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001298 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001299 }
1300
Damiend99b0522013-12-21 18:17:45 +00001301 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001302 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001303 }
Damiend99b0522013-12-21 18:17:45 +00001304 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1305 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1306 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1307 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001308 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1309
Damiend99b0522013-12-21 18:17:45 +00001310 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1311 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1312 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001313 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001314 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001315 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001316 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001317 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001318 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001319 c_del_stmt(comp, pns->nodes[0]);
1320 for (int i = 0; i < n; i++) {
1321 c_del_stmt(comp, pns1->nodes[i]);
1322 }
Damiend99b0522013-12-21 18:17:45 +00001323 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001324 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001325 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001326 } else {
1327 // sequence with 2 items
1328 goto sequence_with_2_items;
1329 }
1330 } else {
1331 // sequence with 2 items
1332 sequence_with_2_items:
1333 c_del_stmt(comp, pns->nodes[0]);
1334 c_del_stmt(comp, pns->nodes[1]);
1335 }
1336 } else {
1337 // tuple with 1 element
1338 c_del_stmt(comp, pn);
1339 }
1340 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001341 // TODO is there anything else to implement?
1342 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001343 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001344
1345 return;
1346
1347cannot_delete:
1348 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001349}
1350
Damien George969a6b32014-12-10 22:07:04 +00001351STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001352 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1353}
1354
Damien George969a6b32014-12-10 22:07:04 +00001355STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001356 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001357 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001358 }
Damien George090c9232014-10-17 14:08:49 +00001359 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001360 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001361}
1362
Damien George969a6b32014-12-10 22:07:04 +00001363STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001364 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001365 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001366 }
Damien George090c9232014-10-17 14:08:49 +00001367 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001368 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001369}
1370
Damien George969a6b32014-12-10 22:07:04 +00001371STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001372 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001373 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001374 return;
1375 }
Damiend99b0522013-12-21 18:17:45 +00001376 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001377 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001378 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001379 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001380 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001381 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1382 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 +01001383
Damien George6f355fd2014-04-10 14:11:31 +01001384 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001385 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1386 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1387 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001388 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001389 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1390 } else {
1391 compile_node(comp, pns->nodes[0]);
1392 }
1393 EMIT(return_value);
1394}
1395
Damien George969a6b32014-12-10 22:07:04 +00001396STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001397 compile_node(comp, pns->nodes[0]);
1398 EMIT(pop_top);
1399}
1400
Damien George969a6b32014-12-10 22:07:04 +00001401STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001402 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001403 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001404 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001405 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001406 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001407 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001408 compile_node(comp, pns->nodes[0]);
1409 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001410 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001411 } else {
1412 // raise x
1413 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001414 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001415 }
1416}
1417
Damien George635543c2014-04-10 12:56:52 +01001418// q_base holds the base of the name
1419// eg a -> q_base=a
1420// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001421STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001422 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001423 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1424 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001425 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001426 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001427 pn = pns->nodes[0];
1428 is_as = true;
1429 }
Damien George635543c2014-04-10 12:56:52 +01001430 if (MP_PARSE_NODE_IS_NULL(pn)) {
1431 // empty name (eg, from . import x)
1432 *q_base = MP_QSTR_;
1433 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1434 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001435 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001436 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001437 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001438 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001439 }
Damien George635543c2014-04-10 12:56:52 +01001440 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001441 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1442 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1443 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001444 // a name of the form a.b.c
1445 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001446 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001447 }
Damiend99b0522013-12-21 18:17:45 +00001448 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001449 int len = n - 1;
1450 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001451 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001452 }
Damien George55baff42014-01-21 21:40:13 +00001453 byte *q_ptr;
1454 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001455 for (int i = 0; i < n; i++) {
1456 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001457 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001458 }
Damien George39dc1452014-10-03 19:52:22 +01001459 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001460 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001461 memcpy(str_dest, str_src, str_src_len);
1462 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001463 }
Damien George635543c2014-04-10 12:56:52 +01001464 qstr q_full = qstr_build_end(q_ptr);
1465 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001466 if (is_as) {
1467 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001468 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001469 }
1470 }
1471 } else {
Damien George635543c2014-04-10 12:56:52 +01001472 // shouldn't happen
1473 assert(0);
Damien429d7192013-10-04 19:53:11 +01001474 }
1475 } else {
Damien George635543c2014-04-10 12:56:52 +01001476 // shouldn't happen
1477 assert(0);
Damien429d7192013-10-04 19:53:11 +01001478 }
1479}
1480
Damien George6be0b0a2014-08-15 14:30:52 +01001481STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001482 EMIT_ARG(load_const_small_int, 0); // level 0 import
1483 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1484 qstr q_base;
1485 do_import_name(comp, pn, &q_base);
1486 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001487}
1488
Damien George969a6b32014-12-10 22:07:04 +00001489STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001490 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1491}
1492
Damien George969a6b32014-12-10 22:07:04 +00001493STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001494 mp_parse_node_t pn_import_source = pns->nodes[0];
1495
1496 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1497 uint import_level = 0;
1498 do {
1499 mp_parse_node_t pn_rel;
1500 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)) {
1501 // This covers relative imports with dots only like "from .. import"
1502 pn_rel = pn_import_source;
1503 pn_import_source = MP_PARSE_NODE_NULL;
1504 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1505 // This covers relative imports starting with dot(s) like "from .foo import"
1506 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1507 pn_rel = pns_2b->nodes[0];
1508 pn_import_source = pns_2b->nodes[1];
1509 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1510 } else {
1511 // Not a relative import
1512 break;
1513 }
1514
1515 // get the list of . and/or ...'s
1516 mp_parse_node_t *nodes;
1517 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1518
1519 // count the total number of .'s
1520 for (int i = 0; i < n; i++) {
1521 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1522 import_level++;
1523 } else {
1524 // should be an MP_TOKEN_ELLIPSIS
1525 import_level += 3;
1526 }
1527 }
1528 } while (0);
1529
Damiend99b0522013-12-21 18:17:45 +00001530 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001531 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001532
1533 // build the "fromlist" tuple
1534#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001535 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001536#else
Damien George708c0732014-04-27 19:23:46 +01001537 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001538 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001539#endif
1540
1541 // do the import
Damien George635543c2014-04-10 12:56:52 +01001542 qstr dummy_q;
1543 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001544 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001545
Damien429d7192013-10-04 19:53:11 +01001546 } else {
Damien George635543c2014-04-10 12:56:52 +01001547 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001548
1549 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001550 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001551 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001552#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001553 {
1554 vstr_t *vstr = vstr_new();
1555 vstr_printf(vstr, "(");
1556 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001557 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1558 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1559 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001560 if (i > 0) {
1561 vstr_printf(vstr, ", ");
1562 }
1563 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001564 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001565 const byte *str = qstr_data(id2, &len);
1566 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001567 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001568 }
Damien02f89412013-12-12 15:13:36 +00001569 if (n == 1) {
1570 vstr_printf(vstr, ",");
1571 }
1572 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001573 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001574 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001575 }
Damiendb4c3612013-12-10 17:27:24 +00001576#else
1577 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001578 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1579 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1580 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001581 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001582 }
Damien Georgeb9791222014-01-23 00:34:21 +00001583 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001584#endif
1585
1586 // do the import
Damien George635543c2014-04-10 12:56:52 +01001587 qstr dummy_q;
1588 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001589 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001590 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1591 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1592 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001593 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001594 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001595 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001596 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001597 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001598 }
1599 }
1600 EMIT(pop_top);
1601 }
1602}
1603
Damien George584ba672014-12-21 17:26:45 +00001604STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1605 bool added;
1606 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1607 if (!added) {
1608 compile_syntax_error(comp, pn, "identifier already used");
1609 return;
1610 }
1611 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1612
1613 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1614 id_info = scope_find_global(comp->scope_cur, qst);
1615 if (id_info != NULL) {
1616 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1617 }
1618}
1619
Damien George969a6b32014-12-10 22:07:04 +00001620STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001621 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001622 mp_parse_node_t *nodes;
1623 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1624 for (int i = 0; i < n; i++) {
1625 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001626 }
1627 }
1628}
1629
Damien George584ba672014-12-21 17:26:45 +00001630STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1631 bool added;
1632 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1633 if (!added) {
1634 compile_syntax_error(comp, pn, "identifier already used");
1635 return;
1636 }
1637 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1638 if (id_info2 == NULL || !(id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) {
1639 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1640 return;
1641 }
1642 id_info->kind = ID_INFO_KIND_FREE;
1643 scope_close_over_in_parents(comp->scope_cur, qst);
1644}
1645
Damien George969a6b32014-12-10 22:07:04 +00001646STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001647 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001648 if (comp->scope_cur->kind == SCOPE_MODULE) {
1649 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1650 return;
1651 }
1652 mp_parse_node_t *nodes;
1653 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1654 for (int i = 0; i < n; i++) {
1655 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001656 }
1657 }
1658}
1659
Damien George969a6b32014-12-10 22:07:04 +00001660STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001661 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001662 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001663 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 +00001664 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001665 // assertion message
1666 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001667 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001668 }
Damien Georgeb9791222014-01-23 00:34:21 +00001669 EMIT_ARG(raise_varargs, 1);
1670 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001671}
1672
Damien George969a6b32014-12-10 22:07:04 +00001673STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001674 // TODO proper and/or short circuiting
1675
Damien George6f355fd2014-04-10 14:11:31 +01001676 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001677
Damien George391db862014-10-17 17:57:33 +00001678 // optimisation: don't emit anything when "if False" (not in CPython)
1679 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1680 uint l_fail = comp_next_label(comp);
1681 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001682
Damien George391db862014-10-17 17:57:33 +00001683 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001684
Damien George391db862014-10-17 17:57:33 +00001685 // optimisation: skip everything else when "if True" (not in CPython)
1686 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1687 goto done;
1688 }
1689
1690 if (
1691 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1692 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1693 // optimisation: don't jump if last instruction was return
1694 && !EMIT(last_emit_was_return_value)
1695 ) {
1696 // jump over elif/else blocks
1697 EMIT_ARG(jump, l_end);
1698 }
1699
1700 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001701 }
1702
Damien George235f9b32014-10-17 17:30:16 +00001703 // compile elif blocks (if any)
1704 mp_parse_node_t *pn_elif;
1705 int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1706 for (int i = 0; i < n_elif; i++) {
1707 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1708 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001709
Damien George391db862014-10-17 17:57:33 +00001710 // optimisation: don't emit anything when "if False" (not in CPython)
1711 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1712 uint l_fail = comp_next_label(comp);
1713 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001714
Damien George391db862014-10-17 17:57:33 +00001715 compile_node(comp, pns_elif->nodes[1]); // elif block
1716
1717 // optimisation: skip everything else when "elif True" (not in CPython)
1718 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1719 goto done;
1720 }
1721
1722 // optimisation: don't jump if last instruction was return
1723 if (!EMIT(last_emit_was_return_value)) {
1724 EMIT_ARG(jump, l_end);
1725 }
1726 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001727 }
1728 }
1729
1730 // compile else block
1731 compile_node(comp, pns->nodes[3]); // can be null
1732
Damien George391db862014-10-17 17:57:33 +00001733done:
Damien Georgeb9791222014-01-23 00:34:21 +00001734 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001735}
1736
Damien Georgecbddb272014-02-01 20:08:18 +00001737#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001738 uint16_t old_break_label = comp->break_label; \
1739 uint16_t old_continue_label = comp->continue_label; \
1740 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001741 uint break_label = comp_next_label(comp); \
1742 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001743 comp->break_label = break_label; \
1744 comp->continue_label = continue_label; \
1745 comp->break_continue_except_level = comp->cur_except_level;
1746
1747#define END_BREAK_CONTINUE_BLOCK \
1748 comp->break_label = old_break_label; \
1749 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001750 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001751
Damien George969a6b32014-12-10 22:07:04 +00001752STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001753 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001754
Damience89a212013-10-15 22:25:17 +01001755 // compared to CPython, we have an optimised version of while loops
1756#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001757 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001758 EMIT_ARG(setup_loop, break_label);
1759 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001760 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1761 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001762 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001763 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001764 }
Damien Georgeb9791222014-01-23 00:34:21 +00001765 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001766 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1767 // this is a small hack to agree with CPython
1768 if (!node_is_const_true(pns->nodes[0])) {
1769 EMIT(pop_block);
1770 }
Damience89a212013-10-15 22:25:17 +01001771#else
Damien George391db862014-10-17 17:57:33 +00001772 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1773 uint top_label = comp_next_label(comp);
1774 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1775 EMIT_ARG(jump, continue_label);
1776 }
1777 EMIT_ARG(label_assign, top_label);
1778 compile_node(comp, pns->nodes[1]); // body
1779 EMIT_ARG(label_assign, continue_label);
1780 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1781 }
Damience89a212013-10-15 22:25:17 +01001782#endif
1783
1784 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001785 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001786
1787 compile_node(comp, pns->nodes[2]); // else
1788
Damien Georgeb9791222014-01-23 00:34:21 +00001789 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001790}
1791
Damien George2ac4af62014-08-15 16:45:41 +01001792#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001793// This function compiles an optimised for-loop of the form:
1794// for <var> in range(<start>, <end>, <step>):
1795// <body>
1796// else:
1797// <else>
1798// <var> must be an identifier and <step> must be a small-int.
1799//
1800// Semantics of for-loop require:
1801// - final failing value should not be stored in the loop variable
1802// - if the loop never runs, the loop variable should never be assigned
1803// - assignments to <var>, <end> or <step> in the body do not alter the loop
1804// (<step> is a constant for us, so no need to worry about it changing)
1805//
1806// If <end> is a small-int, then the stack during the for-loop contains just
1807// the current value of <var>. Otherwise, the stack contains <end> then the
1808// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001809STATIC 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 +00001810 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001811
Damien George6f355fd2014-04-10 14:11:31 +01001812 uint top_label = comp_next_label(comp);
1813 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001814
Damien Georgee181c0d2014-12-12 17:19:56 +00001815 // put the end value on the stack if it's not a small-int constant
1816 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1817 if (end_on_stack) {
1818 compile_node(comp, pn_end);
1819 }
1820
1821 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001822 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001823
Damien Georgeb9791222014-01-23 00:34:21 +00001824 EMIT_ARG(jump, entry_label);
1825 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001826
Damien Georgec33ce602014-12-11 17:35:23 +00001827 // duplicate next value and store it to var
1828 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001829 c_assign(comp, pn_var, ASSIGN_STORE);
1830
Damienf3822fc2013-11-09 20:12:03 +00001831 // compile body
1832 compile_node(comp, pn_body);
1833
Damien Georgeb9791222014-01-23 00:34:21 +00001834 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001835
Damien Georgee181c0d2014-12-12 17:19:56 +00001836 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001837 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001838 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001839
Damien Georgeb9791222014-01-23 00:34:21 +00001840 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001841
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001842 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001843 if (end_on_stack) {
1844 EMIT(dup_top_two);
1845 EMIT(rot_two);
1846 } else {
1847 EMIT(dup_top);
1848 compile_node(comp, pn_end);
1849 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001850 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1851 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001852 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001853 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001854 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001855 }
Damien Georgeb9791222014-01-23 00:34:21 +00001856 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001857
1858 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001859 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001860
1861 compile_node(comp, pn_else);
1862
Damien Georgeb9791222014-01-23 00:34:21 +00001863 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001864
1865 // discard final value of var that failed the loop condition
1866 EMIT(pop_top);
1867
1868 // discard <end> value if it's on the stack
1869 if (end_on_stack) {
1870 EMIT(pop_top);
1871 }
Damienf72fd0e2013-11-06 20:20:49 +00001872}
Damien George2ac4af62014-08-15 16:45:41 +01001873#endif
Damienf72fd0e2013-11-06 20:20:49 +00001874
Damien George969a6b32014-12-10 22:07:04 +00001875STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001876#if !MICROPY_EMIT_CPYTHON
1877 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1878 // this is actually slower, but uses no heap memory
1879 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001880 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 +00001881 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001882 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1883 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1884 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1885 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001886 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1887 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001888 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001889 mp_parse_node_t pn_range_start;
1890 mp_parse_node_t pn_range_end;
1891 mp_parse_node_t pn_range_step;
1892 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001893 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001894 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001895 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001896 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001897 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001898 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001899 } else if (n_args == 2) {
1900 pn_range_start = args[0];
1901 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001902 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001903 } else {
1904 pn_range_start = args[0];
1905 pn_range_end = args[1];
1906 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001907 // We need to know sign of step. This is possible only if it's constant
1908 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1909 optimize = false;
1910 }
Damienf72fd0e2013-11-06 20:20:49 +00001911 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001912 }
1913 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001914 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1915 return;
1916 }
1917 }
1918 }
1919#endif
1920
Damien Georgecbddb272014-02-01 20:08:18 +00001921 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001922 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001923
Damien George6f355fd2014-04-10 14:11:31 +01001924 uint pop_label = comp_next_label(comp);
1925 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001926
Damience89a212013-10-15 22:25:17 +01001927 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1928#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001929 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001930#endif
1931
Damien429d7192013-10-04 19:53:11 +01001932 compile_node(comp, pns->nodes[1]); // iterator
1933 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001934 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001935 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001936 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1937 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001938 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001939 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001940 }
Damien Georgeb9791222014-01-23 00:34:21 +00001941 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001942 EMIT(for_iter_end);
1943
1944 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001945 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001946
Damience89a212013-10-15 22:25:17 +01001947#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001948 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001949#endif
Damien429d7192013-10-04 19:53:11 +01001950
1951 compile_node(comp, pns->nodes[3]); // else (not tested)
1952
Damien Georgeb9791222014-01-23 00:34:21 +00001953 EMIT_ARG(label_assign, break_label);
1954 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001955}
1956
Damien George6be0b0a2014-08-15 14:30:52 +01001957STATIC 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 +01001958 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001959 uint l1 = comp_next_label(comp);
1960 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001961
Damien Georgeb9791222014-01-23 00:34:21 +00001962 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001963 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001964
Damien429d7192013-10-04 19:53:11 +01001965 compile_node(comp, pn_body); // body
1966 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001967 EMIT_ARG(jump, success_label); // jump over exception handler
1968
1969 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001970 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001971
Damien George6f355fd2014-04-10 14:11:31 +01001972 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001973
1974 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001975 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1976 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001977
1978 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001979 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001980
Damiend99b0522013-12-21 18:17:45 +00001981 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001982 // this is a catch all exception handler
1983 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001984 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001985 return;
1986 }
1987 } else {
1988 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001989 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1990 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1991 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1992 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001993 // handler binds the exception to a local
1994 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001995 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001996 }
1997 }
1998 EMIT(dup_top);
1999 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002000 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00002001 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002002 }
2003
2004 EMIT(pop_top);
2005
2006 if (qstr_exception_local == 0) {
2007 EMIT(pop_top);
2008 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002009 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002010 }
2011
2012 EMIT(pop_top);
2013
Damien George6f355fd2014-04-10 14:11:31 +01002014 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002015 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002016 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002017 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002018 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002019 }
2020 compile_node(comp, pns_except->nodes[1]);
2021 if (qstr_exception_local != 0) {
2022 EMIT(pop_block);
2023 }
2024 EMIT(pop_except);
2025 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002026 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2027 EMIT_ARG(label_assign, l3);
2028 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2029 EMIT_ARG(store_id, qstr_exception_local);
2030 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002031
Damien George8dcc0c72014-03-27 10:55:21 +00002032 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002033 EMIT(end_finally);
2034 }
Damien Georgeb9791222014-01-23 00:34:21 +00002035 EMIT_ARG(jump, l2);
2036 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002037 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002038 }
2039
Damien George8dcc0c72014-03-27 10:55:21 +00002040 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002041 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002042 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002043
Damien Georgeb9791222014-01-23 00:34:21 +00002044 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002045 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002046 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002047}
2048
Damien George6be0b0a2014-08-15 14:30:52 +01002049STATIC 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 +01002050 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002051
Damien Georgeb9791222014-01-23 00:34:21 +00002052 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002053 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002054
Damien429d7192013-10-04 19:53:11 +01002055 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002056 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002057 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002058 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002059 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002060 } else {
2061 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2062 }
2063 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002064 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2065 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002066 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002067
Damien George8dcc0c72014-03-27 10:55:21 +00002068 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002069 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002070}
2071
Damien George969a6b32014-12-10 22:07:04 +00002072STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002073 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2074 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2075 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002076 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002077 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2078 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002079 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002080 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002081 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002082 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002083 // no finally
2084 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2085 } else {
2086 // have finally
Damiend99b0522013-12-21 18:17:45 +00002087 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 +01002088 }
2089 } else {
2090 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002091 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002092 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002093 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002094 }
2095 } else {
2096 // shouldn't happen
2097 assert(0);
2098 }
2099}
2100
Damien George6be0b0a2014-08-15 14:30:52 +01002101STATIC 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 +01002102 if (n == 0) {
2103 // no more pre-bits, compile the body of the with
2104 compile_node(comp, body);
2105 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002106 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002107 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002108 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002109 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002110 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002111 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002112 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2113 } else {
2114 // this pre-bit is just an expression
2115 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002116 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002117 EMIT(pop_top);
2118 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002119 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002120 // compile additional pre-bits and the body
2121 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2122 // finish this with block
2123 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002124 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2125 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002126 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002127 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002128 EMIT(end_finally);
2129 }
2130}
2131
Damien George969a6b32014-12-10 22:07:04 +00002132STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002133 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002134 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002135 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2136 assert(n > 0);
2137
2138 // compile in a nested fashion
2139 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2140}
2141
Damien George969a6b32014-12-10 22:07:04 +00002142STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002143 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002144 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2145 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002146 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002147 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002148 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002149 EMIT(pop_top);
2150
Damien429d7192013-10-04 19:53:11 +01002151 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002152 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002153 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2154 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002155 // do nothing with a lonely constant
2156 } else {
2157 compile_node(comp, pns->nodes[0]); // just an expression
2158 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2159 }
Damien429d7192013-10-04 19:53:11 +01002160 }
2161 } else {
Damiend99b0522013-12-21 18:17:45 +00002162 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2163 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002164 if (kind == PN_expr_stmt_augassign) {
2165 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2166 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002167 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002168 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002169 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002170 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2171 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2172 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2173 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2174 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2175 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2176 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2177 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2178 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2179 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2180 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2181 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2182 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002183 }
Damien George7e5fb242014-02-01 22:18:47 +00002184 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002185 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2186 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002187 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2188 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002189 // following CPython, we store left-most first
2190 if (rhs > 0) {
2191 EMIT(dup_top);
2192 }
2193 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2194 for (int i = 0; i < rhs; i++) {
2195 if (i + 1 < rhs) {
2196 EMIT(dup_top);
2197 }
Damiend99b0522013-12-21 18:17:45 +00002198 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002199 }
2200 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002201 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002202 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2203 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2204 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002205 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002206 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2207 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002208 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2209 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2210 // can't optimise when it's a star expression on the lhs
2211 goto no_optimisation;
2212 }
Damien429d7192013-10-04 19:53:11 +01002213 compile_node(comp, pns10->nodes[0]); // rhs
2214 compile_node(comp, pns10->nodes[1]); // rhs
2215 EMIT(rot_two);
2216 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2217 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002218 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2219 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2220 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2221 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002222 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002223 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2224 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002225 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2226 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2227 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2228 // can't optimise when it's a star expression on the lhs
2229 goto no_optimisation;
2230 }
Damien429d7192013-10-04 19:53:11 +01002231 compile_node(comp, pns10->nodes[0]); // rhs
2232 compile_node(comp, pns10->nodes[1]); // rhs
2233 compile_node(comp, pns10->nodes[2]); // rhs
2234 EMIT(rot_three);
2235 EMIT(rot_two);
2236 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2237 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2238 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2239 } else {
Damien George495d7812014-04-08 17:51:47 +01002240 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002241 compile_node(comp, pns1->nodes[0]); // rhs
2242 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2243 }
2244 } else {
2245 // shouldn't happen
2246 assert(0);
2247 }
2248 }
2249}
2250
Damien George6be0b0a2014-08-15 14:30:52 +01002251STATIC 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 +00002252 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002253 compile_node(comp, pns->nodes[0]);
2254 for (int i = 1; i < num_nodes; i += 1) {
2255 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002256 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002257 }
2258}
2259
Damien George969a6b32014-12-10 22:07:04 +00002260STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002261 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2262 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002263
Damien George6f355fd2014-04-10 14:11:31 +01002264 uint l_fail = comp_next_label(comp);
2265 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002266 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2267 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002268 EMIT_ARG(jump, l_end);
2269 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002270 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002271 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002272 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002273}
2274
Damien George969a6b32014-12-10 22:07:04 +00002275STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002276 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002277 //mp_parse_node_t pn_params = pns->nodes[0];
2278 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002279
Damien George36db6bc2014-05-07 17:24:22 +01002280 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002281 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002282 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 +01002283 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002284 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002285 }
2286
2287 // get the scope for this lambda
2288 scope_t *this_scope = (scope_t*)pns->nodes[2];
2289
2290 // make the lambda
2291 close_over_variables_etc(comp, this_scope, 0, 0);
2292}
2293
Damien George969a6b32014-12-10 22:07:04 +00002294STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002295 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002296 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002297 for (int i = 0; i < n; i += 1) {
2298 compile_node(comp, pns->nodes[i]);
2299 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002300 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002301 }
2302 }
Damien Georgeb9791222014-01-23 00:34:21 +00002303 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002304}
2305
Damien George969a6b32014-12-10 22:07:04 +00002306STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002307 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002308 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002309 for (int i = 0; i < n; i += 1) {
2310 compile_node(comp, pns->nodes[i]);
2311 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002312 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002313 }
2314 }
Damien Georgeb9791222014-01-23 00:34:21 +00002315 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002316}
2317
Damien George969a6b32014-12-10 22:07:04 +00002318STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002319 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002320 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002321}
2322
Damien George969a6b32014-12-10 22:07:04 +00002323STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002324 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002325 compile_node(comp, pns->nodes[0]);
2326 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002327 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002328 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002329 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002330 }
2331 for (int i = 1; i + 1 < num_nodes; i += 2) {
2332 compile_node(comp, pns->nodes[i + 1]);
2333 if (i + 2 < num_nodes) {
2334 EMIT(dup_top);
2335 EMIT(rot_three);
2336 }
Damien George7e5fb242014-02-01 22:18:47 +00002337 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002338 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002339 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002340 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2341 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2342 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2343 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2344 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2345 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2346 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2347 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002348 }
2349 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002350 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2351 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2352 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002353 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002354 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002355 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002356 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002357 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002358 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002359 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002360 }
2361 } else {
2362 // shouldn't happen
2363 assert(0);
2364 }
2365 } else {
2366 // shouldn't happen
2367 assert(0);
2368 }
2369 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002370 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002371 }
2372 }
2373 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002374 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002375 EMIT_ARG(jump, l_end);
2376 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002377 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002378 EMIT(rot_two);
2379 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002380 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002381 }
2382}
2383
Damien George969a6b32014-12-10 22:07:04 +00002384STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002385 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002386}
2387
Damien George969a6b32014-12-10 22:07:04 +00002388STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002389 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002390}
2391
Damien George969a6b32014-12-10 22:07:04 +00002392STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002393 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002394}
2395
Damien George969a6b32014-12-10 22:07:04 +00002396STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002397 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002398}
2399
Damien George969a6b32014-12-10 22:07:04 +00002400STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002401 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002402 compile_node(comp, pns->nodes[0]);
2403 for (int i = 1; i + 1 < num_nodes; i += 2) {
2404 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002405 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002406 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002407 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002408 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002409 } else {
2410 // shouldn't happen
2411 assert(0);
2412 }
2413 }
2414}
2415
Damien George969a6b32014-12-10 22:07:04 +00002416STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002417 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002418 compile_node(comp, pns->nodes[0]);
2419 for (int i = 1; i + 1 < num_nodes; i += 2) {
2420 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002421 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002422 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002423 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002424 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002425 } else {
2426 // shouldn't happen
2427 assert(0);
2428 }
2429 }
2430}
2431
Damien George969a6b32014-12-10 22:07:04 +00002432STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002433 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002434 compile_node(comp, pns->nodes[0]);
2435 for (int i = 1; i + 1 < num_nodes; i += 2) {
2436 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002437 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002438 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002439 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002440 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002441 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002442 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002443 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002444 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002445 } else {
2446 // shouldn't happen
2447 assert(0);
2448 }
2449 }
2450}
2451
Damien George969a6b32014-12-10 22:07:04 +00002452STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002453 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002454 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002455 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002456 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002457 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002458 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002459 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002460 } else {
2461 // shouldn't happen
2462 assert(0);
2463 }
2464}
2465
Damien George969a6b32014-12-10 22:07:04 +00002466STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002467 // this is to handle special super() call
2468 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2469
2470 compile_generic_all_nodes(comp, pns);
2471}
2472
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002473STATIC 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 +01002474 // function to call is on top of stack
2475
Damien George35e2a4e2014-02-05 00:51:47 +00002476#if !MICROPY_EMIT_CPYTHON
2477 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002478 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 +00002479 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002480 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002481 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002482 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002483 // first argument found; load it and call super
Damien George2bf7c092014-04-09 15:26:46 +01002484 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002485 EMIT_ARG(call_function, 2, 0, 0);
2486 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002487 }
2488 }
Damien George01039b52014-12-21 17:44:27 +00002489 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002490 return;
2491 }
2492#endif
2493
Damien George2c0842b2014-04-27 16:46:51 +01002494 // get the list of arguments
2495 mp_parse_node_t *args;
2496 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002497
Damien George2c0842b2014-04-27 16:46:51 +01002498 // compile the arguments
2499 // Rather than calling compile_node on the list, we go through the list of args
2500 // explicitly here so that we can count the number of arguments and give sensible
2501 // error messages.
2502 int n_positional = n_positional_extra;
2503 uint n_keyword = 0;
2504 uint star_flags = 0;
2505 for (int i = 0; i < n_args; i++) {
2506 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2507 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2508 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2509 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2510 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2511 return;
2512 }
2513 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2514 compile_node(comp, pns_arg->nodes[0]);
2515 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2516 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2517 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2518 return;
2519 }
2520 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2521 compile_node(comp, pns_arg->nodes[0]);
2522 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2523 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2524 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2525 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2526 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2527 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2528 return;
2529 }
Damien George968bf342014-04-27 19:12:05 +01002530 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002531 compile_node(comp, pns2->nodes[0]);
2532 n_keyword += 1;
2533 } else {
2534 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2535 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2536 n_positional++;
2537 }
2538 } else {
2539 goto normal_argument;
2540 }
2541 } else {
2542 normal_argument:
2543 if (n_keyword > 0) {
2544 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2545 return;
2546 }
2547 compile_node(comp, args[i]);
2548 n_positional++;
2549 }
Damien429d7192013-10-04 19:53:11 +01002550 }
2551
Damien George2c0842b2014-04-27 16:46:51 +01002552 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002553 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002554 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002555 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002556 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002557 }
Damien429d7192013-10-04 19:53:11 +01002558}
2559
Damien George969a6b32014-12-10 22:07:04 +00002560STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002561 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002562 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002563 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 +01002564 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002565 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2566 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002567 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002568 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002569 i += 1;
2570 } else {
2571 compile_node(comp, pns->nodes[i]);
2572 }
Damien George35e2a4e2014-02-05 00:51:47 +00002573 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002574 }
2575}
2576
Damien George969a6b32014-12-10 22:07:04 +00002577STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002578 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002579 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002580}
2581
Damien George969a6b32014-12-10 22:07:04 +00002582STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002583 // a list of strings
Damien63321742013-12-10 17:41:49 +00002584
2585 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002586 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002587 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002588 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002589 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002590 int pn_kind;
2591 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2592 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2593 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2594 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2595 } else {
2596 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2597 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2598 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2599 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002600 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002601 }
Damien63321742013-12-10 17:41:49 +00002602 if (i == 0) {
2603 string_kind = pn_kind;
2604 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002605 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002606 return;
2607 }
Damien429d7192013-10-04 19:53:11 +01002608 }
Damien63321742013-12-10 17:41:49 +00002609
Damien63321742013-12-10 17:41:49 +00002610 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002611 byte *q_ptr;
2612 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002613 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002614 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002615 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002616 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2617 memcpy(s_dest, s, s_len);
2618 s_dest += s_len;
2619 } else {
2620 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002621 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2622 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002623 }
Damien63321742013-12-10 17:41:49 +00002624 }
Damien George55baff42014-01-21 21:40:13 +00002625 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002626
Damien Georgeb9791222014-01-23 00:34:21 +00002627 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002628}
2629
2630// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002631STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002632 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2633 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2634 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002635
Damien George36db6bc2014-05-07 17:24:22 +01002636 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002637 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002638 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 +01002639 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002640 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002641 }
2642
2643 // get the scope for this comprehension
2644 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2645
2646 // compile the comprehension
2647 close_over_variables_etc(comp, this_scope, 0, 0);
2648
2649 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2650 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002651 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002652}
2653
Damien George969a6b32014-12-10 22:07:04 +00002654STATIC void compile_atom_paren(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 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002657 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2658 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2659 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2660 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2661 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2662 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2663 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002664 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002665 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002666 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002667 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002668 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002669 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002670 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002671 // generator expression
2672 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2673 } else {
2674 // tuple with 2 items
2675 goto tuple_with_2_items;
2676 }
2677 } else {
2678 // tuple with 2 items
2679 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002680 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002681 }
2682 } else {
2683 // parenthesis around a single item, is just that item
2684 compile_node(comp, pns->nodes[0]);
2685 }
2686}
2687
Damien George969a6b32014-12-10 22:07:04 +00002688STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002689 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002690 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002691 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002692 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2693 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2694 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2695 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2696 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002697 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002698 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002699 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002700 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002701 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002702 // list of many items
2703 compile_node(comp, pns2->nodes[0]);
2704 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002705 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002706 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002707 // list comprehension
2708 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2709 } else {
2710 // list with 2 items
2711 goto list_with_2_items;
2712 }
2713 } else {
2714 // list with 2 items
2715 list_with_2_items:
2716 compile_node(comp, pns2->nodes[0]);
2717 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002718 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002719 }
2720 } else {
2721 // list with 1 item
2722 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002723 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002724 }
2725}
2726
Damien George969a6b32014-12-10 22:07:04 +00002727STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002728 mp_parse_node_t pn = pns->nodes[0];
2729 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002730 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002731 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002732 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2733 pns = (mp_parse_node_struct_t*)pn;
2734 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002735 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002736 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002737 compile_node(comp, pn);
2738 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002739 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2740 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2741 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2742 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002743 // dict/set with multiple elements
2744
2745 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002746 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002747 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2748
2749 // first element sets whether it's a dict or set
2750 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002751 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002752 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002753 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002754 compile_node(comp, pns->nodes[0]);
2755 EMIT(store_map);
2756 is_dict = true;
2757 } else {
2758 // a set
2759 compile_node(comp, pns->nodes[0]); // 1st value of set
2760 is_dict = false;
2761 }
2762
2763 // process rest of elements
2764 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002765 mp_parse_node_t pn = nodes[i];
2766 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002767 compile_node(comp, pn);
2768 if (is_dict) {
2769 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002770 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002771 return;
2772 }
2773 EMIT(store_map);
2774 } else {
2775 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002776 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002777 return;
2778 }
2779 }
2780 }
2781
Damien Georgee37dcaa2014-12-27 17:07:16 +00002782 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002783 // if it's a set, build it
2784 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002785 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002786 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002787 #endif
Damiend99b0522013-12-21 18:17:45 +00002788 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002789 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002790 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002791 // a dictionary comprehension
2792 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2793 } else {
2794 // a set comprehension
2795 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2796 }
2797 } else {
2798 // shouldn't happen
2799 assert(0);
2800 }
2801 } else {
2802 // set with one element
2803 goto set_with_one_element;
2804 }
2805 } else {
2806 // set with one element
2807 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002808 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002809 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002810 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002811 #else
2812 assert(0);
2813 #endif
Damien429d7192013-10-04 19:53:11 +01002814 }
2815}
2816
Damien George969a6b32014-12-10 22:07:04 +00002817STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002818 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002819}
2820
Damien George969a6b32014-12-10 22:07:04 +00002821STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002822 // object who's index we want is on top of stack
2823 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002824 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002825}
2826
Damien George969a6b32014-12-10 22:07:04 +00002827STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002828 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002829 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002830}
2831
Damien George83204f32014-12-27 17:20:41 +00002832#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002833STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002834 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2835 mp_parse_node_t pn = pns->nodes[0];
2836 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002837 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002838 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2839 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002840 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2841 pns = (mp_parse_node_struct_t*)pn;
2842 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002843 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002844 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002845 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002846 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002847 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002848 } else {
2849 // [?::x]
2850 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002851 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002852 }
Damiend99b0522013-12-21 18:17:45 +00002853 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002854 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002855 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2856 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2857 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2858 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002859 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002861 } else {
2862 // [?:x:x]
2863 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002864 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002865 }
2866 } else {
2867 // [?:x]
2868 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002869 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002870 }
2871 } else {
2872 // [?:x]
2873 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002874 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002875 }
2876}
2877
Damien George969a6b32014-12-10 22:07:04 +00002878STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002879 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002880 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2881 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002882}
2883
Damien George969a6b32014-12-10 22:07:04 +00002884STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002885 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002886 compile_subscript_3_helper(comp, pns);
2887}
Damien George83204f32014-12-27 17:20:41 +00002888#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002889
Damien George969a6b32014-12-10 22:07:04 +00002890STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002891 // if this is called then we are compiling a dict key:value pair
2892 compile_node(comp, pns->nodes[1]); // value
2893 compile_node(comp, pns->nodes[0]); // key
2894}
2895
Damien George969a6b32014-12-10 22:07:04 +00002896STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002897 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002898 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002899 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002900}
2901
Damien George969a6b32014-12-10 22:07:04 +00002902STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002903 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002904 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002905 return;
2906 }
Damiend99b0522013-12-21 18:17:45 +00002907 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002908 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002909 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002910 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2911 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002912 compile_node(comp, pns->nodes[0]);
2913 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002914 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002915 EMIT(yield_from);
2916 } else {
2917 compile_node(comp, pns->nodes[0]);
2918 EMIT(yield_value);
2919 }
2920}
2921
Damiend99b0522013-12-21 18:17:45 +00002922typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002923STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002924#define nc NULL
2925#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002926#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002927#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002928#undef nc
2929#undef c
2930#undef DEF_RULE
2931};
2932
Damien George6be0b0a2014-08-15 14:30:52 +01002933STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002934 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002935 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002936 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002937 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002938 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002939 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002940 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002941 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002942 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002943 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2944 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2945 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2946 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002947 case MP_PARSE_NODE_TOKEN:
2948 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002949 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002950 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002951 // do nothing
2952 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002953 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002954 }
2955 break;
Damien429d7192013-10-04 19:53:11 +01002956 default: assert(0);
2957 }
2958 } else {
Damiend99b0522013-12-21 18:17:45 +00002959 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002960 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002961 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002962 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 +01002963 } else {
Damien George5042bce2014-05-25 22:06:06 +01002964 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2965 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002966#if MICROPY_DEBUG_PRINTERS
Damien George01039b52014-12-21 17:44:27 +00002967 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien George5042bce2014-05-25 22:06:06 +01002968 mp_parse_node_print(pn, 0);
2969#endif
2970 compile_syntax_error(comp, pn, "internal compiler error");
2971 } else {
2972 f(comp, pns);
2973 }
Damien429d7192013-10-04 19:53:11 +01002974 }
2975 }
2976}
2977
Damien George2ac4af62014-08-15 16:45:41 +01002978STATIC 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 +01002979 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002980 qstr param_name = MP_QSTR_NULL;
2981 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002982 if (MP_PARSE_NODE_IS_ID(pn)) {
2983 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002984 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002985 // comes after a star, so counts as a keyword-only parameter
2986 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002987 } else {
Damien George2827d622014-04-27 15:50:52 +01002988 // comes before a star, so counts as a positional parameter
2989 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002990 }
Damienb14de212013-10-06 00:28:28 +01002991 } else {
Damiend99b0522013-12-21 18:17:45 +00002992 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2993 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2994 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2995 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002996 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002997 // comes after a star, so counts as a keyword-only parameter
2998 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002999 } else {
Damien George2827d622014-04-27 15:50:52 +01003000 // comes before a star, so counts as a positional parameter
3001 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003002 }
Damiend99b0522013-12-21 18:17:45 +00003003 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003004 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003005 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003006 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003007 // bare star
3008 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003009 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003010 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003011 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003012 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003013 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01003014 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01003015 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003016 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003017 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3018 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003019 } else {
3020 // shouldn't happen
3021 assert(0);
3022 }
Damiend99b0522013-12-21 18:17:45 +00003023 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
3024 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003025 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003026 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003027 } else {
Damienb14de212013-10-06 00:28:28 +01003028 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01003029 assert(0);
3030 }
Damien429d7192013-10-04 19:53:11 +01003031 }
3032
Damien George2827d622014-04-27 15:50:52 +01003033 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003034 bool added;
3035 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3036 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003037 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003038 return;
3039 }
Damien429d7192013-10-04 19:53:11 +01003040 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003041 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003042 }
3043}
3044
Damien George2827d622014-04-27 15:50:52 +01003045STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003046 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003047}
3048
Damien George2827d622014-04-27 15:50:52 +01003049STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003050 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3051}
3052
3053STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3054 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3055 // no annotation
3056 return;
3057 }
3058
3059 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3060 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3061 // named parameter with possible annotation
3062 // fallthrough
3063 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3064 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3065 // named star with possible annotation
3066 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3067 // fallthrough
3068 } else {
3069 // no annotation
3070 return;
3071 }
3072 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3073 // double star with possible annotation
3074 // fallthrough
3075 } else {
3076 // no annotation
3077 return;
3078 }
3079
3080 mp_parse_node_t pn_annotation = pns->nodes[1];
3081
3082 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3083 #if MICROPY_EMIT_NATIVE
3084 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3085 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3086 assert(id_info != NULL);
3087
3088 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3089 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3090 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3091 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3092 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003093 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003094 }
3095 }
3096 #endif // MICROPY_EMIT_NATIVE
3097 }
Damien429d7192013-10-04 19:53:11 +01003098}
3099
Damien George6be0b0a2014-08-15 14:30:52 +01003100STATIC 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 +01003101 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003102 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003103 // no more nested if/for; compile inner expression
3104 compile_node(comp, pn_inner_expr);
3105 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003106 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003107 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003108 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003109 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003110 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003111 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003112 #endif
Damien429d7192013-10-04 19:53:11 +01003113 } else {
3114 EMIT(yield_value);
3115 EMIT(pop_top);
3116 }
Damiend99b0522013-12-21 18:17:45 +00003117 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003118 // if condition
Damiend99b0522013-12-21 18:17:45 +00003119 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003120 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3121 pn_iter = pns_comp_if->nodes[1];
3122 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003123 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003124 // for loop
Damiend99b0522013-12-21 18:17:45 +00003125 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003126 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003127 uint l_end2 = comp_next_label(comp);
3128 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003129 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003130 EMIT_ARG(label_assign, l_top2);
3131 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003132 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3133 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 +00003134 EMIT_ARG(jump, l_top2);
3135 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003136 EMIT(for_iter_end);
3137 } else {
3138 // shouldn't happen
3139 assert(0);
3140 }
3141}
3142
Damien George1463c1f2014-04-25 23:52:57 +01003143STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3144#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003145 // see http://www.python.org/dev/peps/pep-0257/
3146
3147 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003148 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003149 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003150 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003151 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003152 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3153 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003154 for (int i = 0; i < num_nodes; i++) {
3155 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003156 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 +00003157 // not a newline, so this is the first statement; finish search
3158 break;
3159 }
3160 }
3161 // 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 +00003162 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003163 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003164 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003165 } else {
3166 return;
3167 }
3168
3169 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003170 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3171 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003172 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3173 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3174 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3175 // compile the doc string
3176 compile_node(comp, pns->nodes[0]);
3177 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003178 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003179 }
3180 }
Damien George1463c1f2014-04-25 23:52:57 +01003181#endif
Damien429d7192013-10-04 19:53:11 +01003182}
3183
Damien George1463c1f2014-04-25 23:52:57 +01003184STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003185 comp->pass = pass;
3186 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003187 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003188 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003189
Damien George36db6bc2014-05-07 17:24:22 +01003190 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003191 // reset maximum stack sizes in scope
3192 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003193 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003194 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003195 }
3196
Damien5ac1b2e2013-10-18 19:58:12 +01003197#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003198 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003199 scope_print_info(scope);
3200 }
Damien5ac1b2e2013-10-18 19:58:12 +01003201#endif
Damien429d7192013-10-04 19:53:11 +01003202
3203 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003204 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3205 assert(scope->kind == SCOPE_MODULE);
3206 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3207 compile_node(comp, pns->nodes[0]); // compile the expression
3208 EMIT(return_value);
3209 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003210 if (!comp->is_repl) {
3211 check_for_doc_string(comp, scope->pn);
3212 }
Damien429d7192013-10-04 19:53:11 +01003213 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003214 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003215 EMIT(return_value);
3216 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003217 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3218 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3219 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003220
3221 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003222 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003223 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003224 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003225 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003226 } else {
3227 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003228
Damien George2ac4af62014-08-15 16:45:41 +01003229 // argument annotations
3230 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3231
3232 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003233 mp_parse_node_t pn_annotation = pns->nodes[2];
3234 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3235 #if MICROPY_EMIT_NATIVE
3236 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3237 // nodes[2] can be null or a test-expr
3238 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3239 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3240 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3241 } else {
3242 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3243 }
Damien George2ac4af62014-08-15 16:45:41 +01003244 }
Damien Georgea5190a72014-08-15 22:39:08 +01003245 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003246 }
Damien George2ac4af62014-08-15 16:45:41 +01003247 }
Damien429d7192013-10-04 19:53:11 +01003248
3249 compile_node(comp, pns->nodes[3]); // 3 is function body
3250 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003251 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003252 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003253 EMIT(return_value);
3254 }
3255 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003256 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3257 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3258 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003259
3260 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003261 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003262 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003263 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003264 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3265 }
3266
3267 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003268
3269 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3270 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3271 EMIT(pop_top);
3272 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3273 }
Damien429d7192013-10-04 19:53:11 +01003274 EMIT(return_value);
3275 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3276 // a bit of a hack at the moment
3277
Damiend99b0522013-12-21 18:17:45 +00003278 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3279 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3280 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3281 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3282 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003283
Damien George708c0732014-04-27 19:23:46 +01003284 // We need a unique name for the comprehension argument (the iterator).
3285 // CPython uses .0, but we should be able to use anything that won't
3286 // clash with a user defined variable. Best to use an existing qstr,
3287 // so we use the blank qstr.
3288#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003289 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003290#else
3291 qstr qstr_arg = MP_QSTR_;
3292#endif
Damien George36db6bc2014-05-07 17:24:22 +01003293 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003294 bool added;
3295 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3296 assert(added);
3297 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003298 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003299 }
3300
3301 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003302 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003303 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003304 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003305 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003306 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003307 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003308 #endif
Damien429d7192013-10-04 19:53:11 +01003309 }
3310
Damien George6f355fd2014-04-10 14:11:31 +01003311 uint l_end = comp_next_label(comp);
3312 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003313 EMIT_ARG(load_id, qstr_arg);
3314 EMIT_ARG(label_assign, l_top);
3315 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003316 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3317 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003318 EMIT_ARG(jump, l_top);
3319 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003320 EMIT(for_iter_end);
3321
3322 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003323 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003324 }
3325 EMIT(return_value);
3326 } else {
3327 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003328 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3329 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3330 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003331
Damien George36db6bc2014-05-07 17:24:22 +01003332 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003333 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003334 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003335 assert(added);
3336 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003337 }
3338
Damien Georgeb9791222014-01-23 00:34:21 +00003339 EMIT_ARG(load_id, MP_QSTR___name__);
3340 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003341 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 +00003342 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003343
3344 check_for_doc_string(comp, pns->nodes[2]);
3345 compile_node(comp, pns->nodes[2]); // 2 is class body
3346
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003347 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003348 assert(id != NULL);
3349 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003350 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003351 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003352#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003353 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003354#else
Damien George2bf7c092014-04-09 15:26:46 +01003355 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003356#endif
Damien429d7192013-10-04 19:53:11 +01003357 }
3358 EMIT(return_value);
3359 }
3360
Damien415eb6f2013-10-05 12:19:06 +01003361 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003362
3363 // make sure we match all the exception levels
3364 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003365}
3366
Damien George094d4502014-04-02 17:31:27 +01003367#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003368// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003369STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003370 comp->pass = pass;
3371 comp->scope_cur = scope;
3372 comp->next_label = 1;
3373
3374 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003375 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003376 return;
3377 }
3378
Damien George36db6bc2014-05-07 17:24:22 +01003379 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003380 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003381 }
3382
Damien826005c2013-10-05 23:17:28 +01003383 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003384 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3385 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3386 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003387
Damiend99b0522013-12-21 18:17:45 +00003388 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003389
Damiena2f2f7d2013-10-06 00:14:13 +01003390 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003391 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003392 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003393 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003394 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003395 }
3396
Damiend99b0522013-12-21 18:17:45 +00003397 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003398
Damiend99b0522013-12-21 18:17:45 +00003399 mp_parse_node_t pn_body = pns->nodes[3]; // body
3400 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003401 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3402
Damien Georgecbd2f742014-01-19 11:48:48 +00003403 /*
Damien George36db6bc2014-05-07 17:24:22 +01003404 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003405 //printf("----\n");
3406 scope_print_info(scope);
3407 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003408 */
Damien826005c2013-10-05 23:17:28 +01003409
3410 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003411 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3412 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003413 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3414 // no instructions
3415 continue;
3416 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3417 // an instruction; fall through
3418 } else {
3419 // not an instruction; error
3420 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3421 return;
3422 }
Damiend99b0522013-12-21 18:17:45 +00003423 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3424 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3425 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3426 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3427 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3428 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3429 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3430 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3431 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3432 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003433 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3434
3435 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003436 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003437 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003438 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003439 return;
3440 }
Damien George6f355fd2014-04-10 14:11:31 +01003441 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003442 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003443 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003444 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003445 } else if (op == MP_QSTR_align) {
3446 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3447 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3448 return;
3449 }
Damien George36db6bc2014-05-07 17:24:22 +01003450 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003451 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3452 }
3453 } else if (op == MP_QSTR_data) {
3454 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3455 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3456 return;
3457 }
Damien George36db6bc2014-05-07 17:24:22 +01003458 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003459 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003460 for (uint i = 1; i < n_args; i++) {
3461 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3462 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3463 return;
3464 }
3465 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3466 }
3467 }
Damien826005c2013-10-05 23:17:28 +01003468 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003469 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003470 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003471 }
3472 }
3473 }
3474
Damien George36db6bc2014-05-07 17:24:22 +01003475 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003476 bool success = EMIT_INLINE_ASM(end_pass);
3477 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003478 // TODO get proper exception from inline assembler
3479 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003480 }
Damienb05d7072013-10-05 13:37:10 +01003481 }
Damien429d7192013-10-04 19:53:11 +01003482}
Damien George094d4502014-04-02 17:31:27 +01003483#endif
Damien429d7192013-10-04 19:53:11 +01003484
Damien George1463c1f2014-04-25 23:52:57 +01003485STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003486#if !MICROPY_EMIT_CPYTHON
3487 // in Micro Python we put the *x parameter after all other parameters (except **y)
3488 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3489 id_info_t *id_param = NULL;
3490 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3491 id_info_t *id = &scope->id_info[i];
3492 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3493 if (id_param != NULL) {
3494 // swap star param with last param
3495 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3496 }
3497 break;
3498 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3499 id_param = id;
3500 }
3501 }
3502 }
3503#endif
3504
Damien429d7192013-10-04 19:53:11 +01003505 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003506 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003507 scope->num_locals = 0;
3508 for (int i = 0; i < scope->id_info_len; i++) {
3509 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003510 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003511 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3512 continue;
3513 }
3514 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3515 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3516 }
Damien George2827d622014-04-27 15:50:52 +01003517 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003518 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003519 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003520 }
3521 }
3522
3523 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003524#if MICROPY_EMIT_CPYTHON
3525 int num_cell = 0;
3526#endif
Damien9ecbcff2013-12-11 00:41:43 +00003527 for (int i = 0; i < scope->id_info_len; i++) {
3528 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003529#if MICROPY_EMIT_CPYTHON
3530 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003531 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003532 id->local_num = num_cell;
3533 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003534 }
Damien George6baf76e2013-12-30 22:32:17 +00003535#else
3536 // in Micro Python the cells come right after the fast locals
3537 // parameters are not counted here, since they remain at the start
3538 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003539 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003540 id->local_num = scope->num_locals;
3541 scope->num_locals += 1;
3542 }
3543#endif
Damien9ecbcff2013-12-11 00:41:43 +00003544 }
Damien9ecbcff2013-12-11 00:41:43 +00003545
3546 // compute the index of free vars (freevars[idx] in CPython)
3547 // make sure they are in the order of the parent scope
3548 if (scope->parent != NULL) {
3549 int num_free = 0;
3550 for (int i = 0; i < scope->parent->id_info_len; i++) {
3551 id_info_t *id = &scope->parent->id_info[i];
3552 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3553 for (int j = 0; j < scope->id_info_len; j++) {
3554 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003555 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003556 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003557#if MICROPY_EMIT_CPYTHON
3558 // in CPython the frees are numbered after the cells
3559 id2->local_num = num_cell + num_free;
3560#else
3561 // in Micro Python the frees come first, before the params
3562 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003563#endif
3564 num_free += 1;
3565 }
3566 }
3567 }
Damien429d7192013-10-04 19:53:11 +01003568 }
Damien George6baf76e2013-12-30 22:32:17 +00003569#if !MICROPY_EMIT_CPYTHON
3570 // in Micro Python shift all other locals after the free locals
3571 if (num_free > 0) {
3572 for (int i = 0; i < scope->id_info_len; i++) {
3573 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003574 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003575 id->local_num += num_free;
3576 }
3577 }
Damien George2827d622014-04-27 15:50:52 +01003578 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 +00003579 scope->num_locals += num_free;
3580 }
3581#endif
Damien429d7192013-10-04 19:53:11 +01003582 }
3583
Damien George8725f8f2014-02-15 19:33:11 +00003584 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003585
3586#if MICROPY_EMIT_CPYTHON
3587 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003588 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) {
3589 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003590 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003591 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003592 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 +00003593 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003594 }
3595 }
Damien George882b3632014-04-02 15:56:31 +01003596#endif
3597
Damien429d7192013-10-04 19:53:11 +01003598 int num_free = 0;
3599 for (int i = 0; i < scope->id_info_len; i++) {
3600 id_info_t *id = &scope->id_info[i];
3601 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3602 num_free += 1;
3603 }
3604 }
3605 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003606 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003607 }
3608}
3609
Damien George65cad122014-04-06 11:48:15 +01003610mp_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 +01003611 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003612 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003613 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003614 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003615
Damien826005c2013-10-05 23:17:28 +01003616 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003617 mp_map_t consts;
3618 mp_map_init(&consts, 0);
3619 pn = fold_constants(comp, pn, &consts);
3620 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003621
3622 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003623 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003624
Damien826005c2013-10-05 23:17:28 +01003625 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003626 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003627 comp->emit_method_table = &emit_pass1_method_table;
3628 comp->emit_inline_asm = NULL;
3629 comp->emit_inline_asm_method_table = NULL;
3630 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003631 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003632 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003633#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003634 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003635 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003636#endif
Damien826005c2013-10-05 23:17:28 +01003637 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003638 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003639 }
3640
3641 // update maximim number of labels needed
3642 if (comp->next_label > max_num_labels) {
3643 max_num_labels = comp->next_label;
3644 }
Damien429d7192013-10-04 19:53:11 +01003645 }
3646
Damien826005c2013-10-05 23:17:28 +01003647 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003648 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003649 compile_scope_compute_things(comp, s);
3650 }
3651
Damien826005c2013-10-05 23:17:28 +01003652 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003653 emit_pass1_free(comp->emit);
3654
Damien826005c2013-10-05 23:17:28 +01003655 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003656#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003657 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003658#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003659 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003660#endif
Damien3ef4abb2013-10-12 16:53:13 +01003661#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003662 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003663#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003664#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003665 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003666 if (false) {
3667 // dummy
3668
Damien3ef4abb2013-10-12 16:53:13 +01003669#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003670 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003671 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003672 if (emit_inline_thumb == NULL) {
3673 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3674 }
3675 comp->emit = NULL;
3676 comp->emit_method_table = NULL;
3677 comp->emit_inline_asm = emit_inline_thumb;
3678 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003679 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003680 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003681 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003682 }
Damienc025ebb2013-10-12 14:30:21 +01003683#endif
3684
Damien826005c2013-10-05 23:17:28 +01003685 } else {
Damienc025ebb2013-10-12 14:30:21 +01003686
3687 // choose the emit type
3688
Damien3ef4abb2013-10-12 16:53:13 +01003689#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003690 comp->emit = emit_cpython_new(max_num_labels);
3691 comp->emit_method_table = &emit_cpython_method_table;
3692#else
Damien826005c2013-10-05 23:17:28 +01003693 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003694
3695#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003696 case MP_EMIT_OPT_NATIVE_PYTHON:
3697 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003698#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003699 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003700 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003701 }
Damien13ed3a62013-10-08 09:05:10 +01003702 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003703#elif MICROPY_EMIT_X86
3704 if (emit_native == NULL) {
3705 emit_native = emit_native_x86_new(max_num_labels);
3706 }
3707 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003708#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003709 if (emit_native == NULL) {
3710 emit_native = emit_native_thumb_new(max_num_labels);
3711 }
3712 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003713#elif MICROPY_EMIT_ARM
3714 if (emit_native == NULL) {
3715 emit_native = emit_native_arm_new(max_num_labels);
3716 }
3717 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003718#endif
3719 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003720 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 +01003721
3722 // native emitters need an extra pass to compute stack size
3723 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3724
Damien7af3d192013-10-07 00:02:49 +01003725 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003726#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003727
Damien826005c2013-10-05 23:17:28 +01003728 default:
3729 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003730 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003731 }
3732 comp->emit = emit_bc;
3733 comp->emit_method_table = &emit_bc_method_table;
3734 break;
3735 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003736#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003737
Damien George36db6bc2014-05-07 17:24:22 +01003738 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003739 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003740 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3741 }
3742
3743 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003744 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003745 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003746 }
Damien6cdd3af2013-10-05 18:08:26 +01003747 }
Damien429d7192013-10-04 19:53:11 +01003748 }
3749
Damien George41d02b62014-01-24 22:42:28 +00003750 // free the emitters
3751#if !MICROPY_EMIT_CPYTHON
3752 if (emit_bc != NULL) {
3753 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003754 }
Damien George41d02b62014-01-24 22:42:28 +00003755#if MICROPY_EMIT_NATIVE
3756 if (emit_native != NULL) {
3757#if MICROPY_EMIT_X64
3758 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003759#elif MICROPY_EMIT_X86
3760 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003761#elif MICROPY_EMIT_THUMB
3762 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003763#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003764 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003765#endif
3766 }
3767#endif
3768#if MICROPY_EMIT_INLINE_THUMB
3769 if (emit_inline_thumb != NULL) {
3770 emit_inline_thumb_free(emit_inline_thumb);
3771 }
3772#endif
3773#endif // !MICROPY_EMIT_CPYTHON
3774
Damien George52b5d762014-09-23 15:31:56 +00003775 // free the parse tree
3776 mp_parse_node_free(pn);
3777
Damien George41d02b62014-01-24 22:42:28 +00003778 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003779 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003780 for (scope_t *s = module_scope; s;) {
3781 scope_t *next = s->next;
3782 scope_free(s);
3783 s = next;
3784 }
Damien5ac1b2e2013-10-18 19:58:12 +01003785
Damien George41d02b62014-01-24 22:42:28 +00003786 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003787 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003788 m_del_obj(compiler_t, comp);
3789
Damien Georgea91ac202014-10-05 19:01:34 +01003790 if (compile_error != MP_OBJ_NULL) {
3791 return compile_error;
Damien George1fb03172014-01-03 14:22:03 +00003792 } else {
3793#if MICROPY_EMIT_CPYTHON
3794 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003795 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003796 return mp_const_true;
3797#else
3798 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003799 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003800#endif
3801 }
Damien429d7192013-10-04 19:53:11 +01003802}