blob: eb9cf9a94808a2669d2d1135f001f833d6b134a7 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
Damiend99b0522013-12-21 18:17:45 +000034#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030035#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010037#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010038#include "parse.h"
Damiend99b0522013-12-21 18:17:45 +000039#include "runtime0.h"
Damien George1fb03172014-01-03 14:22:03 +000040#include "obj.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010041#include "emitglue.h"
42#include "scope.h"
43#include "emit.h"
Damien George1fb03172014-01-03 14:22:03 +000044#include "compile.h"
45#include "runtime.h"
Damien Georgea26dc502014-04-12 17:54:52 +010046#include "builtin.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000047#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010048
49// TODO need to mangle __attr names
50
51typedef enum {
52 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000053#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010054#include "grammar.h"
55#undef DEF_RULE
56 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010057 PN_string, // special node for non-interned string
Damien429d7192013-10-04 19:53:11 +010058} pn_kind_t;
59
Damien Georgeb9791222014-01-23 00:34:21 +000060#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
61#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
62#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
63#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010064
65typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000066 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010067 uint8_t is_repl;
68 uint8_t pass; // holds enum type pass_kind_t
69 uint8_t had_error; // try to keep compiler clean from nlr
70 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010071
Damien George6f355fd2014-04-10 14:11:31 +010072 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010073
Damien George25c84642014-05-30 15:20:41 +010074 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
75 uint16_t continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000076 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010077 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010078
Damien George8b19db02014-04-11 23:25:34 +010079 uint8_t have_star;
Damien George69b89d22014-04-11 13:38:30 +000080 uint16_t num_dict_params;
81 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010082
83 scope_t *scope_head;
84 scope_t *scope_cur;
85
Damien6cdd3af2013-10-05 18:08:26 +010086 emit_t *emit; // current emitter
87 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010088
89 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
90 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 +010091} compiler_t;
92
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010093STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000094 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010095 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +010096 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010097 } else {
98 printf(" File \"%s\"\n", qstr_str(comp->source_file));
99 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000100 printf("SyntaxError: %s\n", msg);
101 comp->had_error = true;
102}
103
Damien George57e99eb2014-04-10 22:42:11 +0100104STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300105 #if MICROPY_PY_UCTYPES
106 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
107 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100108 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100109 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100110};
111
112STATIC const mp_map_t mp_constants_map = {
113 .all_keys_are_qstrs = 1,
114 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200115 .used = MP_ARRAY_SIZE(mp_constants_table),
116 .alloc = MP_ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +0100117 .table = (mp_map_elem_t*)mp_constants_table,
118};
119
Damien Georgeffae48d2014-05-08 15:58:39 +0000120// this function is essentially a simple preprocessor
121STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
122 if (0) {
123 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100124#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000125 } else if (MP_PARSE_NODE_IS_ID(pn)) {
126 // lookup identifier in table of dynamic constants
127 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
128 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
129 if (elem != NULL) {
130 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
131 }
132#endif
133 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000134 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100135
Damien Georgeffae48d2014-05-08 15:58:39 +0000136 // fold some parse nodes before folding their arguments
137 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100138#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000139 case PN_expr_stmt:
140 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
141 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
142 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
143 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
144 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
145 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
146 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
147 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
148 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
149 ) {
150 // code to assign dynamic constants: id = const(value)
151
152 // get the id
153 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
154
155 // get the value
156 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
157 pn_value = fold_constants(comp, pn_value, consts);
158 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
159 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
160 break;
161 }
Damien George40f3c022014-07-03 13:25:24 +0100162 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000163
164 // store the value in the table of dynamic constants
165 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
166 if (elem->value != MP_OBJ_NULL) {
167 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
168 break;
169 }
170 elem->value = MP_OBJ_NEW_SMALL_INT(value);
171
172 // replace const(value) with value
173 pns1->nodes[0] = pn_value;
174
175 // finished folding this assignment
176 return pn;
177 }
178 }
179 }
180 break;
181#endif
Damien George5042bce2014-05-25 22:06:06 +0100182 case PN_string:
183 return pn;
Damien429d7192013-10-04 19:53:11 +0100184 }
185
Damien Georgeffae48d2014-05-08 15:58:39 +0000186 // fold arguments
187 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
188 for (int i = 0; i < n; i++) {
189 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
190 }
191
192 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000193 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100194 case PN_atom_paren:
195 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
196 // (int)
197 pn = pns->nodes[0];
198 }
199 break;
200
201 case PN_expr:
202 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
203 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100204 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
205 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100206 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
207 }
208 break;
209
210 case PN_and_expr:
211 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
212 // int & int
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[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100215 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
216 }
217 break;
218
Damien429d7192013-10-04 19:53:11 +0100219 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000220 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 +0100221 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
222 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000223 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100224 // int << int
225 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
226 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
227 }
Damiend99b0522013-12-21 18:17:45 +0000228 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100229 // int >> int
Damiend99b0522013-12-21 18:17:45 +0000230 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100231 } else {
232 // shouldn't happen
233 assert(0);
234 }
235 }
236 break;
237
238 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100239 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000240 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 +0100241 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
242 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000243 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100244 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000245 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000246 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100247 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000248 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100249 } else {
250 // shouldn't happen
251 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100252 }
Damien Georged1e355e2014-05-28 14:51:12 +0100253 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100254 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000255 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100256 }
257 }
258 break;
259
260 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000261 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 +0100262 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
263 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000264 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100265 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000266 if (!mp_small_int_mul_overflow(arg0, arg1)) {
267 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100268 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000269 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
270 }
271 }
Damiend99b0522013-12-21 18:17:45 +0000272 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100273 // int / int
274 // pass
Damiend99b0522013-12-21 18:17:45 +0000275 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100276 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000277 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000278 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300279 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100280 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000281 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 +0300282 }
Damien429d7192013-10-04 19:53:11 +0100283 } else {
284 // shouldn't happen
285 assert(0);
286 }
287 }
288 break;
289
290 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000291 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100292 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000293 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
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_MINUS)) {
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);
299 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100300 // ~int
Damiend99b0522013-12-21 18:17:45 +0000301 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100302 } else {
303 // shouldn't happen
304 assert(0);
305 }
306 }
307 break;
308
309 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100310 if (0) {
311#if MICROPY_EMIT_CPYTHON
312 } 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 +0100313 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100314 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000315 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
316 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200317 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100318 if (power >= 0) {
319 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200320 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100321 for (; power > 0; power--) {
322 ans *= base;
323 }
Damiend99b0522013-12-21 18:17:45 +0000324 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100325 }
326 }
Damien George57e99eb2014-04-10 22:42:11 +0100327#endif
328 } 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])) {
329 // id.id
330 // look it up in constant table, see if it can be replaced with an integer
331 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
332 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
333 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
334 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
335 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
336 if (elem != NULL) {
337 mp_obj_t dest[2];
338 mp_load_method_maybe(elem->value, q_attr, dest);
339 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100340 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georged1e355e2014-05-28 14:51:12 +0100341 if (MP_SMALL_INT_FITS(val)) {
Damien George57e99eb2014-04-10 22:42:11 +0100342 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
343 }
344 }
345 }
Damien429d7192013-10-04 19:53:11 +0100346 }
347 break;
348 }
349 }
350
351 return pn;
352}
353
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200354STATIC 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 +0100355STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000356STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100357
Damien George6f355fd2014-04-10 14:11:31 +0100358STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100359 return comp->next_label++;
360}
361
Damien George8dcc0c72014-03-27 10:55:21 +0000362STATIC void compile_increase_except_level(compiler_t *comp) {
363 comp->cur_except_level += 1;
364 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
365 comp->scope_cur->exc_stack_size = comp->cur_except_level;
366 }
367}
368
369STATIC void compile_decrease_except_level(compiler_t *comp) {
370 assert(comp->cur_except_level > 0);
371 comp->cur_except_level -= 1;
372}
373
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200374STATIC 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 +0100375 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100376 scope->parent = comp->scope_cur;
377 scope->next = NULL;
378 if (comp->scope_head == NULL) {
379 comp->scope_head = scope;
380 } else {
381 scope_t *s = comp->scope_head;
382 while (s->next != NULL) {
383 s = s->next;
384 }
385 s->next = scope;
386 }
387 return scope;
388}
389
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200390STATIC 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 +0000391 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
392 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
393 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100394 for (int i = 0; i < num_nodes; i++) {
395 f(comp, pns->nodes[i]);
396 }
Damiend99b0522013-12-21 18:17:45 +0000397 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100398 f(comp, pn);
399 }
400}
401
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200402STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000403 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100404 *nodes = NULL;
405 return 0;
Damiend99b0522013-12-21 18:17:45 +0000406 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100407 *nodes = pn;
408 return 1;
409 } else {
Damiend99b0522013-12-21 18:17:45 +0000410 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
411 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100412 *nodes = pn;
413 return 1;
414 } else {
415 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000416 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100417 }
418 }
419}
420
Damiend99b0522013-12-21 18:17:45 +0000421void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100422}
423
Damiend99b0522013-12-21 18:17:45 +0000424void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
425 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100426 for (int i = 0; i < num_nodes; i++) {
427 compile_node(comp, pns->nodes[i]);
428 }
429}
430
Damien3ef4abb2013-10-12 16:53:13 +0100431#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200432STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100433 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
434 return true;
435 }
Damiend99b0522013-12-21 18:17:45 +0000436 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100437 return false;
438 }
Damiend99b0522013-12-21 18:17:45 +0000439 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100440 return false;
441 }
442 return true;
443}
444
Damien George5042bce2014-05-25 22:06:06 +0100445STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000446 bool has_single_quote = false;
447 bool has_double_quote = false;
448 for (int i = 0; i < len; i++) {
449 if (str[i] == '\'') {
450 has_single_quote = true;
451 } else if (str[i] == '"') {
452 has_double_quote = true;
453 }
454 }
455 if (bytes) {
456 vstr_printf(vstr, "b");
457 }
458 bool quote_single = false;
459 if (has_single_quote && !has_double_quote) {
460 vstr_printf(vstr, "\"");
461 } else {
462 quote_single = true;
463 vstr_printf(vstr, "'");
464 }
465 for (int i = 0; i < len; i++) {
466 if (str[i] == '\n') {
467 vstr_printf(vstr, "\\n");
468 } else if (str[i] == '\\') {
469 vstr_printf(vstr, "\\\\");
470 } else if (str[i] == '\'' && quote_single) {
471 vstr_printf(vstr, "\\'");
472 } else {
473 vstr_printf(vstr, "%c", str[i]);
474 }
475 }
476 if (has_single_quote && !has_double_quote) {
477 vstr_printf(vstr, "\"");
478 } else {
479 vstr_printf(vstr, "'");
480 }
481}
482
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200483STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George5042bce2014-05-25 22:06:06 +0100484 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
485 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George40f3c022014-07-03 13:25:24 +0100486 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 +0100487 return;
488 }
489
Damiend99b0522013-12-21 18:17:45 +0000490 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200491 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
492 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
493 return;
494 }
495
Damiend99b0522013-12-21 18:17:45 +0000496 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
497 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
498 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000499 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
500 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100501 case MP_PARSE_NODE_STRING:
502 case MP_PARSE_NODE_BYTES: {
503 uint len;
504 const byte *str = qstr_data(arg, &len);
505 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
506 break;
507 }
Damiend99b0522013-12-21 18:17:45 +0000508 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100509 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000510 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
511 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
512 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100513 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100514 }
515 break;
516 default: assert(0);
517 }
518}
519
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200520STATIC 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 +0100521 int n = 0;
522 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000523 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100524 }
525 int total = n;
526 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000527 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100528 total += 1;
Damien3a205172013-10-12 15:01:56 +0100529 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100530 is_const = false;
531 }
532 }
533 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100534 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100535 is_const = false;
536 break;
537 }
538 }
539 if (total > 0 && is_const) {
540 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000541 vstr_t *vstr = vstr_new();
542 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000543 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000544 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100545 need_comma = true;
546 }
547 for (int i = 0; i < n; i++) {
548 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000549 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100550 }
Damien02f89412013-12-12 15:13:36 +0000551 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100552 need_comma = true;
553 }
554 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000555 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100556 } else {
Damien02f89412013-12-12 15:13:36 +0000557 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100558 }
Damien Georgeb9791222014-01-23 00:34:21 +0000559 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000560 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100561 } else {
Damiend99b0522013-12-21 18:17:45 +0000562 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100563 compile_node(comp, pn);
564 }
565 for (int i = 0; i < n; i++) {
566 compile_node(comp, pns_list->nodes[i]);
567 }
Damien Georgeb9791222014-01-23 00:34:21 +0000568 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100569 }
570}
Damien3a205172013-10-12 15:01:56 +0100571#endif
572
573// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100574STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100575#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100576 cpython_c_tuple(comp, pn, pns_list);
577#else
578 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000579 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100580 compile_node(comp, pn);
581 total += 1;
582 }
583 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000584 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100585 for (int i = 0; i < n; i++) {
586 compile_node(comp, pns_list->nodes[i]);
587 }
588 total += n;
589 }
Damien Georgeb9791222014-01-23 00:34:21 +0000590 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100591#endif
592}
Damien429d7192013-10-04 19:53:11 +0100593
Damiend99b0522013-12-21 18:17:45 +0000594void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100595 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000596 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100597}
598
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200599STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000600 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200601 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100602}
603
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200604STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200605 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100606}
607
Damien3ef4abb2013-10-12 16:53:13 +0100608#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100609// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200610STATIC 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 +0100611 if (node_is_const_false(pn)) {
612 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000613 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100614 }
615 return;
616 } else if (node_is_const_true(pn)) {
617 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000618 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100619 }
620 return;
Damiend99b0522013-12-21 18:17:45 +0000621 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
622 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
623 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
624 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100625 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100626 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100627 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100628 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100629 }
Damien3a205172013-10-12 15:01:56 +0100630 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000631 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100632 } else {
633 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100634 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100635 }
636 }
637 return;
Damiend99b0522013-12-21 18:17:45 +0000638 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100639 if (jump_if == false) {
640 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100641 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100642 }
643 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100644 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100645 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100646 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100647 }
Damien3a205172013-10-12 15:01:56 +0100648 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000649 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100650 }
651 return;
Damiend99b0522013-12-21 18:17:45 +0000652 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100653 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100654 return;
655 }
656 }
657
658 // nothing special, fall back to default compiling for node and jump
659 compile_node(comp, pn);
660 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000661 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100662 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000663 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100664 }
665}
Damien3a205172013-10-12 15:01:56 +0100666#endif
Damien429d7192013-10-04 19:53:11 +0100667
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200668STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100669#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100670 cpython_c_if_cond(comp, pn, jump_if, label, false);
671#else
672 if (node_is_const_false(pn)) {
673 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000674 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100675 }
676 return;
677 } else if (node_is_const_true(pn)) {
678 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000679 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100680 }
681 return;
Damiend99b0522013-12-21 18:17:45 +0000682 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
683 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
684 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
685 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100686 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100687 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100688 for (int i = 0; i < n - 1; i++) {
689 c_if_cond(comp, pns->nodes[i], true, label2);
690 }
691 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000692 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100693 } else {
694 for (int i = 0; i < n; i++) {
695 c_if_cond(comp, pns->nodes[i], true, label);
696 }
697 }
698 return;
Damiend99b0522013-12-21 18:17:45 +0000699 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100700 if (jump_if == false) {
701 for (int i = 0; i < n; i++) {
702 c_if_cond(comp, pns->nodes[i], false, label);
703 }
704 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100705 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100706 for (int i = 0; i < n - 1; i++) {
707 c_if_cond(comp, pns->nodes[i], false, label2);
708 }
709 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000710 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100711 }
712 return;
Damiend99b0522013-12-21 18:17:45 +0000713 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100714 c_if_cond(comp, pns->nodes[0], !jump_if, label);
715 return;
716 }
717 }
718
719 // nothing special, fall back to default compiling for node and jump
720 compile_node(comp, pn);
721 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000722 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100723 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000724 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100725 }
726#endif
Damien429d7192013-10-04 19:53:11 +0100727}
728
729typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100730STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100731
Damien George6be0b0a2014-08-15 14:30:52 +0100732STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100733 if (assign_kind != ASSIGN_AUG_STORE) {
734 compile_node(comp, pns->nodes[0]);
735 }
736
Damiend99b0522013-12-21 18:17:45 +0000737 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
738 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
739 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
740 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100741 if (assign_kind != ASSIGN_AUG_STORE) {
742 for (int i = 0; i < n - 1; i++) {
743 compile_node(comp, pns1->nodes[i]);
744 }
745 }
Damiend99b0522013-12-21 18:17:45 +0000746 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
747 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100748 }
Damiend99b0522013-12-21 18:17:45 +0000749 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100750 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000751 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100752 if (assign_kind == ASSIGN_AUG_STORE) {
753 EMIT(rot_three);
754 EMIT(store_subscr);
755 } else {
756 compile_node(comp, pns1->nodes[0]);
757 if (assign_kind == ASSIGN_AUG_LOAD) {
758 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100759 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100760 } else {
761 EMIT(store_subscr);
762 }
763 }
Damiend99b0522013-12-21 18:17:45 +0000764 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
765 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100766 if (assign_kind == ASSIGN_AUG_LOAD) {
767 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000768 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100769 } else {
770 if (assign_kind == ASSIGN_AUG_STORE) {
771 EMIT(rot_two);
772 }
Damien Georgeb9791222014-01-23 00:34:21 +0000773 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100774 }
775 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100776 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100777 }
778 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100779 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100780 }
781
Damiend99b0522013-12-21 18:17:45 +0000782 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100783 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100784 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100785
786 return;
787
788cannot_assign:
789 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100790}
791
Damien George0288cf02014-04-11 11:53:00 +0000792// 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 +0100793STATIC 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 +0000794 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
795
796 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100797 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000798 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
799 EMIT_ARG(unpack_ex, 0, num_tail);
800 have_star_index = 0;
801 }
802 for (int i = 0; i < num_tail; i++) {
803 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100804 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000805 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
806 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100807 } else {
Damien George9d181f62014-04-27 16:55:27 +0100808 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100809 return;
810 }
811 }
812 }
813 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000814 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100815 }
Damien George0288cf02014-04-11 11:53:00 +0000816 if (num_head != 0) {
817 if (0 == have_star_index) {
818 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100819 } else {
Damien George0288cf02014-04-11 11:53:00 +0000820 c_assign(comp, node_head, ASSIGN_STORE);
821 }
822 }
823 for (int i = 0; i < num_tail; i++) {
824 if (num_head + i == have_star_index) {
825 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
826 } else {
827 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100828 }
829 }
830}
831
832// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100833STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100834 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000835 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100836 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000837 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
838 if (MP_PARSE_NODE_IS_ID(pn)) {
839 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100840 switch (assign_kind) {
841 case ASSIGN_STORE:
842 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000843 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100844 break;
845 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000846 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100847 break;
848 }
849 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100850 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100851 return;
852 }
853 } else {
Damiend99b0522013-12-21 18:17:45 +0000854 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
855 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100856 case PN_power:
857 // lhs is an index or attribute
858 c_assign_power(comp, pns, assign_kind);
859 break;
860
861 case PN_testlist_star_expr:
862 case PN_exprlist:
863 // lhs is a tuple
864 if (assign_kind != ASSIGN_STORE) {
865 goto bad_aug;
866 }
Damien George0288cf02014-04-11 11:53:00 +0000867 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100868 break;
869
870 case PN_atom_paren:
871 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000872 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100873 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100874 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000875 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
876 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100877 goto testlist_comp;
878 } else {
879 // parenthesis around 1 item, is just that item
880 pn = pns->nodes[0];
881 goto tail_recursion;
882 }
883 break;
884
885 case PN_atom_bracket:
886 // lhs is something in brackets
887 if (assign_kind != ASSIGN_STORE) {
888 goto bad_aug;
889 }
Damiend99b0522013-12-21 18:17:45 +0000890 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100891 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000892 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000893 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
894 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100895 goto testlist_comp;
896 } else {
897 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000898 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100899 }
900 break;
901
902 default:
Damien George9d181f62014-04-27 16:55:27 +0100903 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100904 }
905 return;
906
907 testlist_comp:
908 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000909 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
910 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
911 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100912 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000913 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000914 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000915 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100916 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000917 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
918 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000919 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100920 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100921 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100922 } else {
923 // sequence with 2 items
924 goto sequence_with_2_items;
925 }
926 } else {
927 // sequence with 2 items
928 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000929 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100930 }
931 return;
932 }
933 return;
934
Damien George9d181f62014-04-27 16:55:27 +0100935 cannot_assign:
936 compile_syntax_error(comp, pn, "can't assign to expression");
937 return;
938
Damien429d7192013-10-04 19:53:11 +0100939 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100940 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100941}
942
943// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100944// if we are not in CPython compatibility mode then:
945// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
946// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
947// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100948STATIC 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 +0100949 assert(n_pos_defaults >= 0);
950 assert(n_kw_defaults >= 0);
951
Damien429d7192013-10-04 19:53:11 +0100952 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000953 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100954 int nfree = 0;
955 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000956 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
957 id_info_t *id = &comp->scope_cur->id_info[i];
958 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
959 for (int j = 0; j < this_scope->id_info_len; j++) {
960 id_info_t *id2 = &this_scope->id_info[j];
961 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000962#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000963 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000964#else
965 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100966 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000967#endif
Damien318aec62013-12-10 18:28:17 +0000968 nfree += 1;
969 }
970 }
Damien429d7192013-10-04 19:53:11 +0100971 }
972 }
973 }
Damien429d7192013-10-04 19:53:11 +0100974
975 // make the function/closure
976 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100977 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100978 } else {
Damien George3558f622014-04-20 17:50:40 +0100979 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100980 }
981}
982
Damien George6be0b0a2014-08-15 14:30:52 +0100983STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000984 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100985 comp->have_star = true;
986 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000987 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
988 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100989 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100990 } else {
991 // named star
Damien429d7192013-10-04 19:53:11 +0100992 }
Damien George8b19db02014-04-11 23:25:34 +0100993 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000994
995 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100996 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000997 // TODO do we need to do anything with this?
998
999 } else {
1000 mp_parse_node_t pn_id;
1001 mp_parse_node_t pn_colon;
1002 mp_parse_node_t pn_equal;
1003 if (MP_PARSE_NODE_IS_ID(pn)) {
1004 // this parameter is just an id
1005
1006 pn_id = pn;
1007 pn_colon = MP_PARSE_NODE_NULL;
1008 pn_equal = MP_PARSE_NODE_NULL;
1009
1010 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1011 // this parameter has a colon and/or equal specifier
1012
1013 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1014 pn_id = pns->nodes[0];
1015 pn_colon = pns->nodes[1];
1016 pn_equal = pns->nodes[2];
1017
1018 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001019 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001020 assert(0);
1021 return;
1022 }
1023
1024 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1025 // this parameter does not have a default value
1026
1027 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001028 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001029 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001030 return;
1031 }
1032
1033 } else {
1034 // this parameter has a default value
1035 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1036
Damien George8b19db02014-04-11 23:25:34 +01001037 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001038 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001039#if MICROPY_EMIT_CPYTHON
1040 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1041 compile_node(comp, pn_equal);
1042#else
Damien George69b89d22014-04-11 13:38:30 +00001043 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1044 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001045 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1046 // we need to do this here before we start building the map for the default keywords
1047 if (comp->num_default_params > 0) {
1048 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001049 } else {
1050 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001051 }
Damien George69b89d22014-04-11 13:38:30 +00001052 // first default dict param, so make the map
1053 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001054 }
Damien Georgef0778a72014-06-07 22:01:00 +01001055
1056 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001057 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001058 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001059 EMIT(store_map);
1060#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001061 } else {
Damien George69b89d22014-04-11 13:38:30 +00001062 comp->num_default_params += 1;
1063 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001064 }
1065 }
1066
1067 // TODO pn_colon not implemented
1068 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001069 }
1070}
1071
1072// leaves function object on stack
1073// returns function name
Damiend99b0522013-12-21 18:17:45 +00001074qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001075 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001076 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001077 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001078 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001079 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001080 }
1081
1082 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001083 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001084 uint old_num_dict_params = comp->num_dict_params;
1085 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001086
1087 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001088 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001089 comp->num_dict_params = 0;
1090 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001091 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001092
1093 if (comp->had_error) {
1094 return MP_QSTR_NULL;
1095 }
1096
Damien Georgee337f1e2014-03-31 15:18:37 +01001097#if !MICROPY_EMIT_CPYTHON
1098 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001099 // the default keywords args may have already made the tuple; if not, do it now
1100 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001101 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001102 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001103 }
1104#endif
1105
Damien429d7192013-10-04 19:53:11 +01001106 // get the scope for this function
1107 scope_t *fscope = (scope_t*)pns->nodes[4];
1108
1109 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001110 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001111
1112 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001113 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001114 comp->num_dict_params = old_num_dict_params;
1115 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001116
1117 // return its name (the 'f' in "def f(...):")
1118 return fscope->simple_name;
1119}
1120
1121// leaves class object on stack
1122// returns class name
Damiend99b0522013-12-21 18:17:45 +00001123qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001124 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001125 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001126 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001127 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001128 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001129 }
1130
1131 EMIT(load_build_class);
1132
1133 // scope for this class
1134 scope_t *cscope = (scope_t*)pns->nodes[3];
1135
1136 // compile the class
1137 close_over_variables_etc(comp, cscope, 0, 0);
1138
1139 // get its name
Damien George968bf342014-04-27 19:12:05 +01001140 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001141
1142 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001143 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1144 mp_parse_node_t parents = pns->nodes[1];
1145 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1146 parents = MP_PARSE_NODE_NULL;
1147 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001148 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001149 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001150
1151 // return its name (the 'C' in class C(...):")
1152 return cscope->simple_name;
1153}
1154
Damien6cdd3af2013-10-05 18:08:26 +01001155// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001156STATIC 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 +00001157 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001158 return false;
1159 }
1160
1161 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001162 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001163 return true;
1164 }
1165
Damiend99b0522013-12-21 18:17:45 +00001166 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001167 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001168 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001169#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001170 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001171 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001172 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001173 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001174#endif
Damien3ef4abb2013-10-12 16:53:13 +01001175#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001176 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001177 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001178#endif
Damien6cdd3af2013-10-05 18:08:26 +01001179 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001180 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001181 }
1182
1183 return true;
1184}
1185
Damiend99b0522013-12-21 18:17:45 +00001186void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001187 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001188 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001189 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1190
Damien6cdd3af2013-10-05 18:08:26 +01001191 // inherit emit options for this function/class definition
1192 uint emit_options = comp->scope_cur->emit_options;
1193
1194 // compile each decorator
1195 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001196 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001197 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1198 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001199
1200 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001201 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001202 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1203
1204 // check for built-in decorators
1205 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1206 // this was a built-in
1207 num_built_in_decorators += 1;
1208
1209 } else {
1210 // not a built-in, compile normally
1211
1212 // compile the decorator function
1213 compile_node(comp, name_nodes[0]);
1214 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001215 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001216 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001217 }
1218
1219 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001220 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001221 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001222 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001223 compile_node(comp, pns_decorator->nodes[1]);
1224 }
Damien429d7192013-10-04 19:53:11 +01001225 }
1226 }
1227
1228 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001229 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001230 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001231 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001232 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001233 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001234 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001235 } else {
1236 // shouldn't happen
1237 assert(0);
1238 }
1239
1240 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001241 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001242 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001243 }
1244
1245 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001246 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001247}
1248
Damiend99b0522013-12-21 18:17:45 +00001249void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001250 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001251 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001252 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001253}
1254
Damien George6be0b0a2014-08-15 14:30:52 +01001255STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001256 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001257 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001258 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1259 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001260
1261 compile_node(comp, pns->nodes[0]); // base of the power node
1262
Damiend99b0522013-12-21 18:17:45 +00001263 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1264 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1265 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1266 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001267 for (int i = 0; i < n - 1; i++) {
1268 compile_node(comp, pns1->nodes[i]);
1269 }
Damiend99b0522013-12-21 18:17:45 +00001270 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1271 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001272 }
Damiend99b0522013-12-21 18:17:45 +00001273 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001274 // can't delete function calls
1275 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001276 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001277 compile_node(comp, pns1->nodes[0]);
1278 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001279 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1280 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001281 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001282 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001283 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001284 }
1285 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001286 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001287 }
1288
Damiend99b0522013-12-21 18:17:45 +00001289 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001290 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001291 }
Damiend99b0522013-12-21 18:17:45 +00001292 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1293 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1294 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1295 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001296 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1297
Damiend99b0522013-12-21 18:17:45 +00001298 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1299 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1300 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001301 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001302 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001303 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001304 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001305 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001306 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001307 c_del_stmt(comp, pns->nodes[0]);
1308 for (int i = 0; i < n; i++) {
1309 c_del_stmt(comp, pns1->nodes[i]);
1310 }
Damiend99b0522013-12-21 18:17:45 +00001311 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001312 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001313 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001314 } else {
1315 // sequence with 2 items
1316 goto sequence_with_2_items;
1317 }
1318 } else {
1319 // sequence with 2 items
1320 sequence_with_2_items:
1321 c_del_stmt(comp, pns->nodes[0]);
1322 c_del_stmt(comp, pns->nodes[1]);
1323 }
1324 } else {
1325 // tuple with 1 element
1326 c_del_stmt(comp, pn);
1327 }
1328 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001329 // TODO is there anything else to implement?
1330 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001331 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001332
1333 return;
1334
1335cannot_delete:
1336 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001337}
1338
Damiend99b0522013-12-21 18:17:45 +00001339void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001340 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1341}
1342
Damiend99b0522013-12-21 18:17:45 +00001343void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001344 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001345 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001346 }
Damien Georgecbddb272014-02-01 20:08:18 +00001347 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001348}
1349
Damiend99b0522013-12-21 18:17:45 +00001350void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001351 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001352 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001353 }
Damien Georgecbddb272014-02-01 20:08:18 +00001354 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001355}
1356
Damiend99b0522013-12-21 18:17:45 +00001357void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001358 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001359 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001360 return;
1361 }
Damiend99b0522013-12-21 18:17:45 +00001362 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001363 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001364 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001365 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001366 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001367 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1368 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 +01001369
Damien George6f355fd2014-04-10 14:11:31 +01001370 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001371 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1372 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1373 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001374 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001375 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1376 } else {
1377 compile_node(comp, pns->nodes[0]);
1378 }
1379 EMIT(return_value);
1380}
1381
Damiend99b0522013-12-21 18:17:45 +00001382void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001383 compile_node(comp, pns->nodes[0]);
1384 EMIT(pop_top);
1385}
1386
Damiend99b0522013-12-21 18:17:45 +00001387void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1388 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001389 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001390 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001391 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001392 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001393 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001394 compile_node(comp, pns->nodes[0]);
1395 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001396 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001397 } else {
1398 // raise x
1399 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001400 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001401 }
1402}
1403
Damien George635543c2014-04-10 12:56:52 +01001404// q_base holds the base of the name
1405// eg a -> q_base=a
1406// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001407STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001408 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001409 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1410 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001411 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001412 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001413 pn = pns->nodes[0];
1414 is_as = true;
1415 }
Damien George635543c2014-04-10 12:56:52 +01001416 if (MP_PARSE_NODE_IS_NULL(pn)) {
1417 // empty name (eg, from . import x)
1418 *q_base = MP_QSTR_;
1419 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1420 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001421 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001422 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001423 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001424 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001425 }
Damien George635543c2014-04-10 12:56:52 +01001426 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001427 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1428 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1429 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001430 // a name of the form a.b.c
1431 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001432 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001433 }
Damiend99b0522013-12-21 18:17:45 +00001434 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001435 int len = n - 1;
1436 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001437 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001438 }
Damien George55baff42014-01-21 21:40:13 +00001439 byte *q_ptr;
1440 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001441 for (int i = 0; i < n; i++) {
1442 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001443 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001444 }
Damien George55baff42014-01-21 21:40:13 +00001445 uint str_src_len;
1446 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001447 memcpy(str_dest, str_src, str_src_len);
1448 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001449 }
Damien George635543c2014-04-10 12:56:52 +01001450 qstr q_full = qstr_build_end(q_ptr);
1451 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001452 if (is_as) {
1453 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001454 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001455 }
1456 }
1457 } else {
Damien George635543c2014-04-10 12:56:52 +01001458 // shouldn't happen
1459 assert(0);
Damien429d7192013-10-04 19:53:11 +01001460 }
1461 } else {
Damien George635543c2014-04-10 12:56:52 +01001462 // shouldn't happen
1463 assert(0);
Damien429d7192013-10-04 19:53:11 +01001464 }
1465}
1466
Damien George6be0b0a2014-08-15 14:30:52 +01001467STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001468 EMIT_ARG(load_const_small_int, 0); // level 0 import
1469 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1470 qstr q_base;
1471 do_import_name(comp, pn, &q_base);
1472 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001473}
1474
Damiend99b0522013-12-21 18:17:45 +00001475void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001476 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1477}
1478
Damiend99b0522013-12-21 18:17:45 +00001479void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001480 mp_parse_node_t pn_import_source = pns->nodes[0];
1481
1482 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1483 uint import_level = 0;
1484 do {
1485 mp_parse_node_t pn_rel;
1486 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)) {
1487 // This covers relative imports with dots only like "from .. import"
1488 pn_rel = pn_import_source;
1489 pn_import_source = MP_PARSE_NODE_NULL;
1490 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1491 // This covers relative imports starting with dot(s) like "from .foo import"
1492 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1493 pn_rel = pns_2b->nodes[0];
1494 pn_import_source = pns_2b->nodes[1];
1495 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1496 } else {
1497 // Not a relative import
1498 break;
1499 }
1500
1501 // get the list of . and/or ...'s
1502 mp_parse_node_t *nodes;
1503 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1504
1505 // count the total number of .'s
1506 for (int i = 0; i < n; i++) {
1507 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1508 import_level++;
1509 } else {
1510 // should be an MP_TOKEN_ELLIPSIS
1511 import_level += 3;
1512 }
1513 }
1514 } while (0);
1515
Damiend99b0522013-12-21 18:17:45 +00001516 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001517 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001518
1519 // build the "fromlist" tuple
1520#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001521 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001522#else
Damien George708c0732014-04-27 19:23:46 +01001523 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001524 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001525#endif
1526
1527 // do the import
Damien George635543c2014-04-10 12:56:52 +01001528 qstr dummy_q;
1529 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001530 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001531
Damien429d7192013-10-04 19:53:11 +01001532 } else {
Damien George635543c2014-04-10 12:56:52 +01001533 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001534
1535 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001536 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001537 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001538#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001539 {
1540 vstr_t *vstr = vstr_new();
1541 vstr_printf(vstr, "(");
1542 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001543 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1544 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1545 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001546 if (i > 0) {
1547 vstr_printf(vstr, ", ");
1548 }
1549 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001550 uint len;
1551 const byte *str = qstr_data(id2, &len);
1552 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001553 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001554 }
Damien02f89412013-12-12 15:13:36 +00001555 if (n == 1) {
1556 vstr_printf(vstr, ",");
1557 }
1558 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001559 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001560 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001561 }
Damiendb4c3612013-12-10 17:27:24 +00001562#else
1563 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001564 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1565 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1566 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001567 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001568 }
Damien Georgeb9791222014-01-23 00:34:21 +00001569 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001570#endif
1571
1572 // do the import
Damien George635543c2014-04-10 12:56:52 +01001573 qstr dummy_q;
1574 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001575 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001576 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1577 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1578 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001579 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001580 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001581 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001582 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001583 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001584 }
1585 }
1586 EMIT(pop_top);
1587 }
1588}
1589
Damiend99b0522013-12-21 18:17:45 +00001590void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001591 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001592 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1593 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001594 } else {
Damiend99b0522013-12-21 18:17:45 +00001595 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1596 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001597 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001598 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001599 }
Damien429d7192013-10-04 19:53:11 +01001600 }
1601 }
1602}
1603
Damiend99b0522013-12-21 18:17:45 +00001604void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001605 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001606 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1607 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001608 } else {
Damiend99b0522013-12-21 18:17:45 +00001609 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1610 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001611 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001612 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001613 }
Damien429d7192013-10-04 19:53:11 +01001614 }
1615 }
1616}
1617
Damiend99b0522013-12-21 18:17:45 +00001618void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001619 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001620 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001621 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 +00001622 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001623 // assertion message
1624 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001625 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001626 }
Damien Georgeb9791222014-01-23 00:34:21 +00001627 EMIT_ARG(raise_varargs, 1);
1628 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001629}
1630
Damiend99b0522013-12-21 18:17:45 +00001631void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001632 // TODO proper and/or short circuiting
1633
Damien George6f355fd2014-04-10 14:11:31 +01001634 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001635
Damien George6f355fd2014-04-10 14:11:31 +01001636 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001637 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1638
1639 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001640
1641 if (
1642#if !MICROPY_EMIT_CPYTHON
1643 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1644 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1645#endif
1646 // optimisation to not jump if last instruction was return
1647 !EMIT(last_emit_was_return_value)
1648 ) {
1649 // jump over elif/else blocks
1650 EMIT_ARG(jump, l_end);
1651 }
1652
Damien Georgeb9791222014-01-23 00:34:21 +00001653 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001654
Damiend99b0522013-12-21 18:17:45 +00001655 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001656 // compile elif blocks
1657
Damiend99b0522013-12-21 18:17:45 +00001658 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001659
Damiend99b0522013-12-21 18:17:45 +00001660 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001661 // multiple elif blocks
1662
Damiend99b0522013-12-21 18:17:45 +00001663 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001664 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001665 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001666 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001667 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1668
1669 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001670 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001671 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001672 }
Damien Georgeb9791222014-01-23 00:34:21 +00001673 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001674 }
1675
1676 } else {
1677 // a single elif block
1678
Damienb05d7072013-10-05 13:37:10 +01001679 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001680 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1681
1682 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001683 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001684 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001685 }
Damien Georgeb9791222014-01-23 00:34:21 +00001686 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001687 }
1688 }
1689
1690 // compile else block
1691 compile_node(comp, pns->nodes[3]); // can be null
1692
Damien Georgeb9791222014-01-23 00:34:21 +00001693 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001694}
1695
Damien Georgecbddb272014-02-01 20:08:18 +00001696#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001697 uint old_break_label = comp->break_label; \
1698 uint old_continue_label = comp->continue_label; \
1699 uint break_label = comp_next_label(comp); \
1700 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001701 comp->break_label = break_label; \
1702 comp->continue_label = continue_label; \
1703 comp->break_continue_except_level = comp->cur_except_level;
1704
1705#define END_BREAK_CONTINUE_BLOCK \
1706 comp->break_label = old_break_label; \
1707 comp->continue_label = old_continue_label; \
1708 comp->break_continue_except_level = comp->cur_except_level;
1709
Damiend99b0522013-12-21 18:17:45 +00001710void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001711 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001712
Damience89a212013-10-15 22:25:17 +01001713 // compared to CPython, we have an optimised version of while loops
1714#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001715 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001716 EMIT_ARG(setup_loop, break_label);
1717 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001718 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1719 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001720 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001721 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001722 }
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001724 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1725 // this is a small hack to agree with CPython
1726 if (!node_is_const_true(pns->nodes[0])) {
1727 EMIT(pop_block);
1728 }
Damience89a212013-10-15 22:25:17 +01001729#else
Damien George6f355fd2014-04-10 14:11:31 +01001730 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001731 EMIT_ARG(jump, continue_label);
1732 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001733 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001734 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001735 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1736#endif
1737
1738 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001739 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001740
1741 compile_node(comp, pns->nodes[2]); // else
1742
Damien Georgeb9791222014-01-23 00:34:21 +00001743 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001744}
1745
Damien George2ac4af62014-08-15 16:45:41 +01001746#if !MICROPY_EMIT_CPYTHON
Damienf72fd0e2013-11-06 20:20:49 +00001747// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001748// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1749// And, if the loop never runs, the loop variable should never be assigned
Damien George6be0b0a2014-08-15 14:30:52 +01001750STATIC 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 +00001751 START_BREAK_CONTINUE_BLOCK
Damien George5b5562c2014-05-31 17:59:11 +01001752 // note that we don't need to pop anything when breaking from an optimise for loop
Damienf72fd0e2013-11-06 20:20:49 +00001753
Damien George6f355fd2014-04-10 14:11:31 +01001754 uint top_label = comp_next_label(comp);
1755 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001756
Damien George3ff2d032014-03-31 18:02:22 +01001757 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001758 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001759 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001760
Damien Georgeb9791222014-01-23 00:34:21 +00001761 EMIT_ARG(jump, entry_label);
1762 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001763
Damien George3ff2d032014-03-31 18:02:22 +01001764 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001765 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001766
1767 // store next value to var
1768 c_assign(comp, pn_var, ASSIGN_STORE);
1769
Damienf3822fc2013-11-09 20:12:03 +00001770 // compile body
1771 compile_node(comp, pn_body);
1772
Damien Georgeb9791222014-01-23 00:34:21 +00001773 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001774
Damien George3ff2d032014-03-31 18:02:22 +01001775 // compile: var + step, duplicated on stack
1776 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001777 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001778 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001779 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001780
Damien Georgeb9791222014-01-23 00:34:21 +00001781 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001782
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001783 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001784 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001785 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1786 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001787 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001788 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001789 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001790 }
Damien Georgeb9791222014-01-23 00:34:21 +00001791 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001792
Damien George3ff2d032014-03-31 18:02:22 +01001793 // discard final value of var that failed the loop condition
1794 EMIT(pop_top);
1795
Damienf72fd0e2013-11-06 20:20:49 +00001796 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001797 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001798
1799 compile_node(comp, pn_else);
1800
Damien Georgeb9791222014-01-23 00:34:21 +00001801 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001802}
Damien George2ac4af62014-08-15 16:45:41 +01001803#endif
Damienf72fd0e2013-11-06 20:20:49 +00001804
Damiend99b0522013-12-21 18:17:45 +00001805void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001806#if !MICROPY_EMIT_CPYTHON
1807 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1808 // this is actually slower, but uses no heap memory
1809 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001810 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 +00001811 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001812 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1813 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1814 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1815 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001816 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1817 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001818 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001819 mp_parse_node_t pn_range_start;
1820 mp_parse_node_t pn_range_end;
1821 mp_parse_node_t pn_range_step;
1822 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001823 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001824 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001825 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001826 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001827 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001828 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001829 } else if (n_args == 2) {
1830 pn_range_start = args[0];
1831 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001832 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001833 } else {
1834 pn_range_start = args[0];
1835 pn_range_end = args[1];
1836 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001837 // We need to know sign of step. This is possible only if it's constant
1838 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1839 optimize = false;
1840 }
Damienf72fd0e2013-11-06 20:20:49 +00001841 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001842 }
1843 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001844 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1845 return;
1846 }
1847 }
1848 }
1849#endif
1850
Damien Georgecbddb272014-02-01 20:08:18 +00001851 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001852 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001853
Damien George6f355fd2014-04-10 14:11:31 +01001854 uint pop_label = comp_next_label(comp);
1855 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001856
Damience89a212013-10-15 22:25:17 +01001857 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1858#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001859 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001860#endif
1861
Damien429d7192013-10-04 19:53:11 +01001862 compile_node(comp, pns->nodes[1]); // iterator
1863 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001864 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001865 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001866 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1867 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001868 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001869 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001870 }
Damien Georgeb9791222014-01-23 00:34:21 +00001871 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001872 EMIT(for_iter_end);
1873
1874 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001875 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001876
Damience89a212013-10-15 22:25:17 +01001877#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001878 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001879#endif
Damien429d7192013-10-04 19:53:11 +01001880
1881 compile_node(comp, pns->nodes[3]); // else (not tested)
1882
Damien Georgeb9791222014-01-23 00:34:21 +00001883 EMIT_ARG(label_assign, break_label);
1884 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001885}
1886
Damien George6be0b0a2014-08-15 14:30:52 +01001887STATIC 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 +01001888 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001889 uint l1 = comp_next_label(comp);
1890 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001891
Damien Georgeb9791222014-01-23 00:34:21 +00001892 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001893 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001894
Damien429d7192013-10-04 19:53:11 +01001895 compile_node(comp, pn_body); // body
1896 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001897 EMIT_ARG(jump, success_label); // jump over exception handler
1898
1899 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001900 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001901
Damien George6f355fd2014-04-10 14:11:31 +01001902 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001903
1904 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001905 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1906 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001907
1908 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001909 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001910
Damiend99b0522013-12-21 18:17:45 +00001911 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001912 // this is a catch all exception handler
1913 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001914 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001915 return;
1916 }
1917 } else {
1918 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001919 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1920 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1921 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1922 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001923 // handler binds the exception to a local
1924 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001925 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001926 }
1927 }
1928 EMIT(dup_top);
1929 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001930 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001931 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001932 }
1933
1934 EMIT(pop_top);
1935
1936 if (qstr_exception_local == 0) {
1937 EMIT(pop_top);
1938 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001939 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001940 }
1941
1942 EMIT(pop_top);
1943
Damien George6f355fd2014-04-10 14:11:31 +01001944 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001945 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001946 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001947 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001948 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001949 }
1950 compile_node(comp, pns_except->nodes[1]);
1951 if (qstr_exception_local != 0) {
1952 EMIT(pop_block);
1953 }
1954 EMIT(pop_except);
1955 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001956 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1957 EMIT_ARG(label_assign, l3);
1958 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1959 EMIT_ARG(store_id, qstr_exception_local);
1960 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001961
Damien George8dcc0c72014-03-27 10:55:21 +00001962 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001963 EMIT(end_finally);
1964 }
Damien Georgeb9791222014-01-23 00:34:21 +00001965 EMIT_ARG(jump, l2);
1966 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001967 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001968 }
1969
Damien George8dcc0c72014-03-27 10:55:21 +00001970 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001971 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001972 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001973
Damien Georgeb9791222014-01-23 00:34:21 +00001974 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001975 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001976 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001977}
1978
Damien George6be0b0a2014-08-15 14:30:52 +01001979STATIC 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 +01001980 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001981
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001983 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001984
Damien429d7192013-10-04 19:53:11 +01001985 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001986 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001987 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001988 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001989 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001990 } else {
1991 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1992 }
1993 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001994 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1995 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001996 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001997
Damien George8dcc0c72014-03-27 10:55:21 +00001998 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001999 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002000}
2001
Damiend99b0522013-12-21 18:17:45 +00002002void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2003 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2004 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2005 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002006 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002007 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2008 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002009 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002010 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002011 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002012 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002013 // no finally
2014 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2015 } else {
2016 // have finally
Damiend99b0522013-12-21 18:17:45 +00002017 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 +01002018 }
2019 } else {
2020 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002021 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002022 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002023 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002024 }
2025 } else {
2026 // shouldn't happen
2027 assert(0);
2028 }
2029}
2030
Damien George6be0b0a2014-08-15 14:30:52 +01002031STATIC 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 +01002032 if (n == 0) {
2033 // no more pre-bits, compile the body of the with
2034 compile_node(comp, body);
2035 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002036 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002037 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002038 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002039 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002040 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002041 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002042 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2043 } else {
2044 // this pre-bit is just an expression
2045 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002046 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002047 EMIT(pop_top);
2048 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002049 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002050 // compile additional pre-bits and the body
2051 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2052 // finish this with block
2053 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002054 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2055 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002056 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002057 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002058 EMIT(end_finally);
2059 }
2060}
2061
Damiend99b0522013-12-21 18:17:45 +00002062void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002063 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002064 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002065 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2066 assert(n > 0);
2067
2068 // compile in a nested fashion
2069 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2070}
2071
Damiend99b0522013-12-21 18:17:45 +00002072void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2073 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002074 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2075 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002076 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002077 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002078 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002079 EMIT(pop_top);
2080
Damien429d7192013-10-04 19:53:11 +01002081 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002082 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002083 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2084 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002085 // do nothing with a lonely constant
2086 } else {
2087 compile_node(comp, pns->nodes[0]); // just an expression
2088 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2089 }
Damien429d7192013-10-04 19:53:11 +01002090 }
2091 } else {
Damiend99b0522013-12-21 18:17:45 +00002092 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2093 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002094 if (kind == PN_expr_stmt_augassign) {
2095 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2096 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002097 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002098 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002099 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002100 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2101 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2102 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2103 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2104 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2105 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2106 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2107 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2108 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2109 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2110 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2111 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2112 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002113 }
Damien George7e5fb242014-02-01 22:18:47 +00002114 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002115 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2116 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002117 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2118 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002119 // following CPython, we store left-most first
2120 if (rhs > 0) {
2121 EMIT(dup_top);
2122 }
2123 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2124 for (int i = 0; i < rhs; i++) {
2125 if (i + 1 < rhs) {
2126 EMIT(dup_top);
2127 }
Damiend99b0522013-12-21 18:17:45 +00002128 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002129 }
2130 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002131 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002132 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2133 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2134 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002135 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002136 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2137 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002138 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2139 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2140 // can't optimise when it's a star expression on the lhs
2141 goto no_optimisation;
2142 }
Damien429d7192013-10-04 19:53:11 +01002143 compile_node(comp, pns10->nodes[0]); // rhs
2144 compile_node(comp, pns10->nodes[1]); // rhs
2145 EMIT(rot_two);
2146 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2147 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002148 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2149 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2150 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2151 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002152 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002153 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2154 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002155 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2156 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2157 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2158 // can't optimise when it's a star expression on the lhs
2159 goto no_optimisation;
2160 }
Damien429d7192013-10-04 19:53:11 +01002161 compile_node(comp, pns10->nodes[0]); // rhs
2162 compile_node(comp, pns10->nodes[1]); // rhs
2163 compile_node(comp, pns10->nodes[2]); // rhs
2164 EMIT(rot_three);
2165 EMIT(rot_two);
2166 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2167 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2168 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2169 } else {
Damien George495d7812014-04-08 17:51:47 +01002170 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002171 compile_node(comp, pns1->nodes[0]); // rhs
2172 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2173 }
2174 } else {
2175 // shouldn't happen
2176 assert(0);
2177 }
2178 }
2179}
2180
Damien George6be0b0a2014-08-15 14:30:52 +01002181STATIC 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 +00002182 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002183 compile_node(comp, pns->nodes[0]);
2184 for (int i = 1; i < num_nodes; i += 1) {
2185 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002186 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002187 }
2188}
2189
Damiend99b0522013-12-21 18:17:45 +00002190void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2191 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2192 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002193
Damien George6f355fd2014-04-10 14:11:31 +01002194 uint l_fail = comp_next_label(comp);
2195 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002196 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2197 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002198 EMIT_ARG(jump, l_end);
2199 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002200 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002201 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002202 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002203}
2204
Damiend99b0522013-12-21 18:17:45 +00002205void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002206 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002207 //mp_parse_node_t pn_params = pns->nodes[0];
2208 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002209
Damien George36db6bc2014-05-07 17:24:22 +01002210 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002211 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002212 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 +01002213 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002214 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002215 }
2216
2217 // get the scope for this lambda
2218 scope_t *this_scope = (scope_t*)pns->nodes[2];
2219
2220 // make the lambda
2221 close_over_variables_etc(comp, this_scope, 0, 0);
2222}
2223
Damiend99b0522013-12-21 18:17:45 +00002224void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002225 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002226 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002227 for (int i = 0; i < n; i += 1) {
2228 compile_node(comp, pns->nodes[i]);
2229 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002230 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002231 }
2232 }
Damien Georgeb9791222014-01-23 00:34:21 +00002233 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002234}
2235
Damiend99b0522013-12-21 18:17:45 +00002236void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002237 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002238 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002239 for (int i = 0; i < n; i += 1) {
2240 compile_node(comp, pns->nodes[i]);
2241 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002242 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002243 }
2244 }
Damien Georgeb9791222014-01-23 00:34:21 +00002245 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002246}
2247
Damiend99b0522013-12-21 18:17:45 +00002248void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002249 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002250 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002251}
2252
Damiend99b0522013-12-21 18:17:45 +00002253void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002254 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002255 compile_node(comp, pns->nodes[0]);
2256 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002257 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002258 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002259 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002260 }
2261 for (int i = 1; i + 1 < num_nodes; i += 2) {
2262 compile_node(comp, pns->nodes[i + 1]);
2263 if (i + 2 < num_nodes) {
2264 EMIT(dup_top);
2265 EMIT(rot_three);
2266 }
Damien George7e5fb242014-02-01 22:18:47 +00002267 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002268 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002269 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002270 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2271 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2272 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2273 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2274 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2275 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2276 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2277 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002278 }
2279 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002280 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2281 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2282 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002283 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002284 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002285 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002286 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002287 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002288 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002289 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002290 }
2291 } else {
2292 // shouldn't happen
2293 assert(0);
2294 }
2295 } else {
2296 // shouldn't happen
2297 assert(0);
2298 }
2299 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002300 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002301 }
2302 }
2303 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002304 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002305 EMIT_ARG(jump, l_end);
2306 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002307 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002308 EMIT(rot_two);
2309 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002310 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002311 }
2312}
2313
Damiend99b0522013-12-21 18:17:45 +00002314void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002315 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002316}
2317
Damiend99b0522013-12-21 18:17:45 +00002318void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002319 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002320}
2321
Damiend99b0522013-12-21 18:17:45 +00002322void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002323 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002324}
2325
Damiend99b0522013-12-21 18:17:45 +00002326void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002327 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002328}
2329
Damiend99b0522013-12-21 18:17:45 +00002330void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2331 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002332 compile_node(comp, pns->nodes[0]);
2333 for (int i = 1; i + 1 < num_nodes; i += 2) {
2334 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002335 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002336 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002337 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002338 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002339 } else {
2340 // shouldn't happen
2341 assert(0);
2342 }
2343 }
2344}
2345
Damiend99b0522013-12-21 18:17:45 +00002346void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2347 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002348 compile_node(comp, pns->nodes[0]);
2349 for (int i = 1; i + 1 < num_nodes; i += 2) {
2350 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002351 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002352 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002353 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002354 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002355 } else {
2356 // shouldn't happen
2357 assert(0);
2358 }
2359 }
2360}
2361
Damiend99b0522013-12-21 18:17:45 +00002362void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2363 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002364 compile_node(comp, pns->nodes[0]);
2365 for (int i = 1; i + 1 < num_nodes; i += 2) {
2366 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002367 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002368 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002369 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002370 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002371 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002372 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002373 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002374 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002375 } else {
2376 // shouldn't happen
2377 assert(0);
2378 }
2379 }
2380}
2381
Damiend99b0522013-12-21 18:17:45 +00002382void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002383 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002384 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002385 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002386 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002387 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002388 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002389 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002390 } else {
2391 // shouldn't happen
2392 assert(0);
2393 }
2394}
2395
Damien George35e2a4e2014-02-05 00:51:47 +00002396void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2397 // this is to handle special super() call
2398 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2399
2400 compile_generic_all_nodes(comp, pns);
2401}
2402
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002403STATIC 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 +01002404 // function to call is on top of stack
2405
Damien George35e2a4e2014-02-05 00:51:47 +00002406#if !MICROPY_EMIT_CPYTHON
2407 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002408 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 +00002409 EMIT_ARG(load_id, MP_QSTR___class__);
2410 // get first argument to function
2411 bool found = false;
2412 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002413 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2414 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George35e2a4e2014-02-05 00:51:47 +00002415 found = true;
2416 break;
2417 }
2418 }
2419 if (!found) {
2420 printf("TypeError: super() call cannot find self\n");
2421 return;
2422 }
Damien George922ddd62014-04-09 12:43:17 +01002423 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002424 return;
2425 }
2426#endif
2427
Damien George2c0842b2014-04-27 16:46:51 +01002428 // get the list of arguments
2429 mp_parse_node_t *args;
2430 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002431
Damien George2c0842b2014-04-27 16:46:51 +01002432 // compile the arguments
2433 // Rather than calling compile_node on the list, we go through the list of args
2434 // explicitly here so that we can count the number of arguments and give sensible
2435 // error messages.
2436 int n_positional = n_positional_extra;
2437 uint n_keyword = 0;
2438 uint star_flags = 0;
2439 for (int i = 0; i < n_args; i++) {
2440 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2441 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2442 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2443 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2444 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2445 return;
2446 }
2447 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2448 compile_node(comp, pns_arg->nodes[0]);
2449 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2450 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2451 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2452 return;
2453 }
2454 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2455 compile_node(comp, pns_arg->nodes[0]);
2456 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2457 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2458 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2459 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2460 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2461 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2462 return;
2463 }
Damien George968bf342014-04-27 19:12:05 +01002464 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002465 compile_node(comp, pns2->nodes[0]);
2466 n_keyword += 1;
2467 } else {
2468 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2469 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2470 n_positional++;
2471 }
2472 } else {
2473 goto normal_argument;
2474 }
2475 } else {
2476 normal_argument:
2477 if (n_keyword > 0) {
2478 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2479 return;
2480 }
2481 compile_node(comp, args[i]);
2482 n_positional++;
2483 }
Damien429d7192013-10-04 19:53:11 +01002484 }
2485
Damien George2c0842b2014-04-27 16:46:51 +01002486 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002487 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002488 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002489 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002490 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002491 }
Damien429d7192013-10-04 19:53:11 +01002492}
2493
Damiend99b0522013-12-21 18:17:45 +00002494void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2495 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002496 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002497 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 +01002498 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002499 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2500 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002501 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002502 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002503 i += 1;
2504 } else {
2505 compile_node(comp, pns->nodes[i]);
2506 }
Damien George35e2a4e2014-02-05 00:51:47 +00002507 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002508 }
2509}
2510
Damiend99b0522013-12-21 18:17:45 +00002511void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002512 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002513 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002514}
2515
Damiend99b0522013-12-21 18:17:45 +00002516void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002517 // a list of strings
Damien63321742013-12-10 17:41:49 +00002518
2519 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002520 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002521 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002522 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002523 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002524 int pn_kind;
2525 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2526 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2527 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2528 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2529 } else {
2530 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2531 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2532 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2533 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002534 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002535 }
Damien63321742013-12-10 17:41:49 +00002536 if (i == 0) {
2537 string_kind = pn_kind;
2538 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002539 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002540 return;
2541 }
Damien429d7192013-10-04 19:53:11 +01002542 }
Damien63321742013-12-10 17:41:49 +00002543
Damien63321742013-12-10 17:41:49 +00002544 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002545 byte *q_ptr;
2546 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002547 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002548 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2549 uint s_len;
2550 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2551 memcpy(s_dest, s, s_len);
2552 s_dest += s_len;
2553 } else {
2554 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002555 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2556 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002557 }
Damien63321742013-12-10 17:41:49 +00002558 }
Damien George55baff42014-01-21 21:40:13 +00002559 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002560
Damien Georgeb9791222014-01-23 00:34:21 +00002561 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002562}
2563
2564// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002565STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002566 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2567 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2568 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002569
Damien George36db6bc2014-05-07 17:24:22 +01002570 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002571 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002572 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 +01002573 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002574 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002575 }
2576
2577 // get the scope for this comprehension
2578 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2579
2580 // compile the comprehension
2581 close_over_variables_etc(comp, this_scope, 0, 0);
2582
2583 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2584 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002585 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002586}
2587
Damiend99b0522013-12-21 18:17:45 +00002588void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2589 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002590 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002591 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2592 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2593 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2594 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2595 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2596 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2597 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002598 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002599 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002600 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002601 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002602 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002603 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002604 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002605 // generator expression
2606 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2607 } else {
2608 // tuple with 2 items
2609 goto tuple_with_2_items;
2610 }
2611 } else {
2612 // tuple with 2 items
2613 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002614 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002615 }
2616 } else {
2617 // parenthesis around a single item, is just that item
2618 compile_node(comp, pns->nodes[0]);
2619 }
2620}
2621
Damiend99b0522013-12-21 18:17:45 +00002622void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2623 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002624 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002625 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002626 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2627 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2628 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2629 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2630 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002631 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002632 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002633 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002634 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002635 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002636 // list of many items
2637 compile_node(comp, pns2->nodes[0]);
2638 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002639 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002640 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002641 // list comprehension
2642 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2643 } else {
2644 // list with 2 items
2645 goto list_with_2_items;
2646 }
2647 } else {
2648 // list with 2 items
2649 list_with_2_items:
2650 compile_node(comp, pns2->nodes[0]);
2651 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002652 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002653 }
2654 } else {
2655 // list with 1 item
2656 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002657 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002658 }
2659}
2660
Damiend99b0522013-12-21 18:17:45 +00002661void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2662 mp_parse_node_t pn = pns->nodes[0];
2663 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002664 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002665 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002666 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2667 pns = (mp_parse_node_struct_t*)pn;
2668 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002669 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002670 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002671 compile_node(comp, pn);
2672 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002673 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2674 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2675 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2676 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002677 // dict/set with multiple elements
2678
2679 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002680 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002681 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2682
2683 // first element sets whether it's a dict or set
2684 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002685 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002686 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002687 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002688 compile_node(comp, pns->nodes[0]);
2689 EMIT(store_map);
2690 is_dict = true;
2691 } else {
2692 // a set
2693 compile_node(comp, pns->nodes[0]); // 1st value of set
2694 is_dict = false;
2695 }
2696
2697 // process rest of elements
2698 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002699 mp_parse_node_t pn = nodes[i];
2700 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002701 compile_node(comp, pn);
2702 if (is_dict) {
2703 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002704 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002705 return;
2706 }
2707 EMIT(store_map);
2708 } else {
2709 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002710 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002711 return;
2712 }
2713 }
2714 }
2715
2716 // if it's a set, build it
2717 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002718 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002719 }
Damiend99b0522013-12-21 18:17:45 +00002720 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002721 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002722 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002723 // a dictionary comprehension
2724 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2725 } else {
2726 // a set comprehension
2727 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2728 }
2729 } else {
2730 // shouldn't happen
2731 assert(0);
2732 }
2733 } else {
2734 // set with one element
2735 goto set_with_one_element;
2736 }
2737 } else {
2738 // set with one element
2739 set_with_one_element:
2740 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002741 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002742 }
2743}
2744
Damiend99b0522013-12-21 18:17:45 +00002745void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002746 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002747}
2748
Damiend99b0522013-12-21 18:17:45 +00002749void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002750 // object who's index we want is on top of stack
2751 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002752 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002753}
2754
Damiend99b0522013-12-21 18:17:45 +00002755void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002756 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002757 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002758}
2759
Damiend99b0522013-12-21 18:17:45 +00002760void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2761 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2762 mp_parse_node_t pn = pns->nodes[0];
2763 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002764 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002765 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2766 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002767 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2768 pns = (mp_parse_node_struct_t*)pn;
2769 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002770 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002771 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002772 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002773 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002774 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002775 } else {
2776 // [?::x]
2777 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002778 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002779 }
Damiend99b0522013-12-21 18:17:45 +00002780 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002781 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002782 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2783 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2784 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2785 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002786 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002787 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002788 } else {
2789 // [?:x:x]
2790 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002791 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002792 }
2793 } else {
2794 // [?:x]
2795 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002796 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002797 }
2798 } else {
2799 // [?:x]
2800 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002801 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002802 }
2803}
2804
Damiend99b0522013-12-21 18:17:45 +00002805void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002806 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002807 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2808 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002809}
2810
Damiend99b0522013-12-21 18:17:45 +00002811void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002812 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002813 compile_subscript_3_helper(comp, pns);
2814}
2815
Damiend99b0522013-12-21 18:17:45 +00002816void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002817 // if this is called then we are compiling a dict key:value pair
2818 compile_node(comp, pns->nodes[1]); // value
2819 compile_node(comp, pns->nodes[0]); // key
2820}
2821
Damiend99b0522013-12-21 18:17:45 +00002822void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002823 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002824 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002825 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002826}
2827
Damiend99b0522013-12-21 18:17:45 +00002828void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002829 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002830 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002831 return;
2832 }
Damiend99b0522013-12-21 18:17:45 +00002833 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002834 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002835 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002836 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2837 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002838 compile_node(comp, pns->nodes[0]);
2839 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002840 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002841 EMIT(yield_from);
2842 } else {
2843 compile_node(comp, pns->nodes[0]);
2844 EMIT(yield_value);
2845 }
2846}
2847
Damiend99b0522013-12-21 18:17:45 +00002848typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002849STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002850 NULL,
2851#define nc NULL
2852#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002853#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002854#include "grammar.h"
2855#undef nc
2856#undef c
2857#undef DEF_RULE
2858};
2859
Damien George6be0b0a2014-08-15 14:30:52 +01002860STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002861 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002862 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002863 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002864 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002865 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002866 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002867 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002868 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002869 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002870 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2871 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2872 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2873 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002874 case MP_PARSE_NODE_TOKEN:
2875 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002876 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002877 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002878 // do nothing
2879 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002880 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002881 }
2882 break;
Damien429d7192013-10-04 19:53:11 +01002883 default: assert(0);
2884 }
2885 } else {
Damiend99b0522013-12-21 18:17:45 +00002886 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002887 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002888 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002889 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 +01002890 } else {
Damien George5042bce2014-05-25 22:06:06 +01002891 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2892 if (f == NULL) {
2893 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2894#if MICROPY_DEBUG_PRINTERS
2895 mp_parse_node_print(pn, 0);
2896#endif
2897 compile_syntax_error(comp, pn, "internal compiler error");
2898 } else {
2899 f(comp, pns);
2900 }
Damien429d7192013-10-04 19:53:11 +01002901 }
2902 }
2903}
2904
Damien George2ac4af62014-08-15 16:45:41 +01002905STATIC 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 +01002906 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002907 qstr param_name = MP_QSTR_NULL;
2908 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002909 if (MP_PARSE_NODE_IS_ID(pn)) {
2910 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002911 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002912 // comes after a star, so counts as a keyword-only parameter
2913 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002914 } else {
Damien George2827d622014-04-27 15:50:52 +01002915 // comes before a star, so counts as a positional parameter
2916 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002917 }
Damienb14de212013-10-06 00:28:28 +01002918 } else {
Damiend99b0522013-12-21 18:17:45 +00002919 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2920 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2921 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2922 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002923 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002924 // comes after a star, so counts as a keyword-only parameter
2925 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002926 } else {
Damien George2827d622014-04-27 15:50:52 +01002927 // comes before a star, so counts as a positional parameter
2928 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002929 }
Damiend99b0522013-12-21 18:17:45 +00002930 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002931 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002932 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002933 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002934 // bare star
2935 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002936 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002937 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002938 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002939 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002940 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01002941 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002942 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002943 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002944 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2945 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002946 } else {
2947 // shouldn't happen
2948 assert(0);
2949 }
Damiend99b0522013-12-21 18:17:45 +00002950 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2951 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002952 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002953 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002954 } else {
Damienb14de212013-10-06 00:28:28 +01002955 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002956 assert(0);
2957 }
Damien429d7192013-10-04 19:53:11 +01002958 }
2959
Damien George2827d622014-04-27 15:50:52 +01002960 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002961 bool added;
2962 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2963 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002964 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002965 return;
2966 }
Damien429d7192013-10-04 19:53:11 +01002967 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002968 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002969 }
2970}
2971
Damien George2827d622014-04-27 15:50:52 +01002972STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002973 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002974}
2975
Damien George2827d622014-04-27 15:50:52 +01002976STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002977 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2978}
2979
2980STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2981 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2982 // no annotation
2983 return;
2984 }
2985
2986 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2987 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2988 // named parameter with possible annotation
2989 // fallthrough
2990 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2991 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2992 // named star with possible annotation
2993 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2994 // fallthrough
2995 } else {
2996 // no annotation
2997 return;
2998 }
2999 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3000 // double star with possible annotation
3001 // fallthrough
3002 } else {
3003 // no annotation
3004 return;
3005 }
3006
3007 mp_parse_node_t pn_annotation = pns->nodes[1];
3008
3009 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3010 #if MICROPY_EMIT_NATIVE
3011 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3012 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3013 assert(id_info != NULL);
3014
3015 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3016 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3017 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3018 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3019 } else {
3020 compile_syntax_error(comp, pn_annotation, "annotation must be an identifier");
3021 }
3022 }
3023 #endif // MICROPY_EMIT_NATIVE
3024 }
Damien429d7192013-10-04 19:53:11 +01003025}
3026
Damien George6be0b0a2014-08-15 14:30:52 +01003027STATIC 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 +01003028 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003029 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003030 // no more nested if/for; compile inner expression
3031 compile_node(comp, pn_inner_expr);
3032 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003033 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003034 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003035 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003036 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003037 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003038 } else {
3039 EMIT(yield_value);
3040 EMIT(pop_top);
3041 }
Damiend99b0522013-12-21 18:17:45 +00003042 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003043 // if condition
Damiend99b0522013-12-21 18:17:45 +00003044 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003045 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3046 pn_iter = pns_comp_if->nodes[1];
3047 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003048 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003049 // for loop
Damiend99b0522013-12-21 18:17:45 +00003050 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003051 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003052 uint l_end2 = comp_next_label(comp);
3053 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003054 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003055 EMIT_ARG(label_assign, l_top2);
3056 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003057 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3058 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 +00003059 EMIT_ARG(jump, l_top2);
3060 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003061 EMIT(for_iter_end);
3062 } else {
3063 // shouldn't happen
3064 assert(0);
3065 }
3066}
3067
Damien George1463c1f2014-04-25 23:52:57 +01003068STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3069#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003070 // see http://www.python.org/dev/peps/pep-0257/
3071
3072 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003073 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003074 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003075 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003076 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003077 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3078 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003079 for (int i = 0; i < num_nodes; i++) {
3080 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003081 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 +00003082 // not a newline, so this is the first statement; finish search
3083 break;
3084 }
3085 }
3086 // 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 +00003087 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003088 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003089 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003090 } else {
3091 return;
3092 }
3093
3094 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003095 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3096 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003097 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3098 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3099 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3100 // compile the doc string
3101 compile_node(comp, pns->nodes[0]);
3102 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003103 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003104 }
3105 }
Damien George1463c1f2014-04-25 23:52:57 +01003106#endif
Damien429d7192013-10-04 19:53:11 +01003107}
3108
Damien George1463c1f2014-04-25 23:52:57 +01003109STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003110 comp->pass = pass;
3111 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003112 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003113 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003114
Damien George36db6bc2014-05-07 17:24:22 +01003115 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003116 // reset maximum stack sizes in scope
3117 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003118 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003119 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003120 }
3121
Damien5ac1b2e2013-10-18 19:58:12 +01003122#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003123 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003124 scope_print_info(scope);
3125 }
Damien5ac1b2e2013-10-18 19:58:12 +01003126#endif
Damien429d7192013-10-04 19:53:11 +01003127
3128 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003129 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3130 assert(scope->kind == SCOPE_MODULE);
3131 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3132 compile_node(comp, pns->nodes[0]); // compile the expression
3133 EMIT(return_value);
3134 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003135 if (!comp->is_repl) {
3136 check_for_doc_string(comp, scope->pn);
3137 }
Damien429d7192013-10-04 19:53:11 +01003138 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003139 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003140 EMIT(return_value);
3141 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003142 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3143 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3144 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003145
3146 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003147 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003148 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003149 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003150 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003151 } else {
3152 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003153
Damien George2ac4af62014-08-15 16:45:41 +01003154 // argument annotations
3155 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3156
3157 // pns->nodes[2] is return/whole function annotation
3158 #if MICROPY_EMIT_NATIVE
3159 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3160 // nodes[2] can be null or a test-expr
3161 if (MP_PARSE_NODE_IS_ID(pns->nodes[2])) {
3162 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
3163 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3164 } else {
3165 compile_syntax_error(comp, pns->nodes[2], "annotation must be an identifier");
3166 }
3167 }
3168 #endif // MICROPY_EMIT_NATIVE
3169 }
Damien429d7192013-10-04 19:53:11 +01003170
3171 compile_node(comp, pns->nodes[3]); // 3 is function body
3172 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003173 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003174 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003175 EMIT(return_value);
3176 }
3177 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003178 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3179 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3180 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003181
3182 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003183 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003184 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003185 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003186 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3187 }
3188
3189 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003190
3191 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3192 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3193 EMIT(pop_top);
3194 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3195 }
Damien429d7192013-10-04 19:53:11 +01003196 EMIT(return_value);
3197 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3198 // a bit of a hack at the moment
3199
Damiend99b0522013-12-21 18:17:45 +00003200 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3201 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3202 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3203 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3204 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003205
Damien George708c0732014-04-27 19:23:46 +01003206 // We need a unique name for the comprehension argument (the iterator).
3207 // CPython uses .0, but we should be able to use anything that won't
3208 // clash with a user defined variable. Best to use an existing qstr,
3209 // so we use the blank qstr.
3210#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003211 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003212#else
3213 qstr qstr_arg = MP_QSTR_;
3214#endif
Damien George36db6bc2014-05-07 17:24:22 +01003215 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003216 bool added;
3217 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3218 assert(added);
3219 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003220 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003221 }
3222
3223 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003224 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003225 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003226 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003227 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003228 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003229 }
3230
Damien George6f355fd2014-04-10 14:11:31 +01003231 uint l_end = comp_next_label(comp);
3232 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003233 EMIT_ARG(load_id, qstr_arg);
3234 EMIT_ARG(label_assign, l_top);
3235 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003236 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3237 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003238 EMIT_ARG(jump, l_top);
3239 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003240 EMIT(for_iter_end);
3241
3242 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003243 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003244 }
3245 EMIT(return_value);
3246 } else {
3247 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003248 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3249 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3250 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003251
Damien George36db6bc2014-05-07 17:24:22 +01003252 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003253 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003254 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003255 assert(added);
3256 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003257 }
3258
Damien Georgeb9791222014-01-23 00:34:21 +00003259 EMIT_ARG(load_id, MP_QSTR___name__);
3260 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003261 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 +00003262 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003263
3264 check_for_doc_string(comp, pns->nodes[2]);
3265 compile_node(comp, pns->nodes[2]); // 2 is class body
3266
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003267 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003268 assert(id != NULL);
3269 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003270 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003271 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003272#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003273 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003274#else
Damien George2bf7c092014-04-09 15:26:46 +01003275 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003276#endif
Damien429d7192013-10-04 19:53:11 +01003277 }
3278 EMIT(return_value);
3279 }
3280
Damien415eb6f2013-10-05 12:19:06 +01003281 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003282
3283 // make sure we match all the exception levels
3284 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003285}
3286
Damien George094d4502014-04-02 17:31:27 +01003287#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003288// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003289STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003290 comp->pass = pass;
3291 comp->scope_cur = scope;
3292 comp->next_label = 1;
3293
3294 if (scope->kind != SCOPE_FUNCTION) {
3295 printf("Error: inline assembler must be a function\n");
3296 return;
3297 }
3298
Damien George36db6bc2014-05-07 17:24:22 +01003299 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003300 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003301 }
3302
Damien826005c2013-10-05 23:17:28 +01003303 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003304 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3305 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3306 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003307
Damiend99b0522013-12-21 18:17:45 +00003308 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003309
Damiena2f2f7d2013-10-06 00:14:13 +01003310 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003311 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003312 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003313 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003314 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003315 }
3316
Damiend99b0522013-12-21 18:17:45 +00003317 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003318
Damiend99b0522013-12-21 18:17:45 +00003319 mp_parse_node_t pn_body = pns->nodes[3]; // body
3320 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003321 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3322
Damien Georgecbd2f742014-01-19 11:48:48 +00003323 /*
Damien George36db6bc2014-05-07 17:24:22 +01003324 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003325 //printf("----\n");
3326 scope_print_info(scope);
3327 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003328 */
Damien826005c2013-10-05 23:17:28 +01003329
3330 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003331 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3332 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003333 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3334 // no instructions
3335 continue;
3336 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3337 // an instruction; fall through
3338 } else {
3339 // not an instruction; error
3340 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3341 return;
3342 }
Damiend99b0522013-12-21 18:17:45 +00003343 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3344 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3345 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3346 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3347 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3348 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3349 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3350 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3351 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3352 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003353 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3354
3355 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003356 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003357 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003358 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003359 return;
3360 }
Damien George6f355fd2014-04-10 14:11:31 +01003361 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003362 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003363 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003364 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003365 } else if (op == MP_QSTR_align) {
3366 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3367 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3368 return;
3369 }
Damien George36db6bc2014-05-07 17:24:22 +01003370 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003371 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3372 }
3373 } else if (op == MP_QSTR_data) {
3374 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3375 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3376 return;
3377 }
Damien George36db6bc2014-05-07 17:24:22 +01003378 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003379 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003380 for (uint i = 1; i < n_args; i++) {
3381 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3382 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3383 return;
3384 }
3385 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3386 }
3387 }
Damien826005c2013-10-05 23:17:28 +01003388 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003389 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003390 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003391 }
3392 }
3393 }
3394
Damien George36db6bc2014-05-07 17:24:22 +01003395 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003396 bool success = EMIT_INLINE_ASM(end_pass);
3397 if (!success) {
3398 comp->had_error = true;
3399 }
Damienb05d7072013-10-05 13:37:10 +01003400 }
Damien429d7192013-10-04 19:53:11 +01003401}
Damien George094d4502014-04-02 17:31:27 +01003402#endif
Damien429d7192013-10-04 19:53:11 +01003403
Damien George1463c1f2014-04-25 23:52:57 +01003404STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003405#if !MICROPY_EMIT_CPYTHON
3406 // in Micro Python we put the *x parameter after all other parameters (except **y)
3407 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3408 id_info_t *id_param = NULL;
3409 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3410 id_info_t *id = &scope->id_info[i];
3411 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3412 if (id_param != NULL) {
3413 // swap star param with last param
3414 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3415 }
3416 break;
3417 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3418 id_param = id;
3419 }
3420 }
3421 }
3422#endif
3423
Damien429d7192013-10-04 19:53:11 +01003424 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003425 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003426 scope->num_locals = 0;
3427 for (int i = 0; i < scope->id_info_len; i++) {
3428 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003429 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003430 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3431 continue;
3432 }
3433 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3434 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3435 }
Damien George2827d622014-04-27 15:50:52 +01003436 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003437 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003438 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003439 }
3440 }
3441
3442 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003443#if MICROPY_EMIT_CPYTHON
3444 int num_cell = 0;
3445#endif
Damien9ecbcff2013-12-11 00:41:43 +00003446 for (int i = 0; i < scope->id_info_len; i++) {
3447 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003448#if MICROPY_EMIT_CPYTHON
3449 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003450 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003451 id->local_num = num_cell;
3452 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003453 }
Damien George6baf76e2013-12-30 22:32:17 +00003454#else
3455 // in Micro Python the cells come right after the fast locals
3456 // parameters are not counted here, since they remain at the start
3457 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003458 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003459 id->local_num = scope->num_locals;
3460 scope->num_locals += 1;
3461 }
3462#endif
Damien9ecbcff2013-12-11 00:41:43 +00003463 }
Damien9ecbcff2013-12-11 00:41:43 +00003464
3465 // compute the index of free vars (freevars[idx] in CPython)
3466 // make sure they are in the order of the parent scope
3467 if (scope->parent != NULL) {
3468 int num_free = 0;
3469 for (int i = 0; i < scope->parent->id_info_len; i++) {
3470 id_info_t *id = &scope->parent->id_info[i];
3471 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3472 for (int j = 0; j < scope->id_info_len; j++) {
3473 id_info_t *id2 = &scope->id_info[j];
3474 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003475 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003476#if MICROPY_EMIT_CPYTHON
3477 // in CPython the frees are numbered after the cells
3478 id2->local_num = num_cell + num_free;
3479#else
3480 // in Micro Python the frees come first, before the params
3481 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003482#endif
3483 num_free += 1;
3484 }
3485 }
3486 }
Damien429d7192013-10-04 19:53:11 +01003487 }
Damien George6baf76e2013-12-30 22:32:17 +00003488#if !MICROPY_EMIT_CPYTHON
3489 // in Micro Python shift all other locals after the free locals
3490 if (num_free > 0) {
3491 for (int i = 0; i < scope->id_info_len; i++) {
3492 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003493 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003494 id->local_num += num_free;
3495 }
3496 }
Damien George2827d622014-04-27 15:50:52 +01003497 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 +00003498 scope->num_locals += num_free;
3499 }
3500#endif
Damien429d7192013-10-04 19:53:11 +01003501 }
3502
Damien George8725f8f2014-02-15 19:33:11 +00003503 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003504
3505#if MICROPY_EMIT_CPYTHON
3506 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003507 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) {
3508 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003509 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003510 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003511 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 +00003512 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003513 }
3514 }
Damien George882b3632014-04-02 15:56:31 +01003515#endif
3516
Damien429d7192013-10-04 19:53:11 +01003517 int num_free = 0;
3518 for (int i = 0; i < scope->id_info_len; i++) {
3519 id_info_t *id = &scope->id_info[i];
3520 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3521 num_free += 1;
3522 }
3523 }
3524 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003525 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003526 }
3527}
3528
Damien George65cad122014-04-06 11:48:15 +01003529mp_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 +01003530 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003531 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003532 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003533
Damien826005c2013-10-05 23:17:28 +01003534 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003535 mp_map_t consts;
3536 mp_map_init(&consts, 0);
3537 pn = fold_constants(comp, pn, &consts);
3538 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003539
3540 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003541 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003542
Damien826005c2013-10-05 23:17:28 +01003543 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003544 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003545 comp->emit_method_table = &emit_pass1_method_table;
3546 comp->emit_inline_asm = NULL;
3547 comp->emit_inline_asm_method_table = NULL;
3548 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003549 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003550 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003551#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003552 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003553 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003554#endif
Damien826005c2013-10-05 23:17:28 +01003555 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003556 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003557 }
3558
3559 // update maximim number of labels needed
3560 if (comp->next_label > max_num_labels) {
3561 max_num_labels = comp->next_label;
3562 }
Damien429d7192013-10-04 19:53:11 +01003563 }
3564
Damien826005c2013-10-05 23:17:28 +01003565 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003566 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003567 compile_scope_compute_things(comp, s);
3568 }
3569
Damien826005c2013-10-05 23:17:28 +01003570 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003571 emit_pass1_free(comp->emit);
3572
Damien826005c2013-10-05 23:17:28 +01003573 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003574#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003575 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003576#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003577 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003578#endif
Damien3ef4abb2013-10-12 16:53:13 +01003579#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003580 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003581#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003582#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003583 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003584 if (false) {
3585 // dummy
3586
Damien3ef4abb2013-10-12 16:53:13 +01003587#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003588 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003589 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003590 if (emit_inline_thumb == NULL) {
3591 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3592 }
3593 comp->emit = NULL;
3594 comp->emit_method_table = NULL;
3595 comp->emit_inline_asm = emit_inline_thumb;
3596 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003597 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea26dc502014-04-12 17:54:52 +01003598 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003599 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003600 }
Damienc025ebb2013-10-12 14:30:21 +01003601#endif
3602
Damien826005c2013-10-05 23:17:28 +01003603 } else {
Damienc025ebb2013-10-12 14:30:21 +01003604
3605 // choose the emit type
3606
Damien3ef4abb2013-10-12 16:53:13 +01003607#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003608 comp->emit = emit_cpython_new(max_num_labels);
3609 comp->emit_method_table = &emit_cpython_method_table;
3610#else
Damien826005c2013-10-05 23:17:28 +01003611 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003612
3613#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003614 case MP_EMIT_OPT_NATIVE_PYTHON:
3615 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003616#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003617 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003618 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003619 }
Damien13ed3a62013-10-08 09:05:10 +01003620 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003621#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003622 if (emit_native == NULL) {
3623 emit_native = emit_native_thumb_new(max_num_labels);
3624 }
3625 comp->emit_method_table = &emit_native_thumb_method_table;
3626#endif
3627 comp->emit = emit_native;
Damien George2ac4af62014-08-15 16:45:41 +01003628 comp->emit_method_table->set_native_type(comp->emit, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien George36db6bc2014-05-07 17:24:22 +01003629
3630 // native emitters need an extra pass to compute stack size
3631 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3632
Damien7af3d192013-10-07 00:02:49 +01003633 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003634#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003635
Damien826005c2013-10-05 23:17:28 +01003636 default:
3637 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003638 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003639 }
3640 comp->emit = emit_bc;
3641 comp->emit_method_table = &emit_bc_method_table;
3642 break;
3643 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003644#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003645
Damien George36db6bc2014-05-07 17:24:22 +01003646 // second last pass: compute code size
Damien Georgea26dc502014-04-12 17:54:52 +01003647 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003648 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3649 }
3650
3651 // final pass: emit code
3652 if (!comp->had_error) {
3653 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003654 }
Damien6cdd3af2013-10-05 18:08:26 +01003655 }
Damien429d7192013-10-04 19:53:11 +01003656 }
3657
Damien George41d02b62014-01-24 22:42:28 +00003658 // free the emitters
3659#if !MICROPY_EMIT_CPYTHON
3660 if (emit_bc != NULL) {
3661 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003662 }
Damien George41d02b62014-01-24 22:42:28 +00003663#if MICROPY_EMIT_NATIVE
3664 if (emit_native != NULL) {
3665#if MICROPY_EMIT_X64
3666 emit_native_x64_free(emit_native);
3667#elif MICROPY_EMIT_THUMB
3668 emit_native_thumb_free(emit_native);
3669#endif
3670 }
3671#endif
3672#if MICROPY_EMIT_INLINE_THUMB
3673 if (emit_inline_thumb != NULL) {
3674 emit_inline_thumb_free(emit_inline_thumb);
3675 }
3676#endif
3677#endif // !MICROPY_EMIT_CPYTHON
3678
3679 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003680 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003681 for (scope_t *s = module_scope; s;) {
3682 scope_t *next = s->next;
3683 scope_free(s);
3684 s = next;
3685 }
Damien5ac1b2e2013-10-18 19:58:12 +01003686
Damien George41d02b62014-01-24 22:42:28 +00003687 // free the compiler
3688 bool had_error = comp->had_error;
3689 m_del_obj(compiler_t, comp);
3690
Damien George1fb03172014-01-03 14:22:03 +00003691 if (had_error) {
3692 // TODO return a proper error message
3693 return mp_const_none;
3694 } else {
3695#if MICROPY_EMIT_CPYTHON
3696 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003697 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003698 return mp_const_true;
3699#else
3700 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003701 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003702#endif
3703 }
Damien429d7192013-10-04 19:53:11 +01003704}