blob: 6ec57adcaed31b89921550d7b2b82c192de63e84 [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
Damience89a212013-10-15 22:25:17 +010051#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
52
Damien429d7192013-10-04 19:53:11 +010053typedef enum {
54 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000055#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010056#include "grammar.h"
57#undef DEF_RULE
58 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010059 PN_string, // special node for non-interned string
Damien429d7192013-10-04 19:53:11 +010060} pn_kind_t;
61
Damien Georgeb9791222014-01-23 00:34:21 +000062#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
63#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
64#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
65#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 +010066
67typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000068 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010069 uint8_t is_repl;
70 uint8_t pass; // holds enum type pass_kind_t
71 uint8_t had_error; // try to keep compiler clean from nlr
72 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010073
Damien George6f355fd2014-04-10 14:11:31 +010074 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010075
Damien George25c84642014-05-30 15:20:41 +010076 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
77 uint16_t continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000078 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010079 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010080
Damien George8b19db02014-04-11 23:25:34 +010081 uint8_t have_star;
Damien George69b89d22014-04-11 13:38:30 +000082 uint16_t num_dict_params;
83 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010084
85 scope_t *scope_head;
86 scope_t *scope_cur;
87
Damien6cdd3af2013-10-05 18:08:26 +010088 emit_t *emit; // current emitter
89 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010090
91 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
92 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 +010093} compiler_t;
94
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010095STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000096 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010097 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +010098 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 +010099 } else {
100 printf(" File \"%s\"\n", qstr_str(comp->source_file));
101 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000102 printf("SyntaxError: %s\n", msg);
103 comp->had_error = true;
104}
105
Damien George57e99eb2014-04-10 22:42:11 +0100106STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300107 #if MICROPY_PY_UCTYPES
108 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
109 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100110 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100111 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100112};
113
114STATIC const mp_map_t mp_constants_map = {
115 .all_keys_are_qstrs = 1,
116 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200117 .used = MP_ARRAY_SIZE(mp_constants_table),
118 .alloc = MP_ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +0100119 .table = (mp_map_elem_t*)mp_constants_table,
120};
121
Damien Georgeffae48d2014-05-08 15:58:39 +0000122// this function is essentially a simple preprocessor
123STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
124 if (0) {
125 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100126#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000127 } else if (MP_PARSE_NODE_IS_ID(pn)) {
128 // lookup identifier in table of dynamic constants
129 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
130 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
131 if (elem != NULL) {
132 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
133 }
134#endif
135 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000136 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100137
Damien Georgeffae48d2014-05-08 15:58:39 +0000138 // fold some parse nodes before folding their arguments
139 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100140#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000141 case PN_expr_stmt:
142 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
143 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
144 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
145 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
146 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
147 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
148 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
149 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
150 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
151 ) {
152 // code to assign dynamic constants: id = const(value)
153
154 // get the id
155 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
156
157 // get the value
158 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
159 pn_value = fold_constants(comp, pn_value, consts);
160 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
161 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
162 break;
163 }
Damien George40f3c022014-07-03 13:25:24 +0100164 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000165
166 // store the value in the table of dynamic constants
167 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
168 if (elem->value != MP_OBJ_NULL) {
169 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
170 break;
171 }
172 elem->value = MP_OBJ_NEW_SMALL_INT(value);
173
174 // replace const(value) with value
175 pns1->nodes[0] = pn_value;
176
177 // finished folding this assignment
178 return pn;
179 }
180 }
181 }
182 break;
183#endif
Damien George5042bce2014-05-25 22:06:06 +0100184 case PN_string:
185 return pn;
Damien429d7192013-10-04 19:53:11 +0100186 }
187
Damien Georgeffae48d2014-05-08 15:58:39 +0000188 // fold arguments
189 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
190 for (int i = 0; i < n; i++) {
191 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
192 }
193
194 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000195 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100196 case PN_atom_paren:
197 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
198 // (int)
199 pn = pns->nodes[0];
200 }
201 break;
202
203 case PN_expr:
204 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
205 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100206 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
207 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100208 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
209 }
210 break;
211
212 case PN_and_expr:
213 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
214 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100215 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
216 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100217 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
218 }
219 break;
220
Damien429d7192013-10-04 19:53:11 +0100221 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000222 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 +0100223 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
224 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000225 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100226 // int << int
227 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
228 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
229 }
Damiend99b0522013-12-21 18:17:45 +0000230 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100231 // int >> int
Damiend99b0522013-12-21 18:17:45 +0000232 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100233 } else {
234 // shouldn't happen
235 assert(0);
236 }
237 }
238 break;
239
240 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100241 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000242 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 +0100243 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
244 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000245 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100246 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000247 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000248 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100249 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000250 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100251 } else {
252 // shouldn't happen
253 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100254 }
Damien Georged1e355e2014-05-28 14:51:12 +0100255 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100256 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000257 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100258 }
259 }
260 break;
261
262 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000263 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 +0100264 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
265 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000266 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100267 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000268 if (!mp_small_int_mul_overflow(arg0, arg1)) {
269 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100270 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000271 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
272 }
273 }
Damiend99b0522013-12-21 18:17:45 +0000274 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100275 // int / int
276 // pass
Damiend99b0522013-12-21 18:17:45 +0000277 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100278 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000279 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000280 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300281 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100282 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000283 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300284 }
Damien429d7192013-10-04 19:53:11 +0100285 } else {
286 // shouldn't happen
287 assert(0);
288 }
289 }
290 break;
291
292 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000293 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100294 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000295 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100296 // +int
Damiend99b0522013-12-21 18:17:45 +0000297 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
298 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100299 // -int
Damiend99b0522013-12-21 18:17:45 +0000300 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
301 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100302 // ~int
Damiend99b0522013-12-21 18:17:45 +0000303 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100304 } else {
305 // shouldn't happen
306 assert(0);
307 }
308 }
309 break;
310
311 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100312 if (0) {
313#if MICROPY_EMIT_CPYTHON
314 } 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 +0100315 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100316 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000317 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
318 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200319 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100320 if (power >= 0) {
321 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200322 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100323 for (; power > 0; power--) {
324 ans *= base;
325 }
Damiend99b0522013-12-21 18:17:45 +0000326 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100327 }
328 }
Damien George57e99eb2014-04-10 22:42:11 +0100329#endif
330 } 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])) {
331 // id.id
332 // look it up in constant table, see if it can be replaced with an integer
333 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
334 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
335 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
336 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
337 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
338 if (elem != NULL) {
339 mp_obj_t dest[2];
340 mp_load_method_maybe(elem->value, q_attr, dest);
341 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100342 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georged1e355e2014-05-28 14:51:12 +0100343 if (MP_SMALL_INT_FITS(val)) {
Damien George57e99eb2014-04-10 22:42:11 +0100344 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
345 }
346 }
347 }
Damien429d7192013-10-04 19:53:11 +0100348 }
349 break;
350 }
351 }
352
353 return pn;
354}
355
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200356STATIC 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 +0100357STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000358STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100359
Damien George6f355fd2014-04-10 14:11:31 +0100360STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100361 return comp->next_label++;
362}
363
Damien George8dcc0c72014-03-27 10:55:21 +0000364STATIC void compile_increase_except_level(compiler_t *comp) {
365 comp->cur_except_level += 1;
366 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
367 comp->scope_cur->exc_stack_size = comp->cur_except_level;
368 }
369}
370
371STATIC void compile_decrease_except_level(compiler_t *comp) {
372 assert(comp->cur_except_level > 0);
373 comp->cur_except_level -= 1;
374}
375
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200376STATIC 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 +0100377 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100378 scope->parent = comp->scope_cur;
379 scope->next = NULL;
380 if (comp->scope_head == NULL) {
381 comp->scope_head = scope;
382 } else {
383 scope_t *s = comp->scope_head;
384 while (s->next != NULL) {
385 s = s->next;
386 }
387 s->next = scope;
388 }
389 return scope;
390}
391
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200392STATIC 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 +0000393 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
394 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
395 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100396 for (int i = 0; i < num_nodes; i++) {
397 f(comp, pns->nodes[i]);
398 }
Damiend99b0522013-12-21 18:17:45 +0000399 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100400 f(comp, pn);
401 }
402}
403
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200404STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000405 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100406 *nodes = NULL;
407 return 0;
Damiend99b0522013-12-21 18:17:45 +0000408 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100409 *nodes = pn;
410 return 1;
411 } else {
Damiend99b0522013-12-21 18:17:45 +0000412 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
413 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100414 *nodes = pn;
415 return 1;
416 } else {
417 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000418 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100419 }
420 }
421}
422
Damiend99b0522013-12-21 18:17:45 +0000423void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100424}
425
Damiend99b0522013-12-21 18:17:45 +0000426void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
427 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100428 for (int i = 0; i < num_nodes; i++) {
429 compile_node(comp, pns->nodes[i]);
430 }
431}
432
Damien3ef4abb2013-10-12 16:53:13 +0100433#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200434STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100435 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
436 return true;
437 }
Damiend99b0522013-12-21 18:17:45 +0000438 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100439 return false;
440 }
Damiend99b0522013-12-21 18:17:45 +0000441 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100442 return false;
443 }
444 return true;
445}
446
Damien George5042bce2014-05-25 22:06:06 +0100447STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000448 bool has_single_quote = false;
449 bool has_double_quote = false;
450 for (int i = 0; i < len; i++) {
451 if (str[i] == '\'') {
452 has_single_quote = true;
453 } else if (str[i] == '"') {
454 has_double_quote = true;
455 }
456 }
457 if (bytes) {
458 vstr_printf(vstr, "b");
459 }
460 bool quote_single = false;
461 if (has_single_quote && !has_double_quote) {
462 vstr_printf(vstr, "\"");
463 } else {
464 quote_single = true;
465 vstr_printf(vstr, "'");
466 }
467 for (int i = 0; i < len; i++) {
468 if (str[i] == '\n') {
469 vstr_printf(vstr, "\\n");
470 } else if (str[i] == '\\') {
471 vstr_printf(vstr, "\\\\");
472 } else if (str[i] == '\'' && quote_single) {
473 vstr_printf(vstr, "\\'");
474 } else {
475 vstr_printf(vstr, "%c", str[i]);
476 }
477 }
478 if (has_single_quote && !has_double_quote) {
479 vstr_printf(vstr, "\"");
480 } else {
481 vstr_printf(vstr, "'");
482 }
483}
484
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200485STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George5042bce2014-05-25 22:06:06 +0100486 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
487 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George40f3c022014-07-03 13:25:24 +0100488 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 +0100489 return;
490 }
491
Damiend99b0522013-12-21 18:17:45 +0000492 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200493 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
494 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
495 return;
496 }
497
Damiend99b0522013-12-21 18:17:45 +0000498 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
499 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
500 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000501 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
502 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100503 case MP_PARSE_NODE_STRING:
504 case MP_PARSE_NODE_BYTES: {
505 uint len;
506 const byte *str = qstr_data(arg, &len);
507 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
508 break;
509 }
Damiend99b0522013-12-21 18:17:45 +0000510 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100511 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000512 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
513 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
514 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100515 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100516 }
517 break;
518 default: assert(0);
519 }
520}
521
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200522STATIC 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 +0100523 int n = 0;
524 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000525 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100526 }
527 int total = n;
528 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000529 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100530 total += 1;
Damien3a205172013-10-12 15:01:56 +0100531 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100532 is_const = false;
533 }
534 }
535 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100536 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100537 is_const = false;
538 break;
539 }
540 }
541 if (total > 0 && is_const) {
542 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000543 vstr_t *vstr = vstr_new();
544 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000545 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000546 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100547 need_comma = true;
548 }
549 for (int i = 0; i < n; i++) {
550 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000551 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100552 }
Damien02f89412013-12-12 15:13:36 +0000553 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100554 need_comma = true;
555 }
556 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000557 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100558 } else {
Damien02f89412013-12-12 15:13:36 +0000559 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100560 }
Damien Georgeb9791222014-01-23 00:34:21 +0000561 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000562 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100563 } else {
Damiend99b0522013-12-21 18:17:45 +0000564 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100565 compile_node(comp, pn);
566 }
567 for (int i = 0; i < n; i++) {
568 compile_node(comp, pns_list->nodes[i]);
569 }
Damien Georgeb9791222014-01-23 00:34:21 +0000570 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100571 }
572}
Damien3a205172013-10-12 15:01:56 +0100573#endif
574
575// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100576STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100577#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100578 cpython_c_tuple(comp, pn, pns_list);
579#else
580 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000581 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100582 compile_node(comp, pn);
583 total += 1;
584 }
585 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000586 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100587 for (int i = 0; i < n; i++) {
588 compile_node(comp, pns_list->nodes[i]);
589 }
590 total += n;
591 }
Damien Georgeb9791222014-01-23 00:34:21 +0000592 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100593#endif
594}
Damien429d7192013-10-04 19:53:11 +0100595
Damiend99b0522013-12-21 18:17:45 +0000596void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100597 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000598 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100599}
600
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200601STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000602 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200603 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100604}
605
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200606STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200607 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 +0100608}
609
Damien3ef4abb2013-10-12 16:53:13 +0100610#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100611// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200612STATIC 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 +0100613 if (node_is_const_false(pn)) {
614 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000615 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100616 }
617 return;
618 } else if (node_is_const_true(pn)) {
619 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000620 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100621 }
622 return;
Damiend99b0522013-12-21 18:17:45 +0000623 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
624 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
625 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
626 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100627 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100628 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100629 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100630 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100631 }
Damien3a205172013-10-12 15:01:56 +0100632 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000633 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100634 } else {
635 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100636 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100637 }
638 }
639 return;
Damiend99b0522013-12-21 18:17:45 +0000640 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100641 if (jump_if == false) {
642 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100643 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100644 }
645 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100646 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100647 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100648 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100649 }
Damien3a205172013-10-12 15:01:56 +0100650 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000651 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100652 }
653 return;
Damiend99b0522013-12-21 18:17:45 +0000654 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100655 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100656 return;
657 }
658 }
659
660 // nothing special, fall back to default compiling for node and jump
661 compile_node(comp, pn);
662 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000663 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100664 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000665 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100666 }
667}
Damien3a205172013-10-12 15:01:56 +0100668#endif
Damien429d7192013-10-04 19:53:11 +0100669
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200670STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100671#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100672 cpython_c_if_cond(comp, pn, jump_if, label, false);
673#else
674 if (node_is_const_false(pn)) {
675 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000676 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100677 }
678 return;
679 } else if (node_is_const_true(pn)) {
680 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000681 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100682 }
683 return;
Damiend99b0522013-12-21 18:17:45 +0000684 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
685 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
686 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
687 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100688 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100689 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100690 for (int i = 0; i < n - 1; i++) {
691 c_if_cond(comp, pns->nodes[i], true, label2);
692 }
693 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000694 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100695 } else {
696 for (int i = 0; i < n; i++) {
697 c_if_cond(comp, pns->nodes[i], true, label);
698 }
699 }
700 return;
Damiend99b0522013-12-21 18:17:45 +0000701 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100702 if (jump_if == false) {
703 for (int i = 0; i < n; i++) {
704 c_if_cond(comp, pns->nodes[i], false, label);
705 }
706 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100707 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100708 for (int i = 0; i < n - 1; i++) {
709 c_if_cond(comp, pns->nodes[i], false, label2);
710 }
711 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000712 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100713 }
714 return;
Damiend99b0522013-12-21 18:17:45 +0000715 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100716 c_if_cond(comp, pns->nodes[0], !jump_if, label);
717 return;
718 }
719 }
720
721 // nothing special, fall back to default compiling for node and jump
722 compile_node(comp, pn);
723 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000724 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100725 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000726 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100727 }
728#endif
Damien429d7192013-10-04 19:53:11 +0100729}
730
731typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100732STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100733
Damien George6be0b0a2014-08-15 14:30:52 +0100734STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100735 if (assign_kind != ASSIGN_AUG_STORE) {
736 compile_node(comp, pns->nodes[0]);
737 }
738
Damiend99b0522013-12-21 18:17:45 +0000739 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
740 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
741 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
742 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100743 if (assign_kind != ASSIGN_AUG_STORE) {
744 for (int i = 0; i < n - 1; i++) {
745 compile_node(comp, pns1->nodes[i]);
746 }
747 }
Damiend99b0522013-12-21 18:17:45 +0000748 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
749 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100750 }
Damiend99b0522013-12-21 18:17:45 +0000751 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100752 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000753 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100754 if (assign_kind == ASSIGN_AUG_STORE) {
755 EMIT(rot_three);
756 EMIT(store_subscr);
757 } else {
758 compile_node(comp, pns1->nodes[0]);
759 if (assign_kind == ASSIGN_AUG_LOAD) {
760 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100761 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100762 } else {
763 EMIT(store_subscr);
764 }
765 }
Damiend99b0522013-12-21 18:17:45 +0000766 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
767 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100768 if (assign_kind == ASSIGN_AUG_LOAD) {
769 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000770 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100771 } else {
772 if (assign_kind == ASSIGN_AUG_STORE) {
773 EMIT(rot_two);
774 }
Damien Georgeb9791222014-01-23 00:34:21 +0000775 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100776 }
777 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100778 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100779 }
780 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100781 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100782 }
783
Damiend99b0522013-12-21 18:17:45 +0000784 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100785 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100786 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100787
788 return;
789
790cannot_assign:
791 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100792}
793
Damien George0288cf02014-04-11 11:53:00 +0000794// 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 +0100795STATIC 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 +0000796 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
797
798 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100799 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000800 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
801 EMIT_ARG(unpack_ex, 0, num_tail);
802 have_star_index = 0;
803 }
804 for (int i = 0; i < num_tail; i++) {
805 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100806 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000807 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
808 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100809 } else {
Damien George9d181f62014-04-27 16:55:27 +0100810 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100811 return;
812 }
813 }
814 }
815 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000816 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100817 }
Damien George0288cf02014-04-11 11:53:00 +0000818 if (num_head != 0) {
819 if (0 == have_star_index) {
820 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100821 } else {
Damien George0288cf02014-04-11 11:53:00 +0000822 c_assign(comp, node_head, ASSIGN_STORE);
823 }
824 }
825 for (int i = 0; i < num_tail; i++) {
826 if (num_head + i == have_star_index) {
827 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
828 } else {
829 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100830 }
831 }
832}
833
834// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100835STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100836 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000837 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100838 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000839 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
840 if (MP_PARSE_NODE_IS_ID(pn)) {
841 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100842 switch (assign_kind) {
843 case ASSIGN_STORE:
844 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000845 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100846 break;
847 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000848 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100849 break;
850 }
851 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100852 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100853 return;
854 }
855 } else {
Damiend99b0522013-12-21 18:17:45 +0000856 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
857 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100858 case PN_power:
859 // lhs is an index or attribute
860 c_assign_power(comp, pns, assign_kind);
861 break;
862
863 case PN_testlist_star_expr:
864 case PN_exprlist:
865 // lhs is a tuple
866 if (assign_kind != ASSIGN_STORE) {
867 goto bad_aug;
868 }
Damien George0288cf02014-04-11 11:53:00 +0000869 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100870 break;
871
872 case PN_atom_paren:
873 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000874 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100875 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100876 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000877 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
878 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100879 goto testlist_comp;
880 } else {
881 // parenthesis around 1 item, is just that item
882 pn = pns->nodes[0];
883 goto tail_recursion;
884 }
885 break;
886
887 case PN_atom_bracket:
888 // lhs is something in brackets
889 if (assign_kind != ASSIGN_STORE) {
890 goto bad_aug;
891 }
Damiend99b0522013-12-21 18:17:45 +0000892 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100893 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000894 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000895 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
896 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100897 goto testlist_comp;
898 } else {
899 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000900 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100901 }
902 break;
903
904 default:
Damien George9d181f62014-04-27 16:55:27 +0100905 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100906 }
907 return;
908
909 testlist_comp:
910 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000911 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
912 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
913 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100914 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000915 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000916 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000917 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100918 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000919 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
920 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000921 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100922 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100923 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100924 } else {
925 // sequence with 2 items
926 goto sequence_with_2_items;
927 }
928 } else {
929 // sequence with 2 items
930 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000931 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100932 }
933 return;
934 }
935 return;
936
Damien George9d181f62014-04-27 16:55:27 +0100937 cannot_assign:
938 compile_syntax_error(comp, pn, "can't assign to expression");
939 return;
940
Damien429d7192013-10-04 19:53:11 +0100941 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100942 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100943}
944
945// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100946// if we are not in CPython compatibility mode then:
947// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
948// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
949// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100950STATIC 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 +0100951 assert(n_pos_defaults >= 0);
952 assert(n_kw_defaults >= 0);
953
Damien429d7192013-10-04 19:53:11 +0100954 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000955 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100956 int nfree = 0;
957 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000958 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
959 id_info_t *id = &comp->scope_cur->id_info[i];
960 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
961 for (int j = 0; j < this_scope->id_info_len; j++) {
962 id_info_t *id2 = &this_scope->id_info[j];
963 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000964#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000965 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000966#else
967 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100968 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000969#endif
Damien318aec62013-12-10 18:28:17 +0000970 nfree += 1;
971 }
972 }
Damien429d7192013-10-04 19:53:11 +0100973 }
974 }
975 }
Damien429d7192013-10-04 19:53:11 +0100976
977 // make the function/closure
978 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100979 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100980 } else {
Damien George3558f622014-04-20 17:50:40 +0100981 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100982 }
983}
984
Damien George6be0b0a2014-08-15 14:30:52 +0100985STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000986 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100987 comp->have_star = true;
988 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000989 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
990 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100991 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100992 } else {
993 // named star
Damien429d7192013-10-04 19:53:11 +0100994 }
Damien George8b19db02014-04-11 23:25:34 +0100995 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000996
997 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100998 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000999 // TODO do we need to do anything with this?
1000
1001 } else {
1002 mp_parse_node_t pn_id;
1003 mp_parse_node_t pn_colon;
1004 mp_parse_node_t pn_equal;
1005 if (MP_PARSE_NODE_IS_ID(pn)) {
1006 // this parameter is just an id
1007
1008 pn_id = pn;
1009 pn_colon = MP_PARSE_NODE_NULL;
1010 pn_equal = MP_PARSE_NODE_NULL;
1011
1012 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1013 // this parameter has a colon and/or equal specifier
1014
1015 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1016 pn_id = pns->nodes[0];
1017 pn_colon = pns->nodes[1];
1018 pn_equal = pns->nodes[2];
1019
1020 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001021 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001022 assert(0);
1023 return;
1024 }
1025
1026 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1027 // this parameter does not have a default value
1028
1029 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001030 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001031 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001032 return;
1033 }
1034
1035 } else {
1036 // this parameter has a default value
1037 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1038
Damien George8b19db02014-04-11 23:25:34 +01001039 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001040 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001041#if MICROPY_EMIT_CPYTHON
1042 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1043 compile_node(comp, pn_equal);
1044#else
Damien George69b89d22014-04-11 13:38:30 +00001045 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1046 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001047 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1048 // we need to do this here before we start building the map for the default keywords
1049 if (comp->num_default_params > 0) {
1050 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001051 } else {
1052 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001053 }
Damien George69b89d22014-04-11 13:38:30 +00001054 // first default dict param, so make the map
1055 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001056 }
Damien Georgef0778a72014-06-07 22:01:00 +01001057
1058 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001059 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001060 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001061 EMIT(store_map);
1062#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001063 } else {
Damien George69b89d22014-04-11 13:38:30 +00001064 comp->num_default_params += 1;
1065 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001066 }
1067 }
1068
1069 // TODO pn_colon not implemented
1070 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001071 }
1072}
1073
1074// leaves function object on stack
1075// returns function name
Damiend99b0522013-12-21 18:17:45 +00001076qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001077 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001078 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001079 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001080 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001081 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001082 }
1083
1084 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001085 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001086 uint old_num_dict_params = comp->num_dict_params;
1087 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001088
1089 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001090 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001091 comp->num_dict_params = 0;
1092 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001093 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001094
1095 if (comp->had_error) {
1096 return MP_QSTR_NULL;
1097 }
1098
Damien Georgee337f1e2014-03-31 15:18:37 +01001099#if !MICROPY_EMIT_CPYTHON
1100 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001101 // the default keywords args may have already made the tuple; if not, do it now
1102 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001103 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001104 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001105 }
1106#endif
1107
Damien429d7192013-10-04 19:53:11 +01001108 // get the scope for this function
1109 scope_t *fscope = (scope_t*)pns->nodes[4];
1110
1111 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001112 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001113
1114 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001115 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001116 comp->num_dict_params = old_num_dict_params;
1117 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001118
1119 // return its name (the 'f' in "def f(...):")
1120 return fscope->simple_name;
1121}
1122
1123// leaves class object on stack
1124// returns class name
Damiend99b0522013-12-21 18:17:45 +00001125qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001126 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001127 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001128 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001129 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001130 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001131 }
1132
1133 EMIT(load_build_class);
1134
1135 // scope for this class
1136 scope_t *cscope = (scope_t*)pns->nodes[3];
1137
1138 // compile the class
1139 close_over_variables_etc(comp, cscope, 0, 0);
1140
1141 // get its name
Damien George968bf342014-04-27 19:12:05 +01001142 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001143
1144 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001145 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1146 mp_parse_node_t parents = pns->nodes[1];
1147 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1148 parents = MP_PARSE_NODE_NULL;
1149 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001150 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001151 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001152
1153 // return its name (the 'C' in class C(...):")
1154 return cscope->simple_name;
1155}
1156
Damien6cdd3af2013-10-05 18:08:26 +01001157// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001158STATIC 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 +00001159 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001160 return false;
1161 }
1162
1163 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001164 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001165 return true;
1166 }
1167
Damiend99b0522013-12-21 18:17:45 +00001168 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001169 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001170 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001171#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001172 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001173 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001174 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001175 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001176#endif
Damien3ef4abb2013-10-12 16:53:13 +01001177#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001178 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001179 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001180#endif
Damien6cdd3af2013-10-05 18:08:26 +01001181 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001182 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001183 }
1184
1185 return true;
1186}
1187
Damiend99b0522013-12-21 18:17:45 +00001188void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001189 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001190 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001191 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1192
Damien6cdd3af2013-10-05 18:08:26 +01001193 // inherit emit options for this function/class definition
1194 uint emit_options = comp->scope_cur->emit_options;
1195
1196 // compile each decorator
1197 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001198 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001199 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1200 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001201
1202 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001203 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001204 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1205
1206 // check for built-in decorators
1207 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1208 // this was a built-in
1209 num_built_in_decorators += 1;
1210
1211 } else {
1212 // not a built-in, compile normally
1213
1214 // compile the decorator function
1215 compile_node(comp, name_nodes[0]);
1216 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001217 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001218 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001219 }
1220
1221 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001222 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001223 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001224 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001225 compile_node(comp, pns_decorator->nodes[1]);
1226 }
Damien429d7192013-10-04 19:53:11 +01001227 }
1228 }
1229
1230 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001231 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001232 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001233 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001234 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001235 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001236 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001237 } else {
1238 // shouldn't happen
1239 assert(0);
1240 }
1241
1242 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001243 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001244 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001245 }
1246
1247 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001248 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001249}
1250
Damiend99b0522013-12-21 18:17:45 +00001251void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001252 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001253 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001254 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001255}
1256
Damien George6be0b0a2014-08-15 14:30:52 +01001257STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001258 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001259 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001260 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1261 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001262
1263 compile_node(comp, pns->nodes[0]); // base of the power node
1264
Damiend99b0522013-12-21 18:17:45 +00001265 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1266 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1267 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1268 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001269 for (int i = 0; i < n - 1; i++) {
1270 compile_node(comp, pns1->nodes[i]);
1271 }
Damiend99b0522013-12-21 18:17:45 +00001272 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1273 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001274 }
Damiend99b0522013-12-21 18:17:45 +00001275 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001276 // can't delete function calls
1277 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001278 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001279 compile_node(comp, pns1->nodes[0]);
1280 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001281 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1282 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001283 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001284 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001285 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001286 }
1287 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001288 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001289 }
1290
Damiend99b0522013-12-21 18:17:45 +00001291 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001292 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001293 }
Damiend99b0522013-12-21 18:17:45 +00001294 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1295 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1296 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1297 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001298 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1299
Damiend99b0522013-12-21 18:17:45 +00001300 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1301 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1302 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001303 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001304 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001305 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001306 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001307 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001308 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001309 c_del_stmt(comp, pns->nodes[0]);
1310 for (int i = 0; i < n; i++) {
1311 c_del_stmt(comp, pns1->nodes[i]);
1312 }
Damiend99b0522013-12-21 18:17:45 +00001313 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001314 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001315 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001316 } else {
1317 // sequence with 2 items
1318 goto sequence_with_2_items;
1319 }
1320 } else {
1321 // sequence with 2 items
1322 sequence_with_2_items:
1323 c_del_stmt(comp, pns->nodes[0]);
1324 c_del_stmt(comp, pns->nodes[1]);
1325 }
1326 } else {
1327 // tuple with 1 element
1328 c_del_stmt(comp, pn);
1329 }
1330 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001331 // TODO is there anything else to implement?
1332 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001333 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001334
1335 return;
1336
1337cannot_delete:
1338 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001339}
1340
Damiend99b0522013-12-21 18:17:45 +00001341void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001342 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1343}
1344
Damiend99b0522013-12-21 18:17:45 +00001345void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001346 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001347 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001348 }
Damien Georgecbddb272014-02-01 20:08:18 +00001349 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001350}
1351
Damiend99b0522013-12-21 18:17:45 +00001352void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001353 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001354 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001355 }
Damien Georgecbddb272014-02-01 20:08:18 +00001356 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001357}
1358
Damiend99b0522013-12-21 18:17:45 +00001359void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001360 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001361 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001362 return;
1363 }
Damiend99b0522013-12-21 18:17:45 +00001364 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001365 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001366 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001367 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001368 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001369 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1370 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 +01001371
Damien George6f355fd2014-04-10 14:11:31 +01001372 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001373 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1374 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1375 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001376 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001377 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1378 } else {
1379 compile_node(comp, pns->nodes[0]);
1380 }
1381 EMIT(return_value);
1382}
1383
Damiend99b0522013-12-21 18:17:45 +00001384void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001385 compile_node(comp, pns->nodes[0]);
1386 EMIT(pop_top);
1387}
1388
Damiend99b0522013-12-21 18:17:45 +00001389void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1390 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001391 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001392 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001393 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001394 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001395 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001396 compile_node(comp, pns->nodes[0]);
1397 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001398 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001399 } else {
1400 // raise x
1401 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001402 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001403 }
1404}
1405
Damien George635543c2014-04-10 12:56:52 +01001406// q_base holds the base of the name
1407// eg a -> q_base=a
1408// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001409STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001410 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001411 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1412 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001413 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001414 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001415 pn = pns->nodes[0];
1416 is_as = true;
1417 }
Damien George635543c2014-04-10 12:56:52 +01001418 if (MP_PARSE_NODE_IS_NULL(pn)) {
1419 // empty name (eg, from . import x)
1420 *q_base = MP_QSTR_;
1421 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1422 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001423 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001424 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001425 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001426 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001427 }
Damien George635543c2014-04-10 12:56:52 +01001428 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001429 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1430 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1431 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001432 // a name of the form a.b.c
1433 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001434 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001435 }
Damiend99b0522013-12-21 18:17:45 +00001436 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001437 int len = n - 1;
1438 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001439 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001440 }
Damien George55baff42014-01-21 21:40:13 +00001441 byte *q_ptr;
1442 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001443 for (int i = 0; i < n; i++) {
1444 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001445 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001446 }
Damien George55baff42014-01-21 21:40:13 +00001447 uint str_src_len;
1448 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001449 memcpy(str_dest, str_src, str_src_len);
1450 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001451 }
Damien George635543c2014-04-10 12:56:52 +01001452 qstr q_full = qstr_build_end(q_ptr);
1453 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001454 if (is_as) {
1455 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001456 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001457 }
1458 }
1459 } else {
Damien George635543c2014-04-10 12:56:52 +01001460 // shouldn't happen
1461 assert(0);
Damien429d7192013-10-04 19:53:11 +01001462 }
1463 } else {
Damien George635543c2014-04-10 12:56:52 +01001464 // shouldn't happen
1465 assert(0);
Damien429d7192013-10-04 19:53:11 +01001466 }
1467}
1468
Damien George6be0b0a2014-08-15 14:30:52 +01001469STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001470 EMIT_ARG(load_const_small_int, 0); // level 0 import
1471 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1472 qstr q_base;
1473 do_import_name(comp, pn, &q_base);
1474 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001475}
1476
Damiend99b0522013-12-21 18:17:45 +00001477void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001478 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1479}
1480
Damiend99b0522013-12-21 18:17:45 +00001481void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001482 mp_parse_node_t pn_import_source = pns->nodes[0];
1483
1484 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1485 uint import_level = 0;
1486 do {
1487 mp_parse_node_t pn_rel;
1488 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)) {
1489 // This covers relative imports with dots only like "from .. import"
1490 pn_rel = pn_import_source;
1491 pn_import_source = MP_PARSE_NODE_NULL;
1492 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1493 // This covers relative imports starting with dot(s) like "from .foo import"
1494 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1495 pn_rel = pns_2b->nodes[0];
1496 pn_import_source = pns_2b->nodes[1];
1497 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1498 } else {
1499 // Not a relative import
1500 break;
1501 }
1502
1503 // get the list of . and/or ...'s
1504 mp_parse_node_t *nodes;
1505 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1506
1507 // count the total number of .'s
1508 for (int i = 0; i < n; i++) {
1509 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1510 import_level++;
1511 } else {
1512 // should be an MP_TOKEN_ELLIPSIS
1513 import_level += 3;
1514 }
1515 }
1516 } while (0);
1517
Damiend99b0522013-12-21 18:17:45 +00001518 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001519 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001520
1521 // build the "fromlist" tuple
1522#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001523 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001524#else
Damien George708c0732014-04-27 19:23:46 +01001525 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001526 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001527#endif
1528
1529 // do the import
Damien George635543c2014-04-10 12:56:52 +01001530 qstr dummy_q;
1531 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001532 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001533
Damien429d7192013-10-04 19:53:11 +01001534 } else {
Damien George635543c2014-04-10 12:56:52 +01001535 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001536
1537 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001538 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001539 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001540#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001541 {
1542 vstr_t *vstr = vstr_new();
1543 vstr_printf(vstr, "(");
1544 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001545 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1546 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1547 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001548 if (i > 0) {
1549 vstr_printf(vstr, ", ");
1550 }
1551 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001552 uint len;
1553 const byte *str = qstr_data(id2, &len);
1554 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001555 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001556 }
Damien02f89412013-12-12 15:13:36 +00001557 if (n == 1) {
1558 vstr_printf(vstr, ",");
1559 }
1560 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001562 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001563 }
Damiendb4c3612013-12-10 17:27:24 +00001564#else
1565 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001566 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1567 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1568 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001569 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001570 }
Damien Georgeb9791222014-01-23 00:34:21 +00001571 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001572#endif
1573
1574 // do the import
Damien George635543c2014-04-10 12:56:52 +01001575 qstr dummy_q;
1576 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001577 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001578 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1579 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1580 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001581 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001582 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001583 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001584 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001585 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001586 }
1587 }
1588 EMIT(pop_top);
1589 }
1590}
1591
Damiend99b0522013-12-21 18:17:45 +00001592void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001593 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001594 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1595 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001596 } else {
Damiend99b0522013-12-21 18:17:45 +00001597 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1598 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001599 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001600 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001601 }
Damien429d7192013-10-04 19:53:11 +01001602 }
1603 }
1604}
1605
Damiend99b0522013-12-21 18:17:45 +00001606void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001607 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001608 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1609 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001610 } else {
Damiend99b0522013-12-21 18:17:45 +00001611 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1612 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001613 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001614 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001615 }
Damien429d7192013-10-04 19:53:11 +01001616 }
1617 }
1618}
1619
Damiend99b0522013-12-21 18:17:45 +00001620void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001621 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001622 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001623 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 +00001624 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001625 // assertion message
1626 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001627 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001628 }
Damien Georgeb9791222014-01-23 00:34:21 +00001629 EMIT_ARG(raise_varargs, 1);
1630 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001631}
1632
Damiend99b0522013-12-21 18:17:45 +00001633void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001634 // TODO proper and/or short circuiting
1635
Damien George6f355fd2014-04-10 14:11:31 +01001636 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001637
Damien George6f355fd2014-04-10 14:11:31 +01001638 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001639 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1640
1641 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001642
1643 if (
1644#if !MICROPY_EMIT_CPYTHON
1645 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1646 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1647#endif
1648 // optimisation to not jump if last instruction was return
1649 !EMIT(last_emit_was_return_value)
1650 ) {
1651 // jump over elif/else blocks
1652 EMIT_ARG(jump, l_end);
1653 }
1654
Damien Georgeb9791222014-01-23 00:34:21 +00001655 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001656
Damiend99b0522013-12-21 18:17:45 +00001657 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001658 // compile elif blocks
1659
Damiend99b0522013-12-21 18:17:45 +00001660 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001661
Damiend99b0522013-12-21 18:17:45 +00001662 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001663 // multiple elif blocks
1664
Damiend99b0522013-12-21 18:17:45 +00001665 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001666 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001667 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001668 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001669 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1670
1671 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001672 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001673 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001674 }
Damien Georgeb9791222014-01-23 00:34:21 +00001675 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001676 }
1677
1678 } else {
1679 // a single elif block
1680
Damienb05d7072013-10-05 13:37:10 +01001681 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001682 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1683
1684 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001685 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001686 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001687 }
Damien Georgeb9791222014-01-23 00:34:21 +00001688 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001689 }
1690 }
1691
1692 // compile else block
1693 compile_node(comp, pns->nodes[3]); // can be null
1694
Damien Georgeb9791222014-01-23 00:34:21 +00001695 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001696}
1697
Damien Georgecbddb272014-02-01 20:08:18 +00001698#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001699 uint old_break_label = comp->break_label; \
1700 uint old_continue_label = comp->continue_label; \
1701 uint break_label = comp_next_label(comp); \
1702 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001703 comp->break_label = break_label; \
1704 comp->continue_label = continue_label; \
1705 comp->break_continue_except_level = comp->cur_except_level;
1706
1707#define END_BREAK_CONTINUE_BLOCK \
1708 comp->break_label = old_break_label; \
1709 comp->continue_label = old_continue_label; \
1710 comp->break_continue_except_level = comp->cur_except_level;
1711
Damiend99b0522013-12-21 18:17:45 +00001712void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001713 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001714
Damience89a212013-10-15 22:25:17 +01001715 // compared to CPython, we have an optimised version of while loops
1716#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001717 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001718 EMIT_ARG(setup_loop, break_label);
1719 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001720 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1721 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001722 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001724 }
Damien Georgeb9791222014-01-23 00:34:21 +00001725 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001726 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1727 // this is a small hack to agree with CPython
1728 if (!node_is_const_true(pns->nodes[0])) {
1729 EMIT(pop_block);
1730 }
Damience89a212013-10-15 22:25:17 +01001731#else
Damien George6f355fd2014-04-10 14:11:31 +01001732 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001733 EMIT_ARG(jump, continue_label);
1734 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001735 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001736 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001737 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1738#endif
1739
1740 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001741 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001742
1743 compile_node(comp, pns->nodes[2]); // else
1744
Damien Georgeb9791222014-01-23 00:34:21 +00001745 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001746}
1747
Damienf72fd0e2013-11-06 20:20:49 +00001748// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001749// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1750// And, if the loop never runs, the loop variable should never be assigned
Damien George6be0b0a2014-08-15 14:30:52 +01001751STATIC 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 +00001752 START_BREAK_CONTINUE_BLOCK
Damien George5b5562c2014-05-31 17:59:11 +01001753 // note that we don't need to pop anything when breaking from an optimise for loop
Damienf72fd0e2013-11-06 20:20:49 +00001754
Damien George6f355fd2014-04-10 14:11:31 +01001755 uint top_label = comp_next_label(comp);
1756 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001757
Damien George3ff2d032014-03-31 18:02:22 +01001758 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001759 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001760 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001761
Damien Georgeb9791222014-01-23 00:34:21 +00001762 EMIT_ARG(jump, entry_label);
1763 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001764
Damien George3ff2d032014-03-31 18:02:22 +01001765 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001766 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001767
1768 // store next value to var
1769 c_assign(comp, pn_var, ASSIGN_STORE);
1770
Damienf3822fc2013-11-09 20:12:03 +00001771 // compile body
1772 compile_node(comp, pn_body);
1773
Damien Georgeb9791222014-01-23 00:34:21 +00001774 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001775
Damien George3ff2d032014-03-31 18:02:22 +01001776 // compile: var + step, duplicated on stack
1777 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001778 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001779 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001780 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001781
Damien Georgeb9791222014-01-23 00:34:21 +00001782 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001783
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001784 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001785 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001786 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1787 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001788 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001789 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001790 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001791 }
Damien Georgeb9791222014-01-23 00:34:21 +00001792 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001793
Damien George3ff2d032014-03-31 18:02:22 +01001794 // discard final value of var that failed the loop condition
1795 EMIT(pop_top);
1796
Damienf72fd0e2013-11-06 20:20:49 +00001797 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001798 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001799
1800 compile_node(comp, pn_else);
1801
Damien Georgeb9791222014-01-23 00:34:21 +00001802 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001803}
1804
Damiend99b0522013-12-21 18:17:45 +00001805void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001806#if !MICROPY_EMIT_CPYTHON
1807 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1808 // this is actually slower, but uses no heap memory
1809 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001810 if (/*comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
Damiend99b0522013-12-21 18:17:45 +00001811 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001812 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1813 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1814 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1815 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001816 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1817 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001818 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001819 mp_parse_node_t pn_range_start;
1820 mp_parse_node_t pn_range_end;
1821 mp_parse_node_t pn_range_step;
1822 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001823 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001824 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001825 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001826 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001827 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001828 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001829 } else if (n_args == 2) {
1830 pn_range_start = args[0];
1831 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001832 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001833 } else {
1834 pn_range_start = args[0];
1835 pn_range_end = args[1];
1836 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001837 // We need to know sign of step. This is possible only if it's constant
1838 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1839 optimize = false;
1840 }
Damienf72fd0e2013-11-06 20:20:49 +00001841 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001842 }
1843 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001844 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1845 return;
1846 }
1847 }
1848 }
1849#endif
1850
Damien Georgecbddb272014-02-01 20:08:18 +00001851 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001852 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001853
Damien George6f355fd2014-04-10 14:11:31 +01001854 uint pop_label = comp_next_label(comp);
1855 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001856
Damience89a212013-10-15 22:25:17 +01001857 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1858#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001859 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001860#endif
1861
Damien429d7192013-10-04 19:53:11 +01001862 compile_node(comp, pns->nodes[1]); // iterator
1863 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001864 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001865 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001866 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1867 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001868 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001869 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001870 }
Damien Georgeb9791222014-01-23 00:34:21 +00001871 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001872 EMIT(for_iter_end);
1873
1874 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001875 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001876
Damience89a212013-10-15 22:25:17 +01001877#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001878 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001879#endif
Damien429d7192013-10-04 19:53:11 +01001880
1881 compile_node(comp, pns->nodes[3]); // else (not tested)
1882
Damien Georgeb9791222014-01-23 00:34:21 +00001883 EMIT_ARG(label_assign, break_label);
1884 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001885}
1886
Damien George6be0b0a2014-08-15 14:30:52 +01001887STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_excepts, mp_parse_node_t pn_else) {
Damien429d7192013-10-04 19:53:11 +01001888 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001889 uint l1 = comp_next_label(comp);
1890 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001891
Damien Georgeb9791222014-01-23 00:34:21 +00001892 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001893 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001894
Damien429d7192013-10-04 19:53:11 +01001895 compile_node(comp, pn_body); // body
1896 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001897 EMIT_ARG(jump, success_label); // jump over exception handler
1898
1899 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001900 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001901
Damien George6f355fd2014-04-10 14:11:31 +01001902 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001903
1904 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001905 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1906 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001907
1908 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001909 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001910
Damiend99b0522013-12-21 18:17:45 +00001911 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001912 // this is a catch all exception handler
1913 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001914 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001915 return;
1916 }
1917 } else {
1918 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001919 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1920 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1921 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1922 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001923 // handler binds the exception to a local
1924 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001925 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001926 }
1927 }
1928 EMIT(dup_top);
1929 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001930 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001931 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001932 }
1933
1934 EMIT(pop_top);
1935
1936 if (qstr_exception_local == 0) {
1937 EMIT(pop_top);
1938 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001939 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001940 }
1941
1942 EMIT(pop_top);
1943
Damien George6f355fd2014-04-10 14:11:31 +01001944 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001945 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001946 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001947 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001948 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001949 }
1950 compile_node(comp, pns_except->nodes[1]);
1951 if (qstr_exception_local != 0) {
1952 EMIT(pop_block);
1953 }
1954 EMIT(pop_except);
1955 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001956 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1957 EMIT_ARG(label_assign, l3);
1958 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1959 EMIT_ARG(store_id, qstr_exception_local);
1960 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001961
Damien George8dcc0c72014-03-27 10:55:21 +00001962 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001963 EMIT(end_finally);
1964 }
Damien Georgeb9791222014-01-23 00:34:21 +00001965 EMIT_ARG(jump, l2);
1966 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001967 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001968 }
1969
Damien George8dcc0c72014-03-27 10:55:21 +00001970 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001971 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001972 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001973
Damien Georgeb9791222014-01-23 00:34:21 +00001974 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001975 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001976 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001977}
1978
Damien George6be0b0a2014-08-15 14:30:52 +01001979STATIC void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
Damien George6f355fd2014-04-10 14:11:31 +01001980 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001981
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001983 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001984
Damien429d7192013-10-04 19:53:11 +01001985 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001986 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001987 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001988 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001989 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001990 } else {
1991 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1992 }
1993 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001994 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1995 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001996 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001997
Damien George8dcc0c72014-03-27 10:55:21 +00001998 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001999 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002000}
2001
Damiend99b0522013-12-21 18:17:45 +00002002void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2003 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2004 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2005 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002006 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002007 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2008 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002009 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002010 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002011 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002012 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002013 // no finally
2014 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2015 } else {
2016 // have finally
Damiend99b0522013-12-21 18:17:45 +00002017 compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((mp_parse_node_struct_t*)pns2->nodes[2])->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01002018 }
2019 } else {
2020 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002021 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002022 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002023 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002024 }
2025 } else {
2026 // shouldn't happen
2027 assert(0);
2028 }
2029}
2030
Damien George6be0b0a2014-08-15 14:30:52 +01002031STATIC void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
Damien429d7192013-10-04 19:53:11 +01002032 if (n == 0) {
2033 // no more pre-bits, compile the body of the with
2034 compile_node(comp, body);
2035 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002036 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002037 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002038 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002039 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002040 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002041 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002042 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2043 } else {
2044 // this pre-bit is just an expression
2045 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002046 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002047 EMIT(pop_top);
2048 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002049 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002050 // compile additional pre-bits and the body
2051 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2052 // finish this with block
2053 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002054 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2055 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002056 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002057 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002058 EMIT(end_finally);
2059 }
2060}
2061
Damiend99b0522013-12-21 18:17:45 +00002062void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002063 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002064 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002065 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2066 assert(n > 0);
2067
2068 // compile in a nested fashion
2069 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2070}
2071
Damiend99b0522013-12-21 18:17:45 +00002072void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2073 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002074 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2075 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002076 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002077 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002078 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002079 EMIT(pop_top);
2080
Damien429d7192013-10-04 19:53:11 +01002081 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002082 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002083 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2084 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002085 // do nothing with a lonely constant
2086 } else {
2087 compile_node(comp, pns->nodes[0]); // just an expression
2088 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2089 }
Damien429d7192013-10-04 19:53:11 +01002090 }
2091 } else {
Damiend99b0522013-12-21 18:17:45 +00002092 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2093 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002094 if (kind == PN_expr_stmt_augassign) {
2095 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2096 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002097 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002098 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002099 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002100 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2101 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2102 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2103 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2104 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2105 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2106 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2107 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2108 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2109 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2110 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2111 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2112 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002113 }
Damien George7e5fb242014-02-01 22:18:47 +00002114 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002115 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2116 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002117 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2118 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002119 // following CPython, we store left-most first
2120 if (rhs > 0) {
2121 EMIT(dup_top);
2122 }
2123 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2124 for (int i = 0; i < rhs; i++) {
2125 if (i + 1 < rhs) {
2126 EMIT(dup_top);
2127 }
Damiend99b0522013-12-21 18:17:45 +00002128 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002129 }
2130 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002131 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002132 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2133 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2134 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002135 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002136 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2137 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002138 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2139 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2140 // can't optimise when it's a star expression on the lhs
2141 goto no_optimisation;
2142 }
Damien429d7192013-10-04 19:53:11 +01002143 compile_node(comp, pns10->nodes[0]); // rhs
2144 compile_node(comp, pns10->nodes[1]); // rhs
2145 EMIT(rot_two);
2146 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2147 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002148 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2149 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2150 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2151 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002152 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002153 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2154 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002155 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2156 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2157 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2158 // can't optimise when it's a star expression on the lhs
2159 goto no_optimisation;
2160 }
Damien429d7192013-10-04 19:53:11 +01002161 compile_node(comp, pns10->nodes[0]); // rhs
2162 compile_node(comp, pns10->nodes[1]); // rhs
2163 compile_node(comp, pns10->nodes[2]); // rhs
2164 EMIT(rot_three);
2165 EMIT(rot_two);
2166 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2167 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2168 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2169 } else {
Damien George495d7812014-04-08 17:51:47 +01002170 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002171 compile_node(comp, pns1->nodes[0]); // rhs
2172 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2173 }
2174 } else {
2175 // shouldn't happen
2176 assert(0);
2177 }
2178 }
2179}
2180
Damien George6be0b0a2014-08-15 14:30:52 +01002181STATIC void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002182 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002183 compile_node(comp, pns->nodes[0]);
2184 for (int i = 1; i < num_nodes; i += 1) {
2185 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002186 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002187 }
2188}
2189
Damiend99b0522013-12-21 18:17:45 +00002190void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2191 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2192 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002193
Damien George6f355fd2014-04-10 14:11:31 +01002194 uint l_fail = comp_next_label(comp);
2195 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002196 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2197 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002198 EMIT_ARG(jump, l_end);
2199 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002200 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002201 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002202 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002203}
2204
Damiend99b0522013-12-21 18:17:45 +00002205void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002206 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002207 //mp_parse_node_t pn_params = pns->nodes[0];
2208 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002209
Damien George36db6bc2014-05-07 17:24:22 +01002210 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002211 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002212 scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002213 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002214 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002215 }
2216
2217 // get the scope for this lambda
2218 scope_t *this_scope = (scope_t*)pns->nodes[2];
2219
2220 // make the lambda
2221 close_over_variables_etc(comp, this_scope, 0, 0);
2222}
2223
Damiend99b0522013-12-21 18:17:45 +00002224void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002225 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002226 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002227 for (int i = 0; i < n; i += 1) {
2228 compile_node(comp, pns->nodes[i]);
2229 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002230 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002231 }
2232 }
Damien Georgeb9791222014-01-23 00:34:21 +00002233 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002234}
2235
Damiend99b0522013-12-21 18:17:45 +00002236void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002237 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002238 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002239 for (int i = 0; i < n; i += 1) {
2240 compile_node(comp, pns->nodes[i]);
2241 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002242 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002243 }
2244 }
Damien Georgeb9791222014-01-23 00:34:21 +00002245 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002246}
2247
Damiend99b0522013-12-21 18:17:45 +00002248void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002249 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002250 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002251}
2252
Damiend99b0522013-12-21 18:17:45 +00002253void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002254 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002255 compile_node(comp, pns->nodes[0]);
2256 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002257 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002258 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002259 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002260 }
2261 for (int i = 1; i + 1 < num_nodes; i += 2) {
2262 compile_node(comp, pns->nodes[i + 1]);
2263 if (i + 2 < num_nodes) {
2264 EMIT(dup_top);
2265 EMIT(rot_three);
2266 }
Damien George7e5fb242014-02-01 22:18:47 +00002267 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002268 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002269 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002270 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2271 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2272 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2273 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2274 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2275 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2276 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2277 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002278 }
2279 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002280 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2281 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2282 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002283 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002284 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002285 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002286 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002287 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002288 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002289 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002290 }
2291 } else {
2292 // shouldn't happen
2293 assert(0);
2294 }
2295 } else {
2296 // shouldn't happen
2297 assert(0);
2298 }
2299 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002300 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002301 }
2302 }
2303 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002304 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002305 EMIT_ARG(jump, l_end);
2306 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002307 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002308 EMIT(rot_two);
2309 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002310 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002311 }
2312}
2313
Damiend99b0522013-12-21 18:17:45 +00002314void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002315 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002316}
2317
Damiend99b0522013-12-21 18:17:45 +00002318void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002319 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002320}
2321
Damiend99b0522013-12-21 18:17:45 +00002322void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002323 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002324}
2325
Damiend99b0522013-12-21 18:17:45 +00002326void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002327 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002328}
2329
Damiend99b0522013-12-21 18:17:45 +00002330void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2331 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002332 compile_node(comp, pns->nodes[0]);
2333 for (int i = 1; i + 1 < num_nodes; i += 2) {
2334 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002335 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002336 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002337 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002338 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002339 } else {
2340 // shouldn't happen
2341 assert(0);
2342 }
2343 }
2344}
2345
Damiend99b0522013-12-21 18:17:45 +00002346void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2347 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002348 compile_node(comp, pns->nodes[0]);
2349 for (int i = 1; i + 1 < num_nodes; i += 2) {
2350 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002351 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002352 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002353 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002354 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002355 } else {
2356 // shouldn't happen
2357 assert(0);
2358 }
2359 }
2360}
2361
Damiend99b0522013-12-21 18:17:45 +00002362void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2363 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002364 compile_node(comp, pns->nodes[0]);
2365 for (int i = 1; i + 1 < num_nodes; i += 2) {
2366 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002367 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002368 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002369 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002370 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002371 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002372 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002373 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002374 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002375 } else {
2376 // shouldn't happen
2377 assert(0);
2378 }
2379 }
2380}
2381
Damiend99b0522013-12-21 18:17:45 +00002382void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002383 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002384 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002385 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002386 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002387 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002388 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002389 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002390 } else {
2391 // shouldn't happen
2392 assert(0);
2393 }
2394}
2395
Damien George35e2a4e2014-02-05 00:51:47 +00002396void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2397 // this is to handle special super() call
2398 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2399
2400 compile_generic_all_nodes(comp, pns);
2401}
2402
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002403STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra) {
Damien429d7192013-10-04 19:53:11 +01002404 // function to call is on top of stack
2405
Damien George35e2a4e2014-02-05 00:51:47 +00002406#if !MICROPY_EMIT_CPYTHON
2407 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002408 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George35e2a4e2014-02-05 00:51:47 +00002409 EMIT_ARG(load_id, MP_QSTR___class__);
2410 // get first argument to function
2411 bool found = false;
2412 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002413 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2414 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George35e2a4e2014-02-05 00:51:47 +00002415 found = true;
2416 break;
2417 }
2418 }
2419 if (!found) {
2420 printf("TypeError: super() call cannot find self\n");
2421 return;
2422 }
Damien George922ddd62014-04-09 12:43:17 +01002423 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002424 return;
2425 }
2426#endif
2427
Damien George2c0842b2014-04-27 16:46:51 +01002428 // get the list of arguments
2429 mp_parse_node_t *args;
2430 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002431
Damien George2c0842b2014-04-27 16:46:51 +01002432 // compile the arguments
2433 // Rather than calling compile_node on the list, we go through the list of args
2434 // explicitly here so that we can count the number of arguments and give sensible
2435 // error messages.
2436 int n_positional = n_positional_extra;
2437 uint n_keyword = 0;
2438 uint star_flags = 0;
2439 for (int i = 0; i < n_args; i++) {
2440 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2441 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2442 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2443 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2444 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2445 return;
2446 }
2447 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2448 compile_node(comp, pns_arg->nodes[0]);
2449 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2450 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2451 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2452 return;
2453 }
2454 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2455 compile_node(comp, pns_arg->nodes[0]);
2456 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2457 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2458 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2459 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2460 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2461 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2462 return;
2463 }
Damien George968bf342014-04-27 19:12:05 +01002464 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002465 compile_node(comp, pns2->nodes[0]);
2466 n_keyword += 1;
2467 } else {
2468 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2469 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2470 n_positional++;
2471 }
2472 } else {
2473 goto normal_argument;
2474 }
2475 } else {
2476 normal_argument:
2477 if (n_keyword > 0) {
2478 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2479 return;
2480 }
2481 compile_node(comp, args[i]);
2482 n_positional++;
2483 }
Damien429d7192013-10-04 19:53:11 +01002484 }
2485
Damien George2c0842b2014-04-27 16:46:51 +01002486 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002487 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002488 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002489 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002490 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002491 }
Damien429d7192013-10-04 19:53:11 +01002492}
2493
Damiend99b0522013-12-21 18:17:45 +00002494void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2495 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002496 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002497 if (i + 1 < num_nodes && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i], PN_trailer_period) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i + 1], PN_trailer_paren)) {
Damien429d7192013-10-04 19:53:11 +01002498 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002499 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2500 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002501 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002502 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002503 i += 1;
2504 } else {
2505 compile_node(comp, pns->nodes[i]);
2506 }
Damien George35e2a4e2014-02-05 00:51:47 +00002507 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002508 }
2509}
2510
Damiend99b0522013-12-21 18:17:45 +00002511void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002512 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002513 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002514}
2515
Damiend99b0522013-12-21 18:17:45 +00002516void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002517 // a list of strings
Damien63321742013-12-10 17:41:49 +00002518
2519 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002520 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002521 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002522 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002523 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002524 int pn_kind;
2525 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2526 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2527 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2528 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2529 } else {
2530 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2531 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2532 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2533 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002534 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002535 }
Damien63321742013-12-10 17:41:49 +00002536 if (i == 0) {
2537 string_kind = pn_kind;
2538 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002539 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002540 return;
2541 }
Damien429d7192013-10-04 19:53:11 +01002542 }
Damien63321742013-12-10 17:41:49 +00002543
Damien63321742013-12-10 17:41:49 +00002544 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002545 byte *q_ptr;
2546 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002547 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002548 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2549 uint s_len;
2550 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2551 memcpy(s_dest, s, s_len);
2552 s_dest += s_len;
2553 } else {
2554 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002555 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2556 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002557 }
Damien63321742013-12-10 17:41:49 +00002558 }
Damien George55baff42014-01-21 21:40:13 +00002559 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002560
Damien Georgeb9791222014-01-23 00:34:21 +00002561 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002562}
2563
2564// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002565STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002566 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2567 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2568 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002569
Damien George36db6bc2014-05-07 17:24:22 +01002570 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002571 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002572 scope_t *s = scope_new_and_link(comp, kind, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002573 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002574 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002575 }
2576
2577 // get the scope for this comprehension
2578 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2579
2580 // compile the comprehension
2581 close_over_variables_etc(comp, this_scope, 0, 0);
2582
2583 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2584 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002585 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002586}
2587
Damiend99b0522013-12-21 18:17:45 +00002588void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2589 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002590 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002591 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2592 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2593 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2594 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2595 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2596 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2597 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002598 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002599 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002600 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002601 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002602 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002603 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002604 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002605 // generator expression
2606 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2607 } else {
2608 // tuple with 2 items
2609 goto tuple_with_2_items;
2610 }
2611 } else {
2612 // tuple with 2 items
2613 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002614 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002615 }
2616 } else {
2617 // parenthesis around a single item, is just that item
2618 compile_node(comp, pns->nodes[0]);
2619 }
2620}
2621
Damiend99b0522013-12-21 18:17:45 +00002622void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2623 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002624 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002625 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002626 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2627 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2628 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2629 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2630 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002631 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002632 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002633 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002634 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002635 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002636 // list of many items
2637 compile_node(comp, pns2->nodes[0]);
2638 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002639 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002640 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002641 // list comprehension
2642 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2643 } else {
2644 // list with 2 items
2645 goto list_with_2_items;
2646 }
2647 } else {
2648 // list with 2 items
2649 list_with_2_items:
2650 compile_node(comp, pns2->nodes[0]);
2651 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002652 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002653 }
2654 } else {
2655 // list with 1 item
2656 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002657 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002658 }
2659}
2660
Damiend99b0522013-12-21 18:17:45 +00002661void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2662 mp_parse_node_t pn = pns->nodes[0];
2663 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002664 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002665 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002666 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2667 pns = (mp_parse_node_struct_t*)pn;
2668 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002669 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002670 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002671 compile_node(comp, pn);
2672 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002673 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2674 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2675 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2676 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002677 // dict/set with multiple elements
2678
2679 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002680 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002681 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2682
2683 // first element sets whether it's a dict or set
2684 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002685 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002686 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002687 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002688 compile_node(comp, pns->nodes[0]);
2689 EMIT(store_map);
2690 is_dict = true;
2691 } else {
2692 // a set
2693 compile_node(comp, pns->nodes[0]); // 1st value of set
2694 is_dict = false;
2695 }
2696
2697 // process rest of elements
2698 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002699 mp_parse_node_t pn = nodes[i];
2700 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002701 compile_node(comp, pn);
2702 if (is_dict) {
2703 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002704 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002705 return;
2706 }
2707 EMIT(store_map);
2708 } else {
2709 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002710 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002711 return;
2712 }
2713 }
2714 }
2715
2716 // if it's a set, build it
2717 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002718 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002719 }
Damiend99b0522013-12-21 18:17:45 +00002720 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002721 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002722 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002723 // a dictionary comprehension
2724 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2725 } else {
2726 // a set comprehension
2727 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2728 }
2729 } else {
2730 // shouldn't happen
2731 assert(0);
2732 }
2733 } else {
2734 // set with one element
2735 goto set_with_one_element;
2736 }
2737 } else {
2738 // set with one element
2739 set_with_one_element:
2740 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002741 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002742 }
2743}
2744
Damiend99b0522013-12-21 18:17:45 +00002745void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002746 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002747}
2748
Damiend99b0522013-12-21 18:17:45 +00002749void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002750 // object who's index we want is on top of stack
2751 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002752 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002753}
2754
Damiend99b0522013-12-21 18:17:45 +00002755void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002756 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002757 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002758}
2759
Damiend99b0522013-12-21 18:17:45 +00002760void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2761 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2762 mp_parse_node_t pn = pns->nodes[0];
2763 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002764 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002765 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2766 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002767 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2768 pns = (mp_parse_node_struct_t*)pn;
2769 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002770 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002771 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002772 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002773 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002774 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002775 } else {
2776 // [?::x]
2777 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002778 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002779 }
Damiend99b0522013-12-21 18:17:45 +00002780 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002781 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002782 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2783 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2784 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2785 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002786 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002787 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002788 } else {
2789 // [?:x:x]
2790 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002791 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002792 }
2793 } else {
2794 // [?:x]
2795 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002796 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002797 }
2798 } else {
2799 // [?:x]
2800 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002801 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002802 }
2803}
2804
Damiend99b0522013-12-21 18:17:45 +00002805void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002806 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002807 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2808 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002809}
2810
Damiend99b0522013-12-21 18:17:45 +00002811void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002812 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002813 compile_subscript_3_helper(comp, pns);
2814}
2815
Damiend99b0522013-12-21 18:17:45 +00002816void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002817 // if this is called then we are compiling a dict key:value pair
2818 compile_node(comp, pns->nodes[1]); // value
2819 compile_node(comp, pns->nodes[0]); // key
2820}
2821
Damiend99b0522013-12-21 18:17:45 +00002822void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002823 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002824 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002825 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002826}
2827
Damiend99b0522013-12-21 18:17:45 +00002828void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002829 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002830 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002831 return;
2832 }
Damiend99b0522013-12-21 18:17:45 +00002833 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002834 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002835 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002836 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2837 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002838 compile_node(comp, pns->nodes[0]);
2839 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002840 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002841 EMIT(yield_from);
2842 } else {
2843 compile_node(comp, pns->nodes[0]);
2844 EMIT(yield_value);
2845 }
2846}
2847
Damiend99b0522013-12-21 18:17:45 +00002848typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002849STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002850 NULL,
2851#define nc NULL
2852#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002853#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002854#include "grammar.h"
2855#undef nc
2856#undef c
2857#undef DEF_RULE
2858};
2859
Damien George6be0b0a2014-08-15 14:30:52 +01002860STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002861 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002862 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002863 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002864 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002865 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002866 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002867 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002868 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002869 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002870 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2871 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2872 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2873 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002874 case MP_PARSE_NODE_TOKEN:
2875 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002876 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002877 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002878 // do nothing
2879 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002880 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002881 }
2882 break;
Damien429d7192013-10-04 19:53:11 +01002883 default: assert(0);
2884 }
2885 } else {
Damiend99b0522013-12-21 18:17:45 +00002886 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002887 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002888 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002889 EMIT_ARG(load_const_str, qstr_from_strn((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]), false);
Damien429d7192013-10-04 19:53:11 +01002890 } else {
Damien George5042bce2014-05-25 22:06:06 +01002891 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2892 if (f == NULL) {
2893 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2894#if MICROPY_DEBUG_PRINTERS
2895 mp_parse_node_print(pn, 0);
2896#endif
2897 compile_syntax_error(comp, pn, "internal compiler error");
2898 } else {
2899 f(comp, pns);
2900 }
Damien429d7192013-10-04 19:53:11 +01002901 }
2902 }
2903}
2904
Damien George6be0b0a2014-08-15 14:30:52 +01002905STATIC void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
Damien429d7192013-10-04 19:53:11 +01002906 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002907 qstr param_name = MP_QSTR_NULL;
2908 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002909 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2910 if (MP_PARSE_NODE_IS_ID(pn)) {
2911 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002912 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002913 // comes after a star, so counts as a keyword-only parameter
2914 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002915 } else {
Damien George2827d622014-04-27 15:50:52 +01002916 // comes before a star, so counts as a positional parameter
2917 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002918 }
Damienb14de212013-10-06 00:28:28 +01002919 } else {
Damiend99b0522013-12-21 18:17:45 +00002920 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2921 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2922 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2923 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002924 //int node_index = 1; unused
2925 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002926 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002927 // this parameter has an annotation
2928 pn_annotation = pns->nodes[1];
2929 }
2930 //node_index = 2; unused
2931 }
2932 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002933 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002934 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002935 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002936 comp->scope_cur->num_dict_params += 1;
2937 } else {
2938 comp->scope_cur->num_default_params += 1;
2939 }
2940 }
2941 */
Damien George8b19db02014-04-11 23:25:34 +01002942 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002943 // comes after a star, so counts as a keyword-only parameter
2944 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002945 } else {
Damien George2827d622014-04-27 15:50:52 +01002946 // comes before a star, so counts as a positional parameter
2947 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002948 }
Damiend99b0522013-12-21 18:17:45 +00002949 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002950 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002951 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002952 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002953 // bare star
2954 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002955 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002956 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002957 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002958 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002959 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2960 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002961 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002962 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002963 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2964 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002965 pn_annotation = pns->nodes[1];
2966 } else {
2967 // shouldn't happen
2968 assert(0);
2969 }
Damiend99b0522013-12-21 18:17:45 +00002970 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2971 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002972 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002973 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002974 // this parameter has an annotation
2975 pn_annotation = pns->nodes[1];
2976 }
Damien George8725f8f2014-02-15 19:33:11 +00002977 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002978 } else {
Damienb14de212013-10-06 00:28:28 +01002979 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002980 assert(0);
2981 }
Damien429d7192013-10-04 19:53:11 +01002982 }
2983
Damien George2827d622014-04-27 15:50:52 +01002984 if (param_name != MP_QSTR_NULL) {
Damiend99b0522013-12-21 18:17:45 +00002985 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002986 // TODO this parameter has an annotation
2987 }
2988 bool added;
2989 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2990 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002991 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002992 return;
2993 }
Damien429d7192013-10-04 19:53:11 +01002994 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002995 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002996 }
2997}
2998
Damien George2827d622014-04-27 15:50:52 +01002999STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01003000 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
3001}
3002
Damien George2827d622014-04-27 15:50:52 +01003003STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01003004 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
3005}
3006
Damien George6be0b0a2014-08-15 14:30:52 +01003007STATIC 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 +01003008 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003009 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003010 // no more nested if/for; compile inner expression
3011 compile_node(comp, pn_inner_expr);
3012 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003013 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003014 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003015 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003016 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003017 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003018 } else {
3019 EMIT(yield_value);
3020 EMIT(pop_top);
3021 }
Damiend99b0522013-12-21 18:17:45 +00003022 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003023 // if condition
Damiend99b0522013-12-21 18:17:45 +00003024 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003025 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3026 pn_iter = pns_comp_if->nodes[1];
3027 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003028 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003029 // for loop
Damiend99b0522013-12-21 18:17:45 +00003030 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003031 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003032 uint l_end2 = comp_next_label(comp);
3033 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003034 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003035 EMIT_ARG(label_assign, l_top2);
3036 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003037 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3038 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 +00003039 EMIT_ARG(jump, l_top2);
3040 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003041 EMIT(for_iter_end);
3042 } else {
3043 // shouldn't happen
3044 assert(0);
3045 }
3046}
3047
Damien George1463c1f2014-04-25 23:52:57 +01003048STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3049#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003050 // see http://www.python.org/dev/peps/pep-0257/
3051
3052 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003053 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003054 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003055 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003056 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003057 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3058 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003059 for (int i = 0; i < num_nodes; i++) {
3060 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003061 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 +00003062 // not a newline, so this is the first statement; finish search
3063 break;
3064 }
3065 }
3066 // 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 +00003067 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003068 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003069 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003070 } else {
3071 return;
3072 }
3073
3074 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003075 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3076 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003077 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3078 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3079 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3080 // compile the doc string
3081 compile_node(comp, pns->nodes[0]);
3082 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003083 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003084 }
3085 }
Damien George1463c1f2014-04-25 23:52:57 +01003086#endif
Damien429d7192013-10-04 19:53:11 +01003087}
3088
Damien George1463c1f2014-04-25 23:52:57 +01003089STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003090 comp->pass = pass;
3091 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003092 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003093 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003094
Damien George36db6bc2014-05-07 17:24:22 +01003095 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003096 // reset maximum stack sizes in scope
3097 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003098 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003099 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003100 }
3101
Damien5ac1b2e2013-10-18 19:58:12 +01003102#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003103 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003104 scope_print_info(scope);
3105 }
Damien5ac1b2e2013-10-18 19:58:12 +01003106#endif
Damien429d7192013-10-04 19:53:11 +01003107
3108 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003109 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3110 assert(scope->kind == SCOPE_MODULE);
3111 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3112 compile_node(comp, pns->nodes[0]); // compile the expression
3113 EMIT(return_value);
3114 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003115 if (!comp->is_repl) {
3116 check_for_doc_string(comp, scope->pn);
3117 }
Damien429d7192013-10-04 19:53:11 +01003118 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003119 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003120 EMIT(return_value);
3121 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003122 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3123 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3124 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003125
3126 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003127 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003128 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003129 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003130 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3131 }
3132
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003133 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003134
3135 compile_node(comp, pns->nodes[3]); // 3 is function body
3136 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003137 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003138 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003139 EMIT(return_value);
3140 }
3141 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003142 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3143 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3144 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003145
3146 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003147 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003148 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003149 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003150 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3151 }
3152
3153 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003154
3155 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3156 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3157 EMIT(pop_top);
3158 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3159 }
Damien429d7192013-10-04 19:53:11 +01003160 EMIT(return_value);
3161 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3162 // a bit of a hack at the moment
3163
Damiend99b0522013-12-21 18:17:45 +00003164 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3165 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3166 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3167 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3168 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003169
Damien George708c0732014-04-27 19:23:46 +01003170 // We need a unique name for the comprehension argument (the iterator).
3171 // CPython uses .0, but we should be able to use anything that won't
3172 // clash with a user defined variable. Best to use an existing qstr,
3173 // so we use the blank qstr.
3174#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003175 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003176#else
3177 qstr qstr_arg = MP_QSTR_;
3178#endif
Damien George36db6bc2014-05-07 17:24:22 +01003179 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003180 bool added;
3181 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3182 assert(added);
3183 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003184 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003185 }
3186
3187 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003188 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003189 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003190 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003191 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003192 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003193 }
3194
Damien George6f355fd2014-04-10 14:11:31 +01003195 uint l_end = comp_next_label(comp);
3196 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003197 EMIT_ARG(load_id, qstr_arg);
3198 EMIT_ARG(label_assign, l_top);
3199 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003200 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3201 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003202 EMIT_ARG(jump, l_top);
3203 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003204 EMIT(for_iter_end);
3205
3206 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003207 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003208 }
3209 EMIT(return_value);
3210 } else {
3211 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003212 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3213 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3214 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003215
Damien George36db6bc2014-05-07 17:24:22 +01003216 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003217 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003218 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003219 assert(added);
3220 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003221 }
3222
Damien Georgeb9791222014-01-23 00:34:21 +00003223 EMIT_ARG(load_id, MP_QSTR___name__);
3224 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003225 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 +00003226 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003227
3228 check_for_doc_string(comp, pns->nodes[2]);
3229 compile_node(comp, pns->nodes[2]); // 2 is class body
3230
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003231 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003232 assert(id != NULL);
3233 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003234 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003235 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003236#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003237 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003238#else
Damien George2bf7c092014-04-09 15:26:46 +01003239 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003240#endif
Damien429d7192013-10-04 19:53:11 +01003241 }
3242 EMIT(return_value);
3243 }
3244
Damien415eb6f2013-10-05 12:19:06 +01003245 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003246
3247 // make sure we match all the exception levels
3248 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003249}
3250
Damien George094d4502014-04-02 17:31:27 +01003251#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003252// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003253STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003254 comp->pass = pass;
3255 comp->scope_cur = scope;
3256 comp->next_label = 1;
3257
3258 if (scope->kind != SCOPE_FUNCTION) {
3259 printf("Error: inline assembler must be a function\n");
3260 return;
3261 }
3262
Damien George36db6bc2014-05-07 17:24:22 +01003263 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003264 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003265 }
3266
Damien826005c2013-10-05 23:17:28 +01003267 // get the function definition parse node
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_funcdef);
Damien826005c2013-10-05 23:17:28 +01003271
Damiend99b0522013-12-21 18:17:45 +00003272 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003273
Damiena2f2f7d2013-10-06 00:14:13 +01003274 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003275 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003276 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003277 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003278 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003279 }
3280
Damiend99b0522013-12-21 18:17:45 +00003281 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003282
Damiend99b0522013-12-21 18:17:45 +00003283 mp_parse_node_t pn_body = pns->nodes[3]; // body
3284 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003285 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3286
Damien Georgecbd2f742014-01-19 11:48:48 +00003287 /*
Damien George36db6bc2014-05-07 17:24:22 +01003288 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003289 //printf("----\n");
3290 scope_print_info(scope);
3291 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003292 */
Damien826005c2013-10-05 23:17:28 +01003293
3294 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003295 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3296 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003297 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3298 // no instructions
3299 continue;
3300 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3301 // an instruction; fall through
3302 } else {
3303 // not an instruction; error
3304 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3305 return;
3306 }
Damiend99b0522013-12-21 18:17:45 +00003307 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3308 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3309 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3310 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3311 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3312 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3313 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3314 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3315 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3316 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003317 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3318
3319 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003320 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003321 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003322 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003323 return;
3324 }
Damien George6f355fd2014-04-10 14:11:31 +01003325 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003326 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003327 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003328 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003329 } else if (op == MP_QSTR_align) {
3330 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3331 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3332 return;
3333 }
Damien George36db6bc2014-05-07 17:24:22 +01003334 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003335 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3336 }
3337 } else if (op == MP_QSTR_data) {
3338 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3339 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3340 return;
3341 }
Damien George36db6bc2014-05-07 17:24:22 +01003342 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003343 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003344 for (uint i = 1; i < n_args; i++) {
3345 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3346 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3347 return;
3348 }
3349 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3350 }
3351 }
Damien826005c2013-10-05 23:17:28 +01003352 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003353 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003354 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003355 }
3356 }
3357 }
3358
Damien George36db6bc2014-05-07 17:24:22 +01003359 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003360 bool success = EMIT_INLINE_ASM(end_pass);
3361 if (!success) {
3362 comp->had_error = true;
3363 }
Damienb05d7072013-10-05 13:37:10 +01003364 }
Damien429d7192013-10-04 19:53:11 +01003365}
Damien George094d4502014-04-02 17:31:27 +01003366#endif
Damien429d7192013-10-04 19:53:11 +01003367
Damien George1463c1f2014-04-25 23:52:57 +01003368STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003369#if !MICROPY_EMIT_CPYTHON
3370 // in Micro Python we put the *x parameter after all other parameters (except **y)
3371 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3372 id_info_t *id_param = NULL;
3373 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3374 id_info_t *id = &scope->id_info[i];
3375 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3376 if (id_param != NULL) {
3377 // swap star param with last param
3378 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3379 }
3380 break;
3381 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3382 id_param = id;
3383 }
3384 }
3385 }
3386#endif
3387
Damien429d7192013-10-04 19:53:11 +01003388 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003389 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003390 scope->num_locals = 0;
3391 for (int i = 0; i < scope->id_info_len; i++) {
3392 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003393 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003394 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3395 continue;
3396 }
3397 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3398 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3399 }
Damien George2827d622014-04-27 15:50:52 +01003400 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003401 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003402 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003403 }
3404 }
3405
3406 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003407#if MICROPY_EMIT_CPYTHON
3408 int num_cell = 0;
3409#endif
Damien9ecbcff2013-12-11 00:41:43 +00003410 for (int i = 0; i < scope->id_info_len; i++) {
3411 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003412#if MICROPY_EMIT_CPYTHON
3413 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003414 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003415 id->local_num = num_cell;
3416 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003417 }
Damien George6baf76e2013-12-30 22:32:17 +00003418#else
3419 // in Micro Python the cells come right after the fast locals
3420 // parameters are not counted here, since they remain at the start
3421 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003422 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003423 id->local_num = scope->num_locals;
3424 scope->num_locals += 1;
3425 }
3426#endif
Damien9ecbcff2013-12-11 00:41:43 +00003427 }
Damien9ecbcff2013-12-11 00:41:43 +00003428
3429 // compute the index of free vars (freevars[idx] in CPython)
3430 // make sure they are in the order of the parent scope
3431 if (scope->parent != NULL) {
3432 int num_free = 0;
3433 for (int i = 0; i < scope->parent->id_info_len; i++) {
3434 id_info_t *id = &scope->parent->id_info[i];
3435 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3436 for (int j = 0; j < scope->id_info_len; j++) {
3437 id_info_t *id2 = &scope->id_info[j];
3438 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003439 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003440#if MICROPY_EMIT_CPYTHON
3441 // in CPython the frees are numbered after the cells
3442 id2->local_num = num_cell + num_free;
3443#else
3444 // in Micro Python the frees come first, before the params
3445 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003446#endif
3447 num_free += 1;
3448 }
3449 }
3450 }
Damien429d7192013-10-04 19:53:11 +01003451 }
Damien George6baf76e2013-12-30 22:32:17 +00003452#if !MICROPY_EMIT_CPYTHON
3453 // in Micro Python shift all other locals after the free locals
3454 if (num_free > 0) {
3455 for (int i = 0; i < scope->id_info_len; i++) {
3456 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003457 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003458 id->local_num += num_free;
3459 }
3460 }
Damien George2827d622014-04-27 15:50:52 +01003461 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 +00003462 scope->num_locals += num_free;
3463 }
3464#endif
Damien429d7192013-10-04 19:53:11 +01003465 }
3466
Damien George8725f8f2014-02-15 19:33:11 +00003467 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003468
3469#if MICROPY_EMIT_CPYTHON
3470 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003471 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) {
3472 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003473 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003474 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003475 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 +00003476 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003477 }
3478 }
Damien George882b3632014-04-02 15:56:31 +01003479#endif
3480
Damien429d7192013-10-04 19:53:11 +01003481 int num_free = 0;
3482 for (int i = 0; i < scope->id_info_len; i++) {
3483 id_info_t *id = &scope->id_info[i];
3484 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3485 num_free += 1;
3486 }
3487 }
3488 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003489 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003490 }
3491}
3492
Damien George65cad122014-04-06 11:48:15 +01003493mp_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 +01003494 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003495 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003496 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003497
Damien826005c2013-10-05 23:17:28 +01003498 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003499 mp_map_t consts;
3500 mp_map_init(&consts, 0);
3501 pn = fold_constants(comp, pn, &consts);
3502 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003503
3504 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003505 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003506
Damien826005c2013-10-05 23:17:28 +01003507 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003508 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003509 comp->emit_method_table = &emit_pass1_method_table;
3510 comp->emit_inline_asm = NULL;
3511 comp->emit_inline_asm_method_table = NULL;
3512 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003513 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003514 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003515#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003516 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003517 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003518#endif
Damien826005c2013-10-05 23:17:28 +01003519 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003520 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003521 }
3522
3523 // update maximim number of labels needed
3524 if (comp->next_label > max_num_labels) {
3525 max_num_labels = comp->next_label;
3526 }
Damien429d7192013-10-04 19:53:11 +01003527 }
3528
Damien826005c2013-10-05 23:17:28 +01003529 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003530 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003531 compile_scope_compute_things(comp, s);
3532 }
3533
Damien826005c2013-10-05 23:17:28 +01003534 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003535 emit_pass1_free(comp->emit);
3536
Damien826005c2013-10-05 23:17:28 +01003537 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003538#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003539 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003540#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003541 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003542#endif
Damien3ef4abb2013-10-12 16:53:13 +01003543#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003544 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003545#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003546#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003547 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003548 if (false) {
3549 // dummy
3550
Damien3ef4abb2013-10-12 16:53:13 +01003551#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003552 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003553 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003554 if (emit_inline_thumb == NULL) {
3555 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3556 }
3557 comp->emit = NULL;
3558 comp->emit_method_table = NULL;
3559 comp->emit_inline_asm = emit_inline_thumb;
3560 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003561 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea26dc502014-04-12 17:54:52 +01003562 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003563 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003564 }
Damienc025ebb2013-10-12 14:30:21 +01003565#endif
3566
Damien826005c2013-10-05 23:17:28 +01003567 } else {
Damienc025ebb2013-10-12 14:30:21 +01003568
3569 // choose the emit type
3570
Damien3ef4abb2013-10-12 16:53:13 +01003571#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003572 comp->emit = emit_cpython_new(max_num_labels);
3573 comp->emit_method_table = &emit_cpython_method_table;
3574#else
Damien826005c2013-10-05 23:17:28 +01003575 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003576
3577#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003578 case MP_EMIT_OPT_NATIVE_PYTHON:
3579 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003580#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003581 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003582 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003583 }
Damien13ed3a62013-10-08 09:05:10 +01003584 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003585#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003586 if (emit_native == NULL) {
3587 emit_native = emit_native_thumb_new(max_num_labels);
3588 }
3589 comp->emit_method_table = &emit_native_thumb_method_table;
3590#endif
3591 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003592 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien George36db6bc2014-05-07 17:24:22 +01003593
3594 // native emitters need an extra pass to compute stack size
3595 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3596
Damien7af3d192013-10-07 00:02:49 +01003597 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003598#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003599
Damien826005c2013-10-05 23:17:28 +01003600 default:
3601 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003602 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003603 }
3604 comp->emit = emit_bc;
3605 comp->emit_method_table = &emit_bc_method_table;
3606 break;
3607 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003608#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003609
Damien George36db6bc2014-05-07 17:24:22 +01003610 // second last pass: compute code size
Damien Georgea26dc502014-04-12 17:54:52 +01003611 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003612 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3613 }
3614
3615 // final pass: emit code
3616 if (!comp->had_error) {
3617 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003618 }
Damien6cdd3af2013-10-05 18:08:26 +01003619 }
Damien429d7192013-10-04 19:53:11 +01003620 }
3621
Damien George41d02b62014-01-24 22:42:28 +00003622 // free the emitters
3623#if !MICROPY_EMIT_CPYTHON
3624 if (emit_bc != NULL) {
3625 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003626 }
Damien George41d02b62014-01-24 22:42:28 +00003627#if MICROPY_EMIT_NATIVE
3628 if (emit_native != NULL) {
3629#if MICROPY_EMIT_X64
3630 emit_native_x64_free(emit_native);
3631#elif MICROPY_EMIT_THUMB
3632 emit_native_thumb_free(emit_native);
3633#endif
3634 }
3635#endif
3636#if MICROPY_EMIT_INLINE_THUMB
3637 if (emit_inline_thumb != NULL) {
3638 emit_inline_thumb_free(emit_inline_thumb);
3639 }
3640#endif
3641#endif // !MICROPY_EMIT_CPYTHON
3642
3643 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003644 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003645 for (scope_t *s = module_scope; s;) {
3646 scope_t *next = s->next;
3647 scope_free(s);
3648 s = next;
3649 }
Damien5ac1b2e2013-10-18 19:58:12 +01003650
Damien George41d02b62014-01-24 22:42:28 +00003651 // free the compiler
3652 bool had_error = comp->had_error;
3653 m_del_obj(compiler_t, comp);
3654
Damien George1fb03172014-01-03 14:22:03 +00003655 if (had_error) {
3656 // TODO return a proper error message
3657 return mp_const_none;
3658 } else {
3659#if MICROPY_EMIT_CPYTHON
3660 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003661 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003662 return mp_const_true;
3663#else
3664 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003665 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003666#endif
3667 }
Damien429d7192013-10-04 19:53:11 +01003668}