blob: a9d19d2a949c75676ff02260c3c32dcb4041808e [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;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100716 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
717 // cond is something in parenthesis
718 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
719 // empty tuple, acts as false for the condition
720 if (jump_if == false) {
721 EMIT_ARG(jump, label);
722 }
723 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
724 // non-empty tuple, acts as true for the condition
725 if (jump_if == true) {
726 EMIT_ARG(jump, label);
727 }
728 } else {
729 // parenthesis around 1 item, is just that item
730 c_if_cond(comp, pns->nodes[0], jump_if, label);
731 }
732 return;
Damien3a205172013-10-12 15:01:56 +0100733 }
734 }
735
736 // nothing special, fall back to default compiling for node and jump
737 compile_node(comp, pn);
738 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000739 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100740 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000741 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100742 }
743#endif
Damien429d7192013-10-04 19:53:11 +0100744}
745
746typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100747STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100748
Damien George6be0b0a2014-08-15 14:30:52 +0100749STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100750 if (assign_kind != ASSIGN_AUG_STORE) {
751 compile_node(comp, pns->nodes[0]);
752 }
753
Damiend99b0522013-12-21 18:17:45 +0000754 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
755 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
756 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
757 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100758 if (assign_kind != ASSIGN_AUG_STORE) {
759 for (int i = 0; i < n - 1; i++) {
760 compile_node(comp, pns1->nodes[i]);
761 }
762 }
Damiend99b0522013-12-21 18:17:45 +0000763 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
764 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100765 }
Damiend99b0522013-12-21 18:17:45 +0000766 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100767 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000768 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100769 if (assign_kind == ASSIGN_AUG_STORE) {
770 EMIT(rot_three);
771 EMIT(store_subscr);
772 } else {
773 compile_node(comp, pns1->nodes[0]);
774 if (assign_kind == ASSIGN_AUG_LOAD) {
775 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100776 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100777 } else {
778 EMIT(store_subscr);
779 }
780 }
Damiend99b0522013-12-21 18:17:45 +0000781 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
782 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100783 if (assign_kind == ASSIGN_AUG_LOAD) {
784 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000785 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100786 } else {
787 if (assign_kind == ASSIGN_AUG_STORE) {
788 EMIT(rot_two);
789 }
Damien Georgeb9791222014-01-23 00:34:21 +0000790 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100791 }
792 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100793 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100794 }
795 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100796 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100797 }
798
Damiend99b0522013-12-21 18:17:45 +0000799 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100800 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100801 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100802
803 return;
804
805cannot_assign:
806 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100807}
808
Damien George0288cf02014-04-11 11:53:00 +0000809// 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 +0100810STATIC 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 +0000811 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
812
813 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100814 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000815 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
816 EMIT_ARG(unpack_ex, 0, num_tail);
817 have_star_index = 0;
818 }
819 for (int i = 0; i < num_tail; i++) {
820 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100821 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000822 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
823 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100824 } else {
Damien George9d181f62014-04-27 16:55:27 +0100825 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100826 return;
827 }
828 }
829 }
830 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000831 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100832 }
Damien George0288cf02014-04-11 11:53:00 +0000833 if (num_head != 0) {
834 if (0 == have_star_index) {
835 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100836 } else {
Damien George0288cf02014-04-11 11:53:00 +0000837 c_assign(comp, node_head, ASSIGN_STORE);
838 }
839 }
840 for (int i = 0; i < num_tail; i++) {
841 if (num_head + i == have_star_index) {
842 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
843 } else {
844 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100845 }
846 }
847}
848
849// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100850STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100851 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000852 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100853 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000854 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
855 if (MP_PARSE_NODE_IS_ID(pn)) {
856 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100857 switch (assign_kind) {
858 case ASSIGN_STORE:
859 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000860 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100861 break;
862 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000863 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100864 break;
865 }
866 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100867 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100868 return;
869 }
870 } else {
Damiend99b0522013-12-21 18:17:45 +0000871 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
872 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100873 case PN_power:
874 // lhs is an index or attribute
875 c_assign_power(comp, pns, assign_kind);
876 break;
877
878 case PN_testlist_star_expr:
879 case PN_exprlist:
880 // lhs is a tuple
881 if (assign_kind != ASSIGN_STORE) {
882 goto bad_aug;
883 }
Damien George0288cf02014-04-11 11:53:00 +0000884 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100885 break;
886
887 case PN_atom_paren:
888 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000889 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100890 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100891 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000892 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
893 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100894 goto testlist_comp;
895 } else {
896 // parenthesis around 1 item, is just that item
897 pn = pns->nodes[0];
898 goto tail_recursion;
899 }
900 break;
901
902 case PN_atom_bracket:
903 // lhs is something in brackets
904 if (assign_kind != ASSIGN_STORE) {
905 goto bad_aug;
906 }
Damiend99b0522013-12-21 18:17:45 +0000907 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100908 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000909 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000910 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
911 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100912 goto testlist_comp;
913 } else {
914 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000915 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100916 }
917 break;
918
919 default:
Damien George9d181f62014-04-27 16:55:27 +0100920 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100921 }
922 return;
923
924 testlist_comp:
925 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000926 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
927 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
928 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100929 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000930 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000931 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000932 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100933 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000934 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
935 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000936 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100937 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100938 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100939 } else {
940 // sequence with 2 items
941 goto sequence_with_2_items;
942 }
943 } else {
944 // sequence with 2 items
945 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000946 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100947 }
948 return;
949 }
950 return;
951
Damien George9d181f62014-04-27 16:55:27 +0100952 cannot_assign:
953 compile_syntax_error(comp, pn, "can't assign to expression");
954 return;
955
Damien429d7192013-10-04 19:53:11 +0100956 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100957 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100958}
959
960// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100961// if we are not in CPython compatibility mode then:
962// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
963// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
964// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100965STATIC 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 +0100966 assert(n_pos_defaults >= 0);
967 assert(n_kw_defaults >= 0);
968
Damien429d7192013-10-04 19:53:11 +0100969 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000970 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100971 int nfree = 0;
972 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000973 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
974 id_info_t *id = &comp->scope_cur->id_info[i];
975 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
976 for (int j = 0; j < this_scope->id_info_len; j++) {
977 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100978 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000979#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100980 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000981#else
982 // in Micro Python we load closures using LOAD_FAST
Damien George7ff996c2014-09-08 23:05:16 +0100983 EMIT_ARG(load_fast, id->qst, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000984#endif
Damien318aec62013-12-10 18:28:17 +0000985 nfree += 1;
986 }
987 }
Damien429d7192013-10-04 19:53:11 +0100988 }
989 }
990 }
Damien429d7192013-10-04 19:53:11 +0100991
992 // make the function/closure
993 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100994 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100995 } else {
Damien George3558f622014-04-20 17:50:40 +0100996 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100997 }
998}
999
Damien George6be0b0a2014-08-15 14:30:52 +01001000STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001001 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001002 comp->have_star = true;
1003 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001004 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1005 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001006 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001007 } else {
1008 // named star
Damien429d7192013-10-04 19:53:11 +01001009 }
Damien George8b19db02014-04-11 23:25:34 +01001010 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001011
1012 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001013 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001014 // TODO do we need to do anything with this?
1015
1016 } else {
1017 mp_parse_node_t pn_id;
1018 mp_parse_node_t pn_colon;
1019 mp_parse_node_t pn_equal;
1020 if (MP_PARSE_NODE_IS_ID(pn)) {
1021 // this parameter is just an id
1022
1023 pn_id = pn;
1024 pn_colon = MP_PARSE_NODE_NULL;
1025 pn_equal = MP_PARSE_NODE_NULL;
1026
1027 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1028 // this parameter has a colon and/or equal specifier
1029
1030 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1031 pn_id = pns->nodes[0];
1032 pn_colon = pns->nodes[1];
1033 pn_equal = pns->nodes[2];
1034
1035 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001036 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001037 assert(0);
1038 return;
1039 }
1040
1041 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1042 // this parameter does not have a default value
1043
1044 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001045 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001046 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001047 return;
1048 }
1049
1050 } else {
1051 // this parameter has a default value
1052 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1053
Damien George8b19db02014-04-11 23:25:34 +01001054 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001055 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001056#if MICROPY_EMIT_CPYTHON
1057 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1058 compile_node(comp, pn_equal);
1059#else
Damien George69b89d22014-04-11 13:38:30 +00001060 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1061 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001062 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1063 // we need to do this here before we start building the map for the default keywords
1064 if (comp->num_default_params > 0) {
1065 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001066 } else {
1067 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001068 }
Damien George69b89d22014-04-11 13:38:30 +00001069 // first default dict param, so make the map
1070 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001071 }
Damien Georgef0778a72014-06-07 22:01:00 +01001072
1073 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001074 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001075 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001076 EMIT(store_map);
1077#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001078 } else {
Damien George69b89d22014-04-11 13:38:30 +00001079 comp->num_default_params += 1;
1080 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001081 }
1082 }
1083
1084 // TODO pn_colon not implemented
1085 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001086 }
1087}
1088
1089// leaves function object on stack
1090// returns function name
Damiend99b0522013-12-21 18:17:45 +00001091qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001092 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001093 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001094 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001095 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001096 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001097 }
1098
1099 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001100 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001101 uint old_num_dict_params = comp->num_dict_params;
1102 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001103
1104 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001105 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001106 comp->num_dict_params = 0;
1107 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001108 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001109
1110 if (comp->had_error) {
1111 return MP_QSTR_NULL;
1112 }
1113
Damien Georgee337f1e2014-03-31 15:18:37 +01001114#if !MICROPY_EMIT_CPYTHON
1115 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001116 // the default keywords args may have already made the tuple; if not, do it now
1117 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001118 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001119 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001120 }
1121#endif
1122
Damien429d7192013-10-04 19:53:11 +01001123 // get the scope for this function
1124 scope_t *fscope = (scope_t*)pns->nodes[4];
1125
1126 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001127 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001128
1129 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001130 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001131 comp->num_dict_params = old_num_dict_params;
1132 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001133
1134 // return its name (the 'f' in "def f(...):")
1135 return fscope->simple_name;
1136}
1137
1138// leaves class object on stack
1139// returns class name
Damiend99b0522013-12-21 18:17:45 +00001140qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001141 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001142 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001143 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001144 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001145 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001146 }
1147
1148 EMIT(load_build_class);
1149
1150 // scope for this class
1151 scope_t *cscope = (scope_t*)pns->nodes[3];
1152
1153 // compile the class
1154 close_over_variables_etc(comp, cscope, 0, 0);
1155
1156 // get its name
Damien George968bf342014-04-27 19:12:05 +01001157 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001158
1159 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001160 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1161 mp_parse_node_t parents = pns->nodes[1];
1162 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1163 parents = MP_PARSE_NODE_NULL;
1164 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001165 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001166 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001167
1168 // return its name (the 'C' in class C(...):")
1169 return cscope->simple_name;
1170}
1171
Damien6cdd3af2013-10-05 18:08:26 +01001172// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001173STATIC 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 +00001174 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001175 return false;
1176 }
1177
1178 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001179 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001180 return true;
1181 }
1182
Damiend99b0522013-12-21 18:17:45 +00001183 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001184 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001185 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001186#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001187 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001188 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001189 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001190 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001191#endif
Damien3ef4abb2013-10-12 16:53:13 +01001192#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001193 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001194 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001195#endif
Damien6cdd3af2013-10-05 18:08:26 +01001196 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001197 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001198 }
1199
1200 return true;
1201}
1202
Damiend99b0522013-12-21 18:17:45 +00001203void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001204 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001205 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001206 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1207
Damien6cdd3af2013-10-05 18:08:26 +01001208 // inherit emit options for this function/class definition
1209 uint emit_options = comp->scope_cur->emit_options;
1210
1211 // compile each decorator
1212 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001213 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001214 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1215 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001216
1217 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001218 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001219 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1220
1221 // check for built-in decorators
1222 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1223 // this was a built-in
1224 num_built_in_decorators += 1;
1225
1226 } else {
1227 // not a built-in, compile normally
1228
1229 // compile the decorator function
1230 compile_node(comp, name_nodes[0]);
1231 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001232 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001233 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001234 }
1235
1236 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001237 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001238 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001239 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001240 compile_node(comp, pns_decorator->nodes[1]);
1241 }
Damien429d7192013-10-04 19:53:11 +01001242 }
1243 }
1244
1245 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001246 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001247 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001248 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001249 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001250 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001251 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001252 } else {
1253 // shouldn't happen
1254 assert(0);
1255 }
1256
1257 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001258 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001259 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001260 }
1261
1262 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001263 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001264}
1265
Damiend99b0522013-12-21 18:17:45 +00001266void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001267 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001268 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001269 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001270}
1271
Damien George6be0b0a2014-08-15 14:30:52 +01001272STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001273 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001274 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001275 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1276 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001277
1278 compile_node(comp, pns->nodes[0]); // base of the power node
1279
Damiend99b0522013-12-21 18:17:45 +00001280 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1281 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1282 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1283 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001284 for (int i = 0; i < n - 1; i++) {
1285 compile_node(comp, pns1->nodes[i]);
1286 }
Damiend99b0522013-12-21 18:17:45 +00001287 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1288 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001289 }
Damiend99b0522013-12-21 18:17:45 +00001290 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001291 // can't delete function calls
1292 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001293 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001294 compile_node(comp, pns1->nodes[0]);
1295 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001296 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1297 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001298 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001299 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001300 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001301 }
1302 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001303 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001304 }
1305
Damiend99b0522013-12-21 18:17:45 +00001306 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001307 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001308 }
Damiend99b0522013-12-21 18:17:45 +00001309 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1310 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1311 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1312 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001313 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1314
Damiend99b0522013-12-21 18:17:45 +00001315 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1316 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1317 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001318 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001319 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001320 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001321 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001322 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001323 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001324 c_del_stmt(comp, pns->nodes[0]);
1325 for (int i = 0; i < n; i++) {
1326 c_del_stmt(comp, pns1->nodes[i]);
1327 }
Damiend99b0522013-12-21 18:17:45 +00001328 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001329 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001330 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001331 } else {
1332 // sequence with 2 items
1333 goto sequence_with_2_items;
1334 }
1335 } else {
1336 // sequence with 2 items
1337 sequence_with_2_items:
1338 c_del_stmt(comp, pns->nodes[0]);
1339 c_del_stmt(comp, pns->nodes[1]);
1340 }
1341 } else {
1342 // tuple with 1 element
1343 c_del_stmt(comp, pn);
1344 }
1345 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001346 // TODO is there anything else to implement?
1347 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001348 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001349
1350 return;
1351
1352cannot_delete:
1353 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001354}
1355
Damiend99b0522013-12-21 18:17:45 +00001356void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001357 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1358}
1359
Damiend99b0522013-12-21 18:17:45 +00001360void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001361 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001362 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001363 }
Damien Georgecbddb272014-02-01 20:08:18 +00001364 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001365}
1366
Damiend99b0522013-12-21 18:17:45 +00001367void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001368 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001369 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001370 }
Damien Georgecbddb272014-02-01 20:08:18 +00001371 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001372}
1373
Damiend99b0522013-12-21 18:17:45 +00001374void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001375 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001376 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001377 return;
1378 }
Damiend99b0522013-12-21 18:17:45 +00001379 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001380 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001381 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001382 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001383 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001384 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1385 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 +01001386
Damien George6f355fd2014-04-10 14:11:31 +01001387 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001388 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1389 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1390 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001391 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001392 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1393 } else {
1394 compile_node(comp, pns->nodes[0]);
1395 }
1396 EMIT(return_value);
1397}
1398
Damiend99b0522013-12-21 18:17:45 +00001399void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001400 compile_node(comp, pns->nodes[0]);
1401 EMIT(pop_top);
1402}
1403
Damiend99b0522013-12-21 18:17:45 +00001404void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1405 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001406 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001407 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001408 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001409 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001410 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001411 compile_node(comp, pns->nodes[0]);
1412 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001413 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001414 } else {
1415 // raise x
1416 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001417 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001418 }
1419}
1420
Damien George635543c2014-04-10 12:56:52 +01001421// q_base holds the base of the name
1422// eg a -> q_base=a
1423// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001424STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001425 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001426 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1427 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001428 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001429 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001430 pn = pns->nodes[0];
1431 is_as = true;
1432 }
Damien George635543c2014-04-10 12:56:52 +01001433 if (MP_PARSE_NODE_IS_NULL(pn)) {
1434 // empty name (eg, from . import x)
1435 *q_base = MP_QSTR_;
1436 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1437 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001438 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001439 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001440 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001441 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001442 }
Damien George635543c2014-04-10 12:56:52 +01001443 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001444 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1445 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1446 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001447 // a name of the form a.b.c
1448 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001449 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001450 }
Damiend99b0522013-12-21 18:17:45 +00001451 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001452 int len = n - 1;
1453 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001454 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001455 }
Damien George55baff42014-01-21 21:40:13 +00001456 byte *q_ptr;
1457 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001458 for (int i = 0; i < n; i++) {
1459 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001460 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001461 }
Damien George55baff42014-01-21 21:40:13 +00001462 uint str_src_len;
1463 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001464 memcpy(str_dest, str_src, str_src_len);
1465 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001466 }
Damien George635543c2014-04-10 12:56:52 +01001467 qstr q_full = qstr_build_end(q_ptr);
1468 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001469 if (is_as) {
1470 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001471 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001472 }
1473 }
1474 } else {
Damien George635543c2014-04-10 12:56:52 +01001475 // shouldn't happen
1476 assert(0);
Damien429d7192013-10-04 19:53:11 +01001477 }
1478 } else {
Damien George635543c2014-04-10 12:56:52 +01001479 // shouldn't happen
1480 assert(0);
Damien429d7192013-10-04 19:53:11 +01001481 }
1482}
1483
Damien George6be0b0a2014-08-15 14:30:52 +01001484STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001485 EMIT_ARG(load_const_small_int, 0); // level 0 import
1486 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1487 qstr q_base;
1488 do_import_name(comp, pn, &q_base);
1489 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001490}
1491
Damiend99b0522013-12-21 18:17:45 +00001492void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001493 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1494}
1495
Damiend99b0522013-12-21 18:17:45 +00001496void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001497 mp_parse_node_t pn_import_source = pns->nodes[0];
1498
1499 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1500 uint import_level = 0;
1501 do {
1502 mp_parse_node_t pn_rel;
1503 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)) {
1504 // This covers relative imports with dots only like "from .. import"
1505 pn_rel = pn_import_source;
1506 pn_import_source = MP_PARSE_NODE_NULL;
1507 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1508 // This covers relative imports starting with dot(s) like "from .foo import"
1509 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1510 pn_rel = pns_2b->nodes[0];
1511 pn_import_source = pns_2b->nodes[1];
1512 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1513 } else {
1514 // Not a relative import
1515 break;
1516 }
1517
1518 // get the list of . and/or ...'s
1519 mp_parse_node_t *nodes;
1520 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1521
1522 // count the total number of .'s
1523 for (int i = 0; i < n; i++) {
1524 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1525 import_level++;
1526 } else {
1527 // should be an MP_TOKEN_ELLIPSIS
1528 import_level += 3;
1529 }
1530 }
1531 } while (0);
1532
Damiend99b0522013-12-21 18:17:45 +00001533 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001534 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001535
1536 // build the "fromlist" tuple
1537#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001538 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001539#else
Damien George708c0732014-04-27 19:23:46 +01001540 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001541 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001542#endif
1543
1544 // do the import
Damien George635543c2014-04-10 12:56:52 +01001545 qstr dummy_q;
1546 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001547 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001548
Damien429d7192013-10-04 19:53:11 +01001549 } else {
Damien George635543c2014-04-10 12:56:52 +01001550 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001551
1552 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001553 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001554 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001555#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001556 {
1557 vstr_t *vstr = vstr_new();
1558 vstr_printf(vstr, "(");
1559 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001560 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1561 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1562 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001563 if (i > 0) {
1564 vstr_printf(vstr, ", ");
1565 }
1566 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001567 uint len;
1568 const byte *str = qstr_data(id2, &len);
1569 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001570 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001571 }
Damien02f89412013-12-12 15:13:36 +00001572 if (n == 1) {
1573 vstr_printf(vstr, ",");
1574 }
1575 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001576 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001577 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001578 }
Damiendb4c3612013-12-10 17:27:24 +00001579#else
1580 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001581 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1582 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1583 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001584 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001585 }
Damien Georgeb9791222014-01-23 00:34:21 +00001586 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001587#endif
1588
1589 // do the import
Damien George635543c2014-04-10 12:56:52 +01001590 qstr dummy_q;
1591 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001592 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001593 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1594 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1595 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001596 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001597 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001598 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001599 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001600 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001601 }
1602 }
1603 EMIT(pop_top);
1604 }
1605}
1606
Damiend99b0522013-12-21 18:17:45 +00001607void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001608 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001609 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1610 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001611 } else {
Damiend99b0522013-12-21 18:17:45 +00001612 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1613 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001614 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001615 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001616 }
Damien429d7192013-10-04 19:53:11 +01001617 }
1618 }
1619}
1620
Damiend99b0522013-12-21 18:17:45 +00001621void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001622 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001623 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1624 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001625 } else {
Damiend99b0522013-12-21 18:17:45 +00001626 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1627 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001628 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001629 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001630 }
Damien429d7192013-10-04 19:53:11 +01001631 }
1632 }
1633}
1634
Damiend99b0522013-12-21 18:17:45 +00001635void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001636 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001637 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001638 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 +00001639 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001640 // assertion message
1641 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001642 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001643 }
Damien Georgeb9791222014-01-23 00:34:21 +00001644 EMIT_ARG(raise_varargs, 1);
1645 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001646}
1647
Damiend99b0522013-12-21 18:17:45 +00001648void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001649 // TODO proper and/or short circuiting
1650
Damien George6f355fd2014-04-10 14:11:31 +01001651 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001652
Damien George6f355fd2014-04-10 14:11:31 +01001653 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001654 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1655
1656 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001657
1658 if (
1659#if !MICROPY_EMIT_CPYTHON
1660 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1661 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1662#endif
1663 // optimisation to not jump if last instruction was return
1664 !EMIT(last_emit_was_return_value)
1665 ) {
1666 // jump over elif/else blocks
1667 EMIT_ARG(jump, l_end);
1668 }
1669
Damien Georgeb9791222014-01-23 00:34:21 +00001670 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001671
Damiend99b0522013-12-21 18:17:45 +00001672 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001673 // compile elif blocks
1674
Damiend99b0522013-12-21 18:17:45 +00001675 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001676
Damiend99b0522013-12-21 18:17:45 +00001677 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001678 // multiple elif blocks
1679
Damiend99b0522013-12-21 18:17:45 +00001680 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001681 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001682 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001683 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001684 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1685
1686 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001687 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001688 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001689 }
Damien Georgeb9791222014-01-23 00:34:21 +00001690 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001691 }
1692
1693 } else {
1694 // a single elif block
1695
Damienb05d7072013-10-05 13:37:10 +01001696 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001697 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1698
1699 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001700 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001701 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001702 }
Damien Georgeb9791222014-01-23 00:34:21 +00001703 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001704 }
1705 }
1706
1707 // compile else block
1708 compile_node(comp, pns->nodes[3]); // can be null
1709
Damien Georgeb9791222014-01-23 00:34:21 +00001710 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001711}
1712
Damien Georgecbddb272014-02-01 20:08:18 +00001713#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001714 uint old_break_label = comp->break_label; \
1715 uint old_continue_label = comp->continue_label; \
1716 uint break_label = comp_next_label(comp); \
1717 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001718 comp->break_label = break_label; \
1719 comp->continue_label = continue_label; \
1720 comp->break_continue_except_level = comp->cur_except_level;
1721
1722#define END_BREAK_CONTINUE_BLOCK \
1723 comp->break_label = old_break_label; \
1724 comp->continue_label = old_continue_label; \
1725 comp->break_continue_except_level = comp->cur_except_level;
1726
Damiend99b0522013-12-21 18:17:45 +00001727void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001728 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001729
Damience89a212013-10-15 22:25:17 +01001730 // compared to CPython, we have an optimised version of while loops
1731#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001732 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001733 EMIT_ARG(setup_loop, break_label);
1734 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001735 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1736 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001737 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001738 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001739 }
Damien Georgeb9791222014-01-23 00:34:21 +00001740 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001741 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1742 // this is a small hack to agree with CPython
1743 if (!node_is_const_true(pns->nodes[0])) {
1744 EMIT(pop_block);
1745 }
Damience89a212013-10-15 22:25:17 +01001746#else
Damien George6f355fd2014-04-10 14:11:31 +01001747 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001748 EMIT_ARG(jump, continue_label);
1749 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001750 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001751 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001752 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1753#endif
1754
1755 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001756 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001757
1758 compile_node(comp, pns->nodes[2]); // else
1759
Damien Georgeb9791222014-01-23 00:34:21 +00001760 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001761}
1762
Damien George2ac4af62014-08-15 16:45:41 +01001763#if !MICROPY_EMIT_CPYTHON
Damienf72fd0e2013-11-06 20:20:49 +00001764// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001765// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1766// And, if the loop never runs, the loop variable should never be assigned
Damien George6be0b0a2014-08-15 14:30:52 +01001767STATIC 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 +00001768 START_BREAK_CONTINUE_BLOCK
Damien George5b5562c2014-05-31 17:59:11 +01001769 // note that we don't need to pop anything when breaking from an optimise for loop
Damienf72fd0e2013-11-06 20:20:49 +00001770
Damien George6f355fd2014-04-10 14:11:31 +01001771 uint top_label = comp_next_label(comp);
1772 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001773
Damien George3ff2d032014-03-31 18:02:22 +01001774 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001775 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001776 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001777
Damien Georgeb9791222014-01-23 00:34:21 +00001778 EMIT_ARG(jump, entry_label);
1779 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001780
Damien George3ff2d032014-03-31 18:02:22 +01001781 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001782 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001783
1784 // store next value to var
1785 c_assign(comp, pn_var, ASSIGN_STORE);
1786
Damienf3822fc2013-11-09 20:12:03 +00001787 // compile body
1788 compile_node(comp, pn_body);
1789
Damien Georgeb9791222014-01-23 00:34:21 +00001790 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001791
Damien George3ff2d032014-03-31 18:02:22 +01001792 // compile: var + step, duplicated on stack
1793 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001794 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001795 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001796 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001797
Damien Georgeb9791222014-01-23 00:34:21 +00001798 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001799
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001800 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001801 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001802 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1803 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001804 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001805 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001806 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001807 }
Damien Georgeb9791222014-01-23 00:34:21 +00001808 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001809
Damien George3ff2d032014-03-31 18:02:22 +01001810 // discard final value of var that failed the loop condition
1811 EMIT(pop_top);
1812
Damienf72fd0e2013-11-06 20:20:49 +00001813 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001814 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001815
1816 compile_node(comp, pn_else);
1817
Damien Georgeb9791222014-01-23 00:34:21 +00001818 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001819}
Damien George2ac4af62014-08-15 16:45:41 +01001820#endif
Damienf72fd0e2013-11-06 20:20:49 +00001821
Damiend99b0522013-12-21 18:17:45 +00001822void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001823#if !MICROPY_EMIT_CPYTHON
1824 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1825 // this is actually slower, but uses no heap memory
1826 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001827 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 +00001828 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001829 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1830 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1831 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1832 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001833 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1834 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001835 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001836 mp_parse_node_t pn_range_start;
1837 mp_parse_node_t pn_range_end;
1838 mp_parse_node_t pn_range_step;
1839 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001840 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001841 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001842 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001843 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001844 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001845 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001846 } else if (n_args == 2) {
1847 pn_range_start = args[0];
1848 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001849 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001850 } else {
1851 pn_range_start = args[0];
1852 pn_range_end = args[1];
1853 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001854 // We need to know sign of step. This is possible only if it's constant
1855 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1856 optimize = false;
1857 }
Damienf72fd0e2013-11-06 20:20:49 +00001858 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001859 }
1860 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001861 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1862 return;
1863 }
1864 }
1865 }
1866#endif
1867
Damien Georgecbddb272014-02-01 20:08:18 +00001868 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001869 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001870
Damien George6f355fd2014-04-10 14:11:31 +01001871 uint pop_label = comp_next_label(comp);
1872 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001873
Damience89a212013-10-15 22:25:17 +01001874 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1875#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001876 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001877#endif
1878
Damien429d7192013-10-04 19:53:11 +01001879 compile_node(comp, pns->nodes[1]); // iterator
1880 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001881 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001882 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001883 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1884 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001885 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001886 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001887 }
Damien Georgeb9791222014-01-23 00:34:21 +00001888 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001889 EMIT(for_iter_end);
1890
1891 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001892 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001893
Damience89a212013-10-15 22:25:17 +01001894#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001895 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001896#endif
Damien429d7192013-10-04 19:53:11 +01001897
1898 compile_node(comp, pns->nodes[3]); // else (not tested)
1899
Damien Georgeb9791222014-01-23 00:34:21 +00001900 EMIT_ARG(label_assign, break_label);
1901 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001902}
1903
Damien George6be0b0a2014-08-15 14:30:52 +01001904STATIC 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 +01001905 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001906 uint l1 = comp_next_label(comp);
1907 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001908
Damien Georgeb9791222014-01-23 00:34:21 +00001909 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001910 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001911
Damien429d7192013-10-04 19:53:11 +01001912 compile_node(comp, pn_body); // body
1913 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001914 EMIT_ARG(jump, success_label); // jump over exception handler
1915
1916 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001917 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001918
Damien George6f355fd2014-04-10 14:11:31 +01001919 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001920
1921 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001922 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1923 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001924
1925 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001926 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001927
Damiend99b0522013-12-21 18:17:45 +00001928 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001929 // this is a catch all exception handler
1930 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001931 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001932 return;
1933 }
1934 } else {
1935 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001936 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1937 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1938 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1939 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001940 // handler binds the exception to a local
1941 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001942 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001943 }
1944 }
1945 EMIT(dup_top);
1946 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001947 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001948 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001949 }
1950
1951 EMIT(pop_top);
1952
1953 if (qstr_exception_local == 0) {
1954 EMIT(pop_top);
1955 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001956 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001957 }
1958
1959 EMIT(pop_top);
1960
Damien George6f355fd2014-04-10 14:11:31 +01001961 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001962 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001963 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001964 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001965 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001966 }
1967 compile_node(comp, pns_except->nodes[1]);
1968 if (qstr_exception_local != 0) {
1969 EMIT(pop_block);
1970 }
1971 EMIT(pop_except);
1972 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001973 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1974 EMIT_ARG(label_assign, l3);
1975 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1976 EMIT_ARG(store_id, qstr_exception_local);
1977 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001978
Damien George8dcc0c72014-03-27 10:55:21 +00001979 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001980 EMIT(end_finally);
1981 }
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(jump, l2);
1983 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001984 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001985 }
1986
Damien George8dcc0c72014-03-27 10:55:21 +00001987 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001988 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001989 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001990
Damien Georgeb9791222014-01-23 00:34:21 +00001991 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001992 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001993 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001994}
1995
Damien George6be0b0a2014-08-15 14:30:52 +01001996STATIC 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 +01001997 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001998
Damien Georgeb9791222014-01-23 00:34:21 +00001999 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002000 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002001
Damien429d7192013-10-04 19:53:11 +01002002 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002003 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002004 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002005 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002006 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002007 } else {
2008 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2009 }
2010 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002011 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2012 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002013 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002014
Damien George8dcc0c72014-03-27 10:55:21 +00002015 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002016 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002017}
2018
Damiend99b0522013-12-21 18:17:45 +00002019void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2020 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2021 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2022 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002023 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002024 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2025 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002026 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002027 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002028 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002029 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002030 // no finally
2031 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2032 } else {
2033 // have finally
Damiend99b0522013-12-21 18:17:45 +00002034 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 +01002035 }
2036 } else {
2037 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002038 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002039 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002040 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002041 }
2042 } else {
2043 // shouldn't happen
2044 assert(0);
2045 }
2046}
2047
Damien George6be0b0a2014-08-15 14:30:52 +01002048STATIC 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 +01002049 if (n == 0) {
2050 // no more pre-bits, compile the body of the with
2051 compile_node(comp, body);
2052 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002053 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002054 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002055 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002056 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002057 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002058 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002059 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2060 } else {
2061 // this pre-bit is just an expression
2062 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002063 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002064 EMIT(pop_top);
2065 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002066 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002067 // compile additional pre-bits and the body
2068 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2069 // finish this with block
2070 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002071 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2072 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002073 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002074 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002075 EMIT(end_finally);
2076 }
2077}
2078
Damiend99b0522013-12-21 18:17:45 +00002079void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002080 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002081 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002082 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2083 assert(n > 0);
2084
2085 // compile in a nested fashion
2086 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2087}
2088
Damiend99b0522013-12-21 18:17:45 +00002089void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2090 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002091 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2092 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002093 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002094 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002095 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002096 EMIT(pop_top);
2097
Damien429d7192013-10-04 19:53:11 +01002098 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002099 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002100 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2101 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002102 // do nothing with a lonely constant
2103 } else {
2104 compile_node(comp, pns->nodes[0]); // just an expression
2105 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2106 }
Damien429d7192013-10-04 19:53:11 +01002107 }
2108 } else {
Damiend99b0522013-12-21 18:17:45 +00002109 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2110 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002111 if (kind == PN_expr_stmt_augassign) {
2112 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2113 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002114 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002115 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002116 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002117 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2118 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2119 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2120 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2121 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2122 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2123 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2124 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2125 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2126 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2127 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2128 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2129 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002130 }
Damien George7e5fb242014-02-01 22:18:47 +00002131 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002132 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2133 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002134 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2135 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002136 // following CPython, we store left-most first
2137 if (rhs > 0) {
2138 EMIT(dup_top);
2139 }
2140 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2141 for (int i = 0; i < rhs; i++) {
2142 if (i + 1 < rhs) {
2143 EMIT(dup_top);
2144 }
Damiend99b0522013-12-21 18:17:45 +00002145 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002146 }
2147 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002148 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002149 && 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]) == 2
2151 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002152 // optimisation for a, b = c, d; 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 // can't optimise when it's a star expression on the lhs
2158 goto no_optimisation;
2159 }
Damien429d7192013-10-04 19:53:11 +01002160 compile_node(comp, pns10->nodes[0]); // rhs
2161 compile_node(comp, pns10->nodes[1]); // rhs
2162 EMIT(rot_two);
2163 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2164 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002165 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2166 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2167 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2168 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002169 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002170 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2171 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002172 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2173 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2174 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2175 // can't optimise when it's a star expression on the lhs
2176 goto no_optimisation;
2177 }
Damien429d7192013-10-04 19:53:11 +01002178 compile_node(comp, pns10->nodes[0]); // rhs
2179 compile_node(comp, pns10->nodes[1]); // rhs
2180 compile_node(comp, pns10->nodes[2]); // rhs
2181 EMIT(rot_three);
2182 EMIT(rot_two);
2183 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2184 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2185 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2186 } else {
Damien George495d7812014-04-08 17:51:47 +01002187 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002188 compile_node(comp, pns1->nodes[0]); // rhs
2189 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2190 }
2191 } else {
2192 // shouldn't happen
2193 assert(0);
2194 }
2195 }
2196}
2197
Damien George6be0b0a2014-08-15 14:30:52 +01002198STATIC 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 +00002199 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002200 compile_node(comp, pns->nodes[0]);
2201 for (int i = 1; i < num_nodes; i += 1) {
2202 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002203 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002204 }
2205}
2206
Damiend99b0522013-12-21 18:17:45 +00002207void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2208 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2209 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002210
Damien George6f355fd2014-04-10 14:11:31 +01002211 uint l_fail = comp_next_label(comp);
2212 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002213 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2214 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002215 EMIT_ARG(jump, l_end);
2216 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002217 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002218 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002219 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002220}
2221
Damiend99b0522013-12-21 18:17:45 +00002222void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002223 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002224 //mp_parse_node_t pn_params = pns->nodes[0];
2225 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002226
Damien George36db6bc2014-05-07 17:24:22 +01002227 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002228 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002229 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 +01002230 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002231 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002232 }
2233
2234 // get the scope for this lambda
2235 scope_t *this_scope = (scope_t*)pns->nodes[2];
2236
2237 // make the lambda
2238 close_over_variables_etc(comp, this_scope, 0, 0);
2239}
2240
Damiend99b0522013-12-21 18:17:45 +00002241void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002242 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002243 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002244 for (int i = 0; i < n; i += 1) {
2245 compile_node(comp, pns->nodes[i]);
2246 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002247 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002248 }
2249 }
Damien Georgeb9791222014-01-23 00:34:21 +00002250 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002251}
2252
Damiend99b0522013-12-21 18:17:45 +00002253void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002254 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002255 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002256 for (int i = 0; i < n; i += 1) {
2257 compile_node(comp, pns->nodes[i]);
2258 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002259 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002260 }
2261 }
Damien Georgeb9791222014-01-23 00:34:21 +00002262 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002263}
2264
Damiend99b0522013-12-21 18:17:45 +00002265void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002266 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002267 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002268}
2269
Damiend99b0522013-12-21 18:17:45 +00002270void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002271 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002272 compile_node(comp, pns->nodes[0]);
2273 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002274 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002275 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002276 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002277 }
2278 for (int i = 1; i + 1 < num_nodes; i += 2) {
2279 compile_node(comp, pns->nodes[i + 1]);
2280 if (i + 2 < num_nodes) {
2281 EMIT(dup_top);
2282 EMIT(rot_three);
2283 }
Damien George7e5fb242014-02-01 22:18:47 +00002284 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002285 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002286 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002287 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2288 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2289 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2290 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2291 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2292 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2293 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2294 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002295 }
2296 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002297 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2298 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2299 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002300 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002301 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002302 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002303 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002304 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002305 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002306 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002307 }
2308 } else {
2309 // shouldn't happen
2310 assert(0);
2311 }
2312 } else {
2313 // shouldn't happen
2314 assert(0);
2315 }
2316 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002317 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002318 }
2319 }
2320 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002321 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002322 EMIT_ARG(jump, l_end);
2323 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002324 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002325 EMIT(rot_two);
2326 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002327 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002328 }
2329}
2330
Damiend99b0522013-12-21 18:17:45 +00002331void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002332 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002333}
2334
Damiend99b0522013-12-21 18:17:45 +00002335void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002336 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002337}
2338
Damiend99b0522013-12-21 18:17:45 +00002339void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002340 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002341}
2342
Damiend99b0522013-12-21 18:17:45 +00002343void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002344 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002345}
2346
Damiend99b0522013-12-21 18:17:45 +00002347void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2348 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002349 compile_node(comp, pns->nodes[0]);
2350 for (int i = 1; i + 1 < num_nodes; i += 2) {
2351 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002352 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002353 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002354 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002355 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002356 } else {
2357 // shouldn't happen
2358 assert(0);
2359 }
2360 }
2361}
2362
Damiend99b0522013-12-21 18:17:45 +00002363void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2364 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002365 compile_node(comp, pns->nodes[0]);
2366 for (int i = 1; i + 1 < num_nodes; i += 2) {
2367 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002368 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002369 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002370 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002371 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002372 } else {
2373 // shouldn't happen
2374 assert(0);
2375 }
2376 }
2377}
2378
Damiend99b0522013-12-21 18:17:45 +00002379void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2380 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002381 compile_node(comp, pns->nodes[0]);
2382 for (int i = 1; i + 1 < num_nodes; i += 2) {
2383 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002384 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002385 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002386 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002387 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002388 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002389 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002390 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002391 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002392 } else {
2393 // shouldn't happen
2394 assert(0);
2395 }
2396 }
2397}
2398
Damiend99b0522013-12-21 18:17:45 +00002399void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002400 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002401 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002402 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002403 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002404 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002405 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002406 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002407 } else {
2408 // shouldn't happen
2409 assert(0);
2410 }
2411}
2412
Damien George35e2a4e2014-02-05 00:51:47 +00002413void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2414 // this is to handle special super() call
2415 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2416
2417 compile_generic_all_nodes(comp, pns);
2418}
2419
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002420STATIC 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 +01002421 // function to call is on top of stack
2422
Damien George35e2a4e2014-02-05 00:51:47 +00002423#if !MICROPY_EMIT_CPYTHON
2424 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002425 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 +00002426 EMIT_ARG(load_id, MP_QSTR___class__);
2427 // get first argument to function
2428 bool found = false;
2429 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002430 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2431 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 +00002432 found = true;
2433 break;
2434 }
2435 }
2436 if (!found) {
2437 printf("TypeError: super() call cannot find self\n");
2438 return;
2439 }
Damien George922ddd62014-04-09 12:43:17 +01002440 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002441 return;
2442 }
2443#endif
2444
Damien George2c0842b2014-04-27 16:46:51 +01002445 // get the list of arguments
2446 mp_parse_node_t *args;
2447 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002448
Damien George2c0842b2014-04-27 16:46:51 +01002449 // compile the arguments
2450 // Rather than calling compile_node on the list, we go through the list of args
2451 // explicitly here so that we can count the number of arguments and give sensible
2452 // error messages.
2453 int n_positional = n_positional_extra;
2454 uint n_keyword = 0;
2455 uint star_flags = 0;
2456 for (int i = 0; i < n_args; i++) {
2457 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2458 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2459 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2460 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2461 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2462 return;
2463 }
2464 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2465 compile_node(comp, pns_arg->nodes[0]);
2466 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2467 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2468 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2469 return;
2470 }
2471 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2472 compile_node(comp, pns_arg->nodes[0]);
2473 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2474 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2475 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2476 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2477 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2478 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2479 return;
2480 }
Damien George968bf342014-04-27 19:12:05 +01002481 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002482 compile_node(comp, pns2->nodes[0]);
2483 n_keyword += 1;
2484 } else {
2485 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2486 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2487 n_positional++;
2488 }
2489 } else {
2490 goto normal_argument;
2491 }
2492 } else {
2493 normal_argument:
2494 if (n_keyword > 0) {
2495 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2496 return;
2497 }
2498 compile_node(comp, args[i]);
2499 n_positional++;
2500 }
Damien429d7192013-10-04 19:53:11 +01002501 }
2502
Damien George2c0842b2014-04-27 16:46:51 +01002503 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002504 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002505 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002506 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002507 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002508 }
Damien429d7192013-10-04 19:53:11 +01002509}
2510
Damiend99b0522013-12-21 18:17:45 +00002511void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2512 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002513 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002514 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 +01002515 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002516 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2517 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002518 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002519 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002520 i += 1;
2521 } else {
2522 compile_node(comp, pns->nodes[i]);
2523 }
Damien George35e2a4e2014-02-05 00:51:47 +00002524 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002525 }
2526}
2527
Damiend99b0522013-12-21 18:17:45 +00002528void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002529 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002530 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002531}
2532
Damiend99b0522013-12-21 18:17:45 +00002533void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002534 // a list of strings
Damien63321742013-12-10 17:41:49 +00002535
2536 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002537 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002538 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002539 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002540 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002541 int pn_kind;
2542 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2543 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2544 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2545 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2546 } else {
2547 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2548 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2549 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2550 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002551 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002552 }
Damien63321742013-12-10 17:41:49 +00002553 if (i == 0) {
2554 string_kind = pn_kind;
2555 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002556 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002557 return;
2558 }
Damien429d7192013-10-04 19:53:11 +01002559 }
Damien63321742013-12-10 17:41:49 +00002560
Damien63321742013-12-10 17:41:49 +00002561 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002562 byte *q_ptr;
2563 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002564 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002565 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2566 uint s_len;
2567 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2568 memcpy(s_dest, s, s_len);
2569 s_dest += s_len;
2570 } else {
2571 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002572 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2573 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002574 }
Damien63321742013-12-10 17:41:49 +00002575 }
Damien George55baff42014-01-21 21:40:13 +00002576 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002577
Damien Georgeb9791222014-01-23 00:34:21 +00002578 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002579}
2580
2581// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002582STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002583 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2584 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2585 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002586
Damien George36db6bc2014-05-07 17:24:22 +01002587 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002588 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002589 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 +01002590 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002591 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002592 }
2593
2594 // get the scope for this comprehension
2595 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2596
2597 // compile the comprehension
2598 close_over_variables_etc(comp, this_scope, 0, 0);
2599
2600 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2601 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002602 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002603}
2604
Damiend99b0522013-12-21 18:17:45 +00002605void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2606 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002607 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002608 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2609 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2610 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2611 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2612 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2613 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2614 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002615 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002616 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002617 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002618 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002619 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002620 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002621 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002622 // generator expression
2623 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2624 } else {
2625 // tuple with 2 items
2626 goto tuple_with_2_items;
2627 }
2628 } else {
2629 // tuple with 2 items
2630 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002631 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002632 }
2633 } else {
2634 // parenthesis around a single item, is just that item
2635 compile_node(comp, pns->nodes[0]);
2636 }
2637}
2638
Damiend99b0522013-12-21 18:17:45 +00002639void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2640 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002641 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002642 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002643 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2644 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2645 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2646 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2647 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002648 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002649 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002650 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002651 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002652 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002653 // list of many items
2654 compile_node(comp, pns2->nodes[0]);
2655 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002656 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002657 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002658 // list comprehension
2659 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2660 } else {
2661 // list with 2 items
2662 goto list_with_2_items;
2663 }
2664 } else {
2665 // list with 2 items
2666 list_with_2_items:
2667 compile_node(comp, pns2->nodes[0]);
2668 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002669 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002670 }
2671 } else {
2672 // list with 1 item
2673 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002674 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002675 }
2676}
2677
Damiend99b0522013-12-21 18:17:45 +00002678void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2679 mp_parse_node_t pn = pns->nodes[0];
2680 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002681 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002682 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002683 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2684 pns = (mp_parse_node_struct_t*)pn;
2685 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002686 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002687 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002688 compile_node(comp, pn);
2689 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002690 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2691 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2692 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2693 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002694 // dict/set with multiple elements
2695
2696 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002697 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002698 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2699
2700 // first element sets whether it's a dict or set
2701 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002702 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002703 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002704 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002705 compile_node(comp, pns->nodes[0]);
2706 EMIT(store_map);
2707 is_dict = true;
2708 } else {
2709 // a set
2710 compile_node(comp, pns->nodes[0]); // 1st value of set
2711 is_dict = false;
2712 }
2713
2714 // process rest of elements
2715 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002716 mp_parse_node_t pn = nodes[i];
2717 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002718 compile_node(comp, pn);
2719 if (is_dict) {
2720 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002721 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002722 return;
2723 }
2724 EMIT(store_map);
2725 } else {
2726 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002727 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002728 return;
2729 }
2730 }
2731 }
2732
2733 // if it's a set, build it
2734 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002735 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002736 }
Damiend99b0522013-12-21 18:17:45 +00002737 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002738 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002739 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002740 // a dictionary comprehension
2741 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2742 } else {
2743 // a set comprehension
2744 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2745 }
2746 } else {
2747 // shouldn't happen
2748 assert(0);
2749 }
2750 } else {
2751 // set with one element
2752 goto set_with_one_element;
2753 }
2754 } else {
2755 // set with one element
2756 set_with_one_element:
2757 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002758 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002759 }
2760}
2761
Damiend99b0522013-12-21 18:17:45 +00002762void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002763 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002764}
2765
Damiend99b0522013-12-21 18:17:45 +00002766void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002767 // object who's index we want is on top of stack
2768 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002769 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002770}
2771
Damiend99b0522013-12-21 18:17:45 +00002772void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002773 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002774 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002775}
2776
Damiend99b0522013-12-21 18:17:45 +00002777void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2778 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2779 mp_parse_node_t pn = pns->nodes[0];
2780 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002781 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002782 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2783 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002784 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2785 pns = (mp_parse_node_struct_t*)pn;
2786 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002787 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002788 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002789 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002790 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002791 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002792 } else {
2793 // [?::x]
2794 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002795 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002796 }
Damiend99b0522013-12-21 18:17:45 +00002797 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002798 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002799 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2800 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2801 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2802 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002803 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002804 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002805 } else {
2806 // [?:x:x]
2807 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002808 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002809 }
2810 } else {
2811 // [?:x]
2812 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002813 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002814 }
2815 } else {
2816 // [?:x]
2817 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002818 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002819 }
2820}
2821
Damiend99b0522013-12-21 18:17:45 +00002822void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002823 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002824 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2825 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002826}
2827
Damiend99b0522013-12-21 18:17:45 +00002828void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002829 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002830 compile_subscript_3_helper(comp, pns);
2831}
2832
Damiend99b0522013-12-21 18:17:45 +00002833void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002834 // if this is called then we are compiling a dict key:value pair
2835 compile_node(comp, pns->nodes[1]); // value
2836 compile_node(comp, pns->nodes[0]); // key
2837}
2838
Damiend99b0522013-12-21 18:17:45 +00002839void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002840 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002841 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002842 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002843}
2844
Damiend99b0522013-12-21 18:17:45 +00002845void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002846 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002847 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002848 return;
2849 }
Damiend99b0522013-12-21 18:17:45 +00002850 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002851 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002852 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002853 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2854 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002855 compile_node(comp, pns->nodes[0]);
2856 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002857 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002858 EMIT(yield_from);
2859 } else {
2860 compile_node(comp, pns->nodes[0]);
2861 EMIT(yield_value);
2862 }
2863}
2864
Damiend99b0522013-12-21 18:17:45 +00002865typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002866STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002867 NULL,
2868#define nc NULL
2869#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002870#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002871#include "grammar.h"
2872#undef nc
2873#undef c
2874#undef DEF_RULE
2875};
2876
Damien George6be0b0a2014-08-15 14:30:52 +01002877STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002878 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002879 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002880 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002881 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002882 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002883 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002884 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002885 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002886 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002887 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2888 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2889 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2890 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002891 case MP_PARSE_NODE_TOKEN:
2892 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002893 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002894 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002895 // do nothing
2896 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002897 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002898 }
2899 break;
Damien429d7192013-10-04 19:53:11 +01002900 default: assert(0);
2901 }
2902 } else {
Damiend99b0522013-12-21 18:17:45 +00002903 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002904 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002905 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002906 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 +01002907 } else {
Damien George5042bce2014-05-25 22:06:06 +01002908 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2909 if (f == NULL) {
2910 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2911#if MICROPY_DEBUG_PRINTERS
2912 mp_parse_node_print(pn, 0);
2913#endif
2914 compile_syntax_error(comp, pn, "internal compiler error");
2915 } else {
2916 f(comp, pns);
2917 }
Damien429d7192013-10-04 19:53:11 +01002918 }
2919 }
2920}
2921
Damien George2ac4af62014-08-15 16:45:41 +01002922STATIC 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 +01002923 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002924 qstr param_name = MP_QSTR_NULL;
2925 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002926 if (MP_PARSE_NODE_IS_ID(pn)) {
2927 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002928 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002929 // comes after a star, so counts as a keyword-only parameter
2930 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002931 } else {
Damien George2827d622014-04-27 15:50:52 +01002932 // comes before a star, so counts as a positional parameter
2933 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002934 }
Damienb14de212013-10-06 00:28:28 +01002935 } else {
Damiend99b0522013-12-21 18:17:45 +00002936 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2937 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2938 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2939 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002940 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002941 // comes after a star, so counts as a keyword-only parameter
2942 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002943 } else {
Damien George2827d622014-04-27 15:50:52 +01002944 // comes before a star, so counts as a positional parameter
2945 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002946 }
Damiend99b0522013-12-21 18:17:45 +00002947 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002948 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002949 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002950 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002951 // bare star
2952 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002953 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002954 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002955 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002956 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002957 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01002958 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002959 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002960 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002961 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2962 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002963 } else {
2964 // shouldn't happen
2965 assert(0);
2966 }
Damiend99b0522013-12-21 18:17:45 +00002967 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2968 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002969 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002970 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002971 } else {
Damienb14de212013-10-06 00:28:28 +01002972 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002973 assert(0);
2974 }
Damien429d7192013-10-04 19:53:11 +01002975 }
2976
Damien George2827d622014-04-27 15:50:52 +01002977 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002978 bool added;
2979 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2980 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002981 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002982 return;
2983 }
Damien429d7192013-10-04 19:53:11 +01002984 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002985 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002986 }
2987}
2988
Damien George2827d622014-04-27 15:50:52 +01002989STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002990 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002991}
2992
Damien George2827d622014-04-27 15:50:52 +01002993STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002994 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2995}
2996
2997STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2998 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2999 // no annotation
3000 return;
3001 }
3002
3003 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3004 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3005 // named parameter with possible annotation
3006 // fallthrough
3007 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3008 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3009 // named star with possible annotation
3010 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3011 // fallthrough
3012 } else {
3013 // no annotation
3014 return;
3015 }
3016 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3017 // double star with possible annotation
3018 // fallthrough
3019 } else {
3020 // no annotation
3021 return;
3022 }
3023
3024 mp_parse_node_t pn_annotation = pns->nodes[1];
3025
3026 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3027 #if MICROPY_EMIT_NATIVE
3028 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3029 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3030 assert(id_info != NULL);
3031
3032 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3033 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3034 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3035 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3036 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003037 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003038 }
3039 }
3040 #endif // MICROPY_EMIT_NATIVE
3041 }
Damien429d7192013-10-04 19:53:11 +01003042}
3043
Damien George6be0b0a2014-08-15 14:30:52 +01003044STATIC 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 +01003045 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003046 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003047 // no more nested if/for; compile inner expression
3048 compile_node(comp, pn_inner_expr);
3049 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003050 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003051 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003052 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003053 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003054 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003055 } else {
3056 EMIT(yield_value);
3057 EMIT(pop_top);
3058 }
Damiend99b0522013-12-21 18:17:45 +00003059 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003060 // if condition
Damiend99b0522013-12-21 18:17:45 +00003061 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003062 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3063 pn_iter = pns_comp_if->nodes[1];
3064 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003065 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003066 // for loop
Damiend99b0522013-12-21 18:17:45 +00003067 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003068 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003069 uint l_end2 = comp_next_label(comp);
3070 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003071 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003072 EMIT_ARG(label_assign, l_top2);
3073 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003074 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3075 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 +00003076 EMIT_ARG(jump, l_top2);
3077 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003078 EMIT(for_iter_end);
3079 } else {
3080 // shouldn't happen
3081 assert(0);
3082 }
3083}
3084
Damien George1463c1f2014-04-25 23:52:57 +01003085STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3086#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003087 // see http://www.python.org/dev/peps/pep-0257/
3088
3089 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003090 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003091 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003092 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003093 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003094 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3095 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003096 for (int i = 0; i < num_nodes; i++) {
3097 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003098 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 +00003099 // not a newline, so this is the first statement; finish search
3100 break;
3101 }
3102 }
3103 // 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 +00003104 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003105 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003106 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003107 } else {
3108 return;
3109 }
3110
3111 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003112 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3113 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003114 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3115 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3116 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3117 // compile the doc string
3118 compile_node(comp, pns->nodes[0]);
3119 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003120 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003121 }
3122 }
Damien George1463c1f2014-04-25 23:52:57 +01003123#endif
Damien429d7192013-10-04 19:53:11 +01003124}
3125
Damien George1463c1f2014-04-25 23:52:57 +01003126STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003127 comp->pass = pass;
3128 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003129 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003130 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003131
Damien George36db6bc2014-05-07 17:24:22 +01003132 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003133 // reset maximum stack sizes in scope
3134 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003135 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003136 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003137 }
3138
Damien5ac1b2e2013-10-18 19:58:12 +01003139#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003140 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003141 scope_print_info(scope);
3142 }
Damien5ac1b2e2013-10-18 19:58:12 +01003143#endif
Damien429d7192013-10-04 19:53:11 +01003144
3145 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003146 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3147 assert(scope->kind == SCOPE_MODULE);
3148 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3149 compile_node(comp, pns->nodes[0]); // compile the expression
3150 EMIT(return_value);
3151 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003152 if (!comp->is_repl) {
3153 check_for_doc_string(comp, scope->pn);
3154 }
Damien429d7192013-10-04 19:53:11 +01003155 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003156 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003157 EMIT(return_value);
3158 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003159 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3160 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3161 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003162
3163 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003164 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003165 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003166 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003167 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003168 } else {
3169 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003170
Damien George2ac4af62014-08-15 16:45:41 +01003171 // argument annotations
3172 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3173
3174 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003175 mp_parse_node_t pn_annotation = pns->nodes[2];
3176 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3177 #if MICROPY_EMIT_NATIVE
3178 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3179 // nodes[2] can be null or a test-expr
3180 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3181 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3182 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3183 } else {
3184 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3185 }
Damien George2ac4af62014-08-15 16:45:41 +01003186 }
Damien Georgea5190a72014-08-15 22:39:08 +01003187 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003188 }
Damien George2ac4af62014-08-15 16:45:41 +01003189 }
Damien429d7192013-10-04 19:53:11 +01003190
3191 compile_node(comp, pns->nodes[3]); // 3 is function body
3192 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003193 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003194 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003195 EMIT(return_value);
3196 }
3197 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003198 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3199 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3200 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003201
3202 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003203 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003204 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003205 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003206 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3207 }
3208
3209 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003210
3211 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3212 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3213 EMIT(pop_top);
3214 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3215 }
Damien429d7192013-10-04 19:53:11 +01003216 EMIT(return_value);
3217 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3218 // a bit of a hack at the moment
3219
Damiend99b0522013-12-21 18:17:45 +00003220 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3221 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3222 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3223 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3224 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003225
Damien George708c0732014-04-27 19:23:46 +01003226 // We need a unique name for the comprehension argument (the iterator).
3227 // CPython uses .0, but we should be able to use anything that won't
3228 // clash with a user defined variable. Best to use an existing qstr,
3229 // so we use the blank qstr.
3230#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003231 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003232#else
3233 qstr qstr_arg = MP_QSTR_;
3234#endif
Damien George36db6bc2014-05-07 17:24:22 +01003235 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003236 bool added;
3237 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3238 assert(added);
3239 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003240 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003241 }
3242
3243 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003244 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003245 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003246 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003247 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003248 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003249 }
3250
Damien George6f355fd2014-04-10 14:11:31 +01003251 uint l_end = comp_next_label(comp);
3252 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003253 EMIT_ARG(load_id, qstr_arg);
3254 EMIT_ARG(label_assign, l_top);
3255 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003256 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3257 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003258 EMIT_ARG(jump, l_top);
3259 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003260 EMIT(for_iter_end);
3261
3262 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003263 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003264 }
3265 EMIT(return_value);
3266 } else {
3267 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003268 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3269 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3270 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003271
Damien George36db6bc2014-05-07 17:24:22 +01003272 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003273 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003274 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003275 assert(added);
3276 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003277 }
3278
Damien Georgeb9791222014-01-23 00:34:21 +00003279 EMIT_ARG(load_id, MP_QSTR___name__);
3280 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003281 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 +00003282 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003283
3284 check_for_doc_string(comp, pns->nodes[2]);
3285 compile_node(comp, pns->nodes[2]); // 2 is class body
3286
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003287 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003288 assert(id != NULL);
3289 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003290 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003291 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003292#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003293 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003294#else
Damien George2bf7c092014-04-09 15:26:46 +01003295 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003296#endif
Damien429d7192013-10-04 19:53:11 +01003297 }
3298 EMIT(return_value);
3299 }
3300
Damien415eb6f2013-10-05 12:19:06 +01003301 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003302
3303 // make sure we match all the exception levels
3304 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003305}
3306
Damien George094d4502014-04-02 17:31:27 +01003307#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003308// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003309STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003310 comp->pass = pass;
3311 comp->scope_cur = scope;
3312 comp->next_label = 1;
3313
3314 if (scope->kind != SCOPE_FUNCTION) {
3315 printf("Error: inline assembler must be a function\n");
3316 return;
3317 }
3318
Damien George36db6bc2014-05-07 17:24:22 +01003319 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003320 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003321 }
3322
Damien826005c2013-10-05 23:17:28 +01003323 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003324 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3325 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3326 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003327
Damiend99b0522013-12-21 18:17:45 +00003328 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003329
Damiena2f2f7d2013-10-06 00:14:13 +01003330 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003331 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003332 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003333 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003334 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003335 }
3336
Damiend99b0522013-12-21 18:17:45 +00003337 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003338
Damiend99b0522013-12-21 18:17:45 +00003339 mp_parse_node_t pn_body = pns->nodes[3]; // body
3340 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003341 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3342
Damien Georgecbd2f742014-01-19 11:48:48 +00003343 /*
Damien George36db6bc2014-05-07 17:24:22 +01003344 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003345 //printf("----\n");
3346 scope_print_info(scope);
3347 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003348 */
Damien826005c2013-10-05 23:17:28 +01003349
3350 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003351 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3352 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003353 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3354 // no instructions
3355 continue;
3356 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3357 // an instruction; fall through
3358 } else {
3359 // not an instruction; error
3360 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3361 return;
3362 }
Damiend99b0522013-12-21 18:17:45 +00003363 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3364 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3365 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3366 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3367 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3368 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3369 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3370 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3371 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3372 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003373 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3374
3375 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003376 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003377 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003378 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003379 return;
3380 }
Damien George6f355fd2014-04-10 14:11:31 +01003381 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003382 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003383 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003384 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003385 } else if (op == MP_QSTR_align) {
3386 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3387 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3388 return;
3389 }
Damien George36db6bc2014-05-07 17:24:22 +01003390 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003391 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3392 }
3393 } else if (op == MP_QSTR_data) {
3394 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3395 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3396 return;
3397 }
Damien George36db6bc2014-05-07 17:24:22 +01003398 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003399 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003400 for (uint i = 1; i < n_args; i++) {
3401 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3402 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3403 return;
3404 }
3405 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3406 }
3407 }
Damien826005c2013-10-05 23:17:28 +01003408 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003409 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003410 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003411 }
3412 }
3413 }
3414
Damien George36db6bc2014-05-07 17:24:22 +01003415 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003416 bool success = EMIT_INLINE_ASM(end_pass);
3417 if (!success) {
3418 comp->had_error = true;
3419 }
Damienb05d7072013-10-05 13:37:10 +01003420 }
Damien429d7192013-10-04 19:53:11 +01003421}
Damien George094d4502014-04-02 17:31:27 +01003422#endif
Damien429d7192013-10-04 19:53:11 +01003423
Damien George1463c1f2014-04-25 23:52:57 +01003424STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003425#if !MICROPY_EMIT_CPYTHON
3426 // in Micro Python we put the *x parameter after all other parameters (except **y)
3427 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3428 id_info_t *id_param = NULL;
3429 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3430 id_info_t *id = &scope->id_info[i];
3431 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3432 if (id_param != NULL) {
3433 // swap star param with last param
3434 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3435 }
3436 break;
3437 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3438 id_param = id;
3439 }
3440 }
3441 }
3442#endif
3443
Damien429d7192013-10-04 19:53:11 +01003444 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003445 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003446 scope->num_locals = 0;
3447 for (int i = 0; i < scope->id_info_len; i++) {
3448 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003449 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003450 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3451 continue;
3452 }
3453 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3454 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3455 }
Damien George2827d622014-04-27 15:50:52 +01003456 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003457 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003458 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003459 }
3460 }
3461
3462 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003463#if MICROPY_EMIT_CPYTHON
3464 int num_cell = 0;
3465#endif
Damien9ecbcff2013-12-11 00:41:43 +00003466 for (int i = 0; i < scope->id_info_len; i++) {
3467 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003468#if MICROPY_EMIT_CPYTHON
3469 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003470 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003471 id->local_num = num_cell;
3472 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003473 }
Damien George6baf76e2013-12-30 22:32:17 +00003474#else
3475 // in Micro Python the cells come right after the fast locals
3476 // parameters are not counted here, since they remain at the start
3477 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003478 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003479 id->local_num = scope->num_locals;
3480 scope->num_locals += 1;
3481 }
3482#endif
Damien9ecbcff2013-12-11 00:41:43 +00003483 }
Damien9ecbcff2013-12-11 00:41:43 +00003484
3485 // compute the index of free vars (freevars[idx] in CPython)
3486 // make sure they are in the order of the parent scope
3487 if (scope->parent != NULL) {
3488 int num_free = 0;
3489 for (int i = 0; i < scope->parent->id_info_len; i++) {
3490 id_info_t *id = &scope->parent->id_info[i];
3491 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3492 for (int j = 0; j < scope->id_info_len; j++) {
3493 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003494 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003495 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003496#if MICROPY_EMIT_CPYTHON
3497 // in CPython the frees are numbered after the cells
3498 id2->local_num = num_cell + num_free;
3499#else
3500 // in Micro Python the frees come first, before the params
3501 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003502#endif
3503 num_free += 1;
3504 }
3505 }
3506 }
Damien429d7192013-10-04 19:53:11 +01003507 }
Damien George6baf76e2013-12-30 22:32:17 +00003508#if !MICROPY_EMIT_CPYTHON
3509 // in Micro Python shift all other locals after the free locals
3510 if (num_free > 0) {
3511 for (int i = 0; i < scope->id_info_len; i++) {
3512 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003513 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003514 id->local_num += num_free;
3515 }
3516 }
Damien George2827d622014-04-27 15:50:52 +01003517 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 +00003518 scope->num_locals += num_free;
3519 }
3520#endif
Damien429d7192013-10-04 19:53:11 +01003521 }
3522
Damien George8725f8f2014-02-15 19:33:11 +00003523 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003524
3525#if MICROPY_EMIT_CPYTHON
3526 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003527 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) {
3528 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003529 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003530 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003531 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 +00003532 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003533 }
3534 }
Damien George882b3632014-04-02 15:56:31 +01003535#endif
3536
Damien429d7192013-10-04 19:53:11 +01003537 int num_free = 0;
3538 for (int i = 0; i < scope->id_info_len; i++) {
3539 id_info_t *id = &scope->id_info[i];
3540 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3541 num_free += 1;
3542 }
3543 }
3544 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003545 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003546 }
3547}
3548
Damien George65cad122014-04-06 11:48:15 +01003549mp_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 +01003550 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003551 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003552 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003553
Damien826005c2013-10-05 23:17:28 +01003554 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003555 mp_map_t consts;
3556 mp_map_init(&consts, 0);
3557 pn = fold_constants(comp, pn, &consts);
3558 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003559
3560 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003561 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003562
Damien826005c2013-10-05 23:17:28 +01003563 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003564 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003565 comp->emit_method_table = &emit_pass1_method_table;
3566 comp->emit_inline_asm = NULL;
3567 comp->emit_inline_asm_method_table = NULL;
3568 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003569 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003570 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003571#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003572 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003573 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003574#endif
Damien826005c2013-10-05 23:17:28 +01003575 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003576 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003577 }
3578
3579 // update maximim number of labels needed
3580 if (comp->next_label > max_num_labels) {
3581 max_num_labels = comp->next_label;
3582 }
Damien429d7192013-10-04 19:53:11 +01003583 }
3584
Damien826005c2013-10-05 23:17:28 +01003585 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003586 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003587 compile_scope_compute_things(comp, s);
3588 }
3589
Damien826005c2013-10-05 23:17:28 +01003590 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003591 emit_pass1_free(comp->emit);
3592
Damien826005c2013-10-05 23:17:28 +01003593 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003594#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003595 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003596#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003597 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003598#endif
Damien3ef4abb2013-10-12 16:53:13 +01003599#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003600 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003601#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003602#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003603 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003604 if (false) {
3605 // dummy
3606
Damien3ef4abb2013-10-12 16:53:13 +01003607#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003608 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003609 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003610 if (emit_inline_thumb == NULL) {
3611 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3612 }
3613 comp->emit = NULL;
3614 comp->emit_method_table = NULL;
3615 comp->emit_inline_asm = emit_inline_thumb;
3616 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003617 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea26dc502014-04-12 17:54:52 +01003618 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003619 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003620 }
Damienc025ebb2013-10-12 14:30:21 +01003621#endif
3622
Damien826005c2013-10-05 23:17:28 +01003623 } else {
Damienc025ebb2013-10-12 14:30:21 +01003624
3625 // choose the emit type
3626
Damien3ef4abb2013-10-12 16:53:13 +01003627#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003628 comp->emit = emit_cpython_new(max_num_labels);
3629 comp->emit_method_table = &emit_cpython_method_table;
3630#else
Damien826005c2013-10-05 23:17:28 +01003631 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003632
3633#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003634 case MP_EMIT_OPT_NATIVE_PYTHON:
3635 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003636#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003637 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003638 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003639 }
Damien13ed3a62013-10-08 09:05:10 +01003640 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003641#elif MICROPY_EMIT_X86
3642 if (emit_native == NULL) {
3643 emit_native = emit_native_x86_new(max_num_labels);
3644 }
3645 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003646#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003647 if (emit_native == NULL) {
3648 emit_native = emit_native_thumb_new(max_num_labels);
3649 }
3650 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003651#elif MICROPY_EMIT_ARM
3652 if (emit_native == NULL) {
3653 emit_native = emit_native_arm_new(max_num_labels);
3654 }
3655 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003656#endif
3657 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003658 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien George36db6bc2014-05-07 17:24:22 +01003659
3660 // native emitters need an extra pass to compute stack size
3661 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3662
Damien7af3d192013-10-07 00:02:49 +01003663 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003664#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003665
Damien826005c2013-10-05 23:17:28 +01003666 default:
3667 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003668 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003669 }
3670 comp->emit = emit_bc;
3671 comp->emit_method_table = &emit_bc_method_table;
3672 break;
3673 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003674#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003675
Damien George36db6bc2014-05-07 17:24:22 +01003676 // second last pass: compute code size
Damien Georgea26dc502014-04-12 17:54:52 +01003677 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003678 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3679 }
3680
3681 // final pass: emit code
3682 if (!comp->had_error) {
3683 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003684 }
Damien6cdd3af2013-10-05 18:08:26 +01003685 }
Damien429d7192013-10-04 19:53:11 +01003686 }
3687
Damien George41d02b62014-01-24 22:42:28 +00003688 // free the emitters
3689#if !MICROPY_EMIT_CPYTHON
3690 if (emit_bc != NULL) {
3691 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003692 }
Damien George41d02b62014-01-24 22:42:28 +00003693#if MICROPY_EMIT_NATIVE
3694 if (emit_native != NULL) {
3695#if MICROPY_EMIT_X64
3696 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003697#elif MICROPY_EMIT_X86
3698 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003699#elif MICROPY_EMIT_THUMB
3700 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003701#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003702 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003703#endif
3704 }
3705#endif
3706#if MICROPY_EMIT_INLINE_THUMB
3707 if (emit_inline_thumb != NULL) {
3708 emit_inline_thumb_free(emit_inline_thumb);
3709 }
3710#endif
3711#endif // !MICROPY_EMIT_CPYTHON
3712
Damien George52b5d762014-09-23 15:31:56 +00003713 // free the parse tree
3714 mp_parse_node_free(pn);
3715
Damien George41d02b62014-01-24 22:42:28 +00003716 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003717 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003718 for (scope_t *s = module_scope; s;) {
3719 scope_t *next = s->next;
3720 scope_free(s);
3721 s = next;
3722 }
Damien5ac1b2e2013-10-18 19:58:12 +01003723
Damien George41d02b62014-01-24 22:42:28 +00003724 // free the compiler
3725 bool had_error = comp->had_error;
3726 m_del_obj(compiler_t, comp);
3727
Damien George1fb03172014-01-03 14:22:03 +00003728 if (had_error) {
3729 // TODO return a proper error message
3730 return mp_const_none;
3731 } else {
3732#if MICROPY_EMIT_CPYTHON
3733 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003734 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003735 return mp_const_true;
3736#else
3737 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003738 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003739#endif
3740 }
Damien429d7192013-10-04 19:53:11 +01003741}