blob: a41851c3a142a2575f38a5da6b5bd88b1ba7216a [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>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
36#include "py/smallint.h"
37#include "py/runtime.h"
38#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010039
40// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien Georgeb9791222014-01-23 00:34:21 +000052#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
53#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000054#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
55#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien Georgeb9791222014-01-23 00:34:21 +000056#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
57#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 +010058
Damien Georgea91ac202014-10-05 19:01:34 +010059// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010060typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000061 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010062
Damien George78035b92014-04-09 12:27:39 +010063 uint8_t is_repl;
64 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010065 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010066 uint8_t have_star;
67
68 // try to keep compiler clean from nlr
69 // this is set to an exception object if we have a compile error
70 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010071
Damien George6f355fd2014-04-10 14:11:31 +010072 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010073
Damien George69b89d22014-04-11 13:38:30 +000074 uint16_t num_dict_params;
75 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010076
Damien Georgea91ac202014-10-05 19:01:34 +010077 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
78 uint16_t continue_label;
79 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000080 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010081
Damien429d7192013-10-04 19:53:11 +010082 scope_t *scope_head;
83 scope_t *scope_cur;
84
Damien6cdd3af2013-10-05 18:08:26 +010085 emit_t *emit; // current emitter
86 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010087
Damien Georgea77ffe62015-03-14 12:59:31 +000088 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +010089 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
90 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien Georgea77ffe62015-03-14 12:59:31 +000091 #endif
Damien429d7192013-10-04 19:53:11 +010092} compiler_t;
93
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010094STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010095 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
96 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010097 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George8dfbd2d2015-02-13 01:00:51 +000098 mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010099 } else {
Damien Georgea91ac202014-10-05 19:01:34 +0100100 // we don't have a line number, so just pass 0
Damien George8dfbd2d2015-02-13 01:00:51 +0000101 mp_obj_exception_add_traceback(exc, comp->source_file, 0, comp->scope_cur->simple_name);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100102 }
Damien Georgea91ac202014-10-05 19:01:34 +0100103 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000104}
105
Damien Georgeddd1e182015-01-10 14:07:24 +0000106#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100107STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300108 #if MICROPY_PY_UCTYPES
109 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
110 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100111 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100112 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100113};
Damien Georgeddd1e182015-01-10 14:07:24 +0000114STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
115#endif
Damien George57e99eb2014-04-10 22:42:11 +0100116
Damien Georgeffae48d2014-05-08 15:58:39 +0000117// this function is essentially a simple preprocessor
118STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
119 if (0) {
120 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100121#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000122 } else if (MP_PARSE_NODE_IS_ID(pn)) {
123 // lookup identifier in table of dynamic constants
124 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
125 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
126 if (elem != NULL) {
127 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
128 }
129#endif
130 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000131 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100132
Damien Georgeffae48d2014-05-08 15:58:39 +0000133 // fold some parse nodes before folding their arguments
134 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100135#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000136 case PN_expr_stmt:
137 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
138 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
139 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
140 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
141 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
142 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
143 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
144 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
145 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
146 ) {
147 // code to assign dynamic constants: id = const(value)
148
149 // get the id
150 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
151
152 // get the value
153 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
154 pn_value = fold_constants(comp, pn_value, consts);
155 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
156 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
157 break;
158 }
Damien George40f3c022014-07-03 13:25:24 +0100159 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000160
161 // store the value in the table of dynamic constants
162 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
163 if (elem->value != MP_OBJ_NULL) {
164 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
165 break;
166 }
167 elem->value = MP_OBJ_NEW_SMALL_INT(value);
168
169 // replace const(value) with value
170 pns1->nodes[0] = pn_value;
171
172 // finished folding this assignment
173 return pn;
174 }
175 }
176 }
177 break;
178#endif
Damien George5042bce2014-05-25 22:06:06 +0100179 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000180 case PN_bytes:
Damien George7d414a12015-02-08 01:57:40 +0000181 case PN_const_object:
Damien George5042bce2014-05-25 22:06:06 +0100182 return pn;
Damien429d7192013-10-04 19:53:11 +0100183 }
184
Damien Georgeffae48d2014-05-08 15:58:39 +0000185 // fold arguments
186 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
187 for (int i = 0; i < n; i++) {
188 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
189 }
190
191 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000192 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100193 case PN_atom_paren:
194 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
195 // (int)
196 pn = pns->nodes[0];
197 }
198 break;
199
200 case PN_expr:
201 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
202 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100203 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
204 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100205 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
206 }
207 break;
208
209 case PN_and_expr:
210 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
211 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100212 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
213 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100214 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
215 }
216 break;
217
Damien429d7192013-10-04 19:53:11 +0100218 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000219 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 +0100220 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
221 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000222 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100223 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000224 if (!(arg1 >= (mp_int_t)BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
Damien Georgea26dc502014-04-12 17:54:52 +0100225 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
226 }
Damien George0bb97132015-02-27 14:25:47 +0000227 } else {
228 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100229 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000230 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200231 // Shifting to big amounts is underfined behavior
232 // in C and is CPU-dependent; propagate sign bit.
233 arg1 = BITS_PER_WORD - 1;
234 }
Damiend99b0522013-12-21 18:17:45 +0000235 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100236 }
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;
Damien George0bb97132015-02-27 14:25:47 +0000248 } else {
249 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
Damien Georgea26dc502014-04-12 17:54:52 +0100250 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000251 arg0 -= arg1;
Damien0efb3a12013-10-12 16:16:56 +0100252 }
Damien Georged1e355e2014-05-28 14:51:12 +0100253 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100254 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000255 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100256 }
257 }
258 break;
259
260 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000261 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100262 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
263 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000264 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100265 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000266 if (!mp_small_int_mul_overflow(arg0, arg1)) {
267 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100268 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000269 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
270 }
271 }
Damiend99b0522013-12-21 18:17:45 +0000272 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100273 // int / int
274 // pass
Damiend99b0522013-12-21 18:17:45 +0000275 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100276 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000277 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damien George0bb97132015-02-27 14:25:47 +0000278 } else {
279 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300280 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100281 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000282 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 +0300283 }
Damien429d7192013-10-04 19:53:11 +0100284 }
285 }
286 break;
287
288 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000289 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100290 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000291 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100292 // +int
Damiend99b0522013-12-21 18:17:45 +0000293 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
294 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100295 // -int
Damiend99b0522013-12-21 18:17:45 +0000296 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
Damien George0bb97132015-02-27 14:25:47 +0000297 } else {
298 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
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);
Damien429d7192013-10-04 19:53:11 +0100301 }
302 }
303 break;
304
305 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100306 if (0) {
307#if MICROPY_EMIT_CPYTHON
308 } 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 +0100309 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100310 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000311 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
312 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200313 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100314 if (power >= 0) {
315 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200316 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100317 for (; power > 0; power--) {
318 ans *= base;
319 }
Damiend99b0522013-12-21 18:17:45 +0000320 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100321 }
322 }
Damien George57e99eb2014-04-10 22:42:11 +0100323#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000324#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100325 } 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])) {
326 // id.id
327 // look it up in constant table, see if it can be replaced with an integer
328 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
329 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
330 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
331 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
332 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
333 if (elem != NULL) {
334 mp_obj_t dest[2];
335 mp_load_method_maybe(elem->value, q_attr, dest);
336 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100337 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000338 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100339 }
340 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000341#endif
Damien429d7192013-10-04 19:53:11 +0100342 }
343 break;
344 }
345 }
346
347 return pn;
348}
349
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200350STATIC 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 +0100351STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000352STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100353
Damien George6f355fd2014-04-10 14:11:31 +0100354STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100355 return comp->next_label++;
356}
357
Damien George8dcc0c72014-03-27 10:55:21 +0000358STATIC void compile_increase_except_level(compiler_t *comp) {
359 comp->cur_except_level += 1;
360 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
361 comp->scope_cur->exc_stack_size = comp->cur_except_level;
362 }
363}
364
365STATIC void compile_decrease_except_level(compiler_t *comp) {
366 assert(comp->cur_except_level > 0);
367 comp->cur_except_level -= 1;
368}
369
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200370STATIC 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 +0100371 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100372 scope->parent = comp->scope_cur;
373 scope->next = NULL;
374 if (comp->scope_head == NULL) {
375 comp->scope_head = scope;
376 } else {
377 scope_t *s = comp->scope_head;
378 while (s->next != NULL) {
379 s = s->next;
380 }
381 s->next = scope;
382 }
383 return scope;
384}
385
Damien George963a5a32015-01-16 17:47:07 +0000386STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
387 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000388 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
389 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100390 for (int i = 0; i < num_nodes; i++) {
391 f(comp, pns->nodes[i]);
392 }
Damiend99b0522013-12-21 18:17:45 +0000393 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100394 f(comp, pn);
395 }
396}
397
Damien George969a6b32014-12-10 22:07:04 +0000398STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000399 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100400 for (int i = 0; i < num_nodes; i++) {
401 compile_node(comp, pns->nodes[i]);
402 }
403}
404
Damien George542bd6b2015-03-26 14:42:40 +0000405STATIC void compile_load_id(compiler_t *comp, qstr qst) {
406 if (comp->pass == MP_PASS_SCOPE) {
407 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
408 } else {
409 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
410 }
411}
412
413STATIC void compile_store_id(compiler_t *comp, qstr qst) {
414 if (comp->pass == MP_PASS_SCOPE) {
415 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
416 } else {
417 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
418 }
419}
420
421STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
422 if (comp->pass == MP_PASS_SCOPE) {
423 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
424 } else {
425 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
426 }
427}
428
Damien3ef4abb2013-10-12 16:53:13 +0100429#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200430STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100431 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
432 return true;
433 }
Damien George4c81ba82015-01-13 16:21:23 +0000434 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
435 return true;
436 }
Damien George7d414a12015-02-08 01:57:40 +0000437 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
438 return true;
439 }
Damiend99b0522013-12-21 18:17:45 +0000440 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100441 return false;
442 }
Damiend99b0522013-12-21 18:17:45 +0000443 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100444 return false;
445 }
446 return true;
447}
448
Damien George5042bce2014-05-25 22:06:06 +0100449STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000450 bool has_single_quote = false;
451 bool has_double_quote = false;
452 for (int i = 0; i < len; i++) {
453 if (str[i] == '\'') {
454 has_single_quote = true;
455 } else if (str[i] == '"') {
456 has_double_quote = true;
457 }
458 }
459 if (bytes) {
460 vstr_printf(vstr, "b");
461 }
462 bool quote_single = false;
463 if (has_single_quote && !has_double_quote) {
464 vstr_printf(vstr, "\"");
465 } else {
466 quote_single = true;
467 vstr_printf(vstr, "'");
468 }
469 for (int i = 0; i < len; i++) {
470 if (str[i] == '\n') {
471 vstr_printf(vstr, "\\n");
472 } else if (str[i] == '\\') {
473 vstr_printf(vstr, "\\\\");
474 } else if (str[i] == '\'' && quote_single) {
475 vstr_printf(vstr, "\\'");
476 } else {
477 vstr_printf(vstr, "%c", str[i]);
478 }
479 }
480 if (has_single_quote && !has_double_quote) {
481 vstr_printf(vstr, "\"");
482 } else {
483 vstr_printf(vstr, "'");
484 }
485}
486
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200487STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000488 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string) || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
Damien George5042bce2014-05-25 22:06:06 +0100489 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000490 cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes));
Damien George5042bce2014-05-25 22:06:06 +0100491 return;
492 }
493
Damien George7d414a12015-02-08 01:57:40 +0000494 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
495 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
496 mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
497 return;
498 }
499
Damiend99b0522013-12-21 18:17:45 +0000500 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200501 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
502 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
503 return;
504 }
505
Damien George42f3de92014-10-03 17:44:14 +0000506 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000507 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
508 case MP_PARSE_NODE_ID: assert(0);
Damien George5042bce2014-05-25 22:06:06 +0100509 case MP_PARSE_NODE_STRING:
510 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100511 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100512 const byte *str = qstr_data(arg, &len);
513 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
514 break;
515 }
Damiend99b0522013-12-21 18:17:45 +0000516 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100517 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000518 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
519 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
520 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100521 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100522 }
523 break;
524 default: assert(0);
525 }
526}
527
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200528STATIC void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100529 int n = 0;
530 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000531 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100532 }
533 int total = n;
534 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000535 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100536 total += 1;
Damien3a205172013-10-12 15:01:56 +0100537 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100538 is_const = false;
539 }
540 }
541 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100542 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100543 is_const = false;
544 break;
545 }
546 }
547 if (total > 0 && is_const) {
548 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000549 vstr_t *vstr = vstr_new();
550 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000551 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000552 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100553 need_comma = true;
554 }
555 for (int i = 0; i < n; i++) {
556 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000557 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100558 }
Damien02f89412013-12-12 15:13:36 +0000559 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100560 need_comma = true;
561 }
562 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000563 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100564 } else {
Damien02f89412013-12-12 15:13:36 +0000565 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100566 }
Damien George0d3cb672015-01-28 23:43:01 +0000567 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000568 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100569 } else {
Damiend99b0522013-12-21 18:17:45 +0000570 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100571 compile_node(comp, pn);
572 }
573 for (int i = 0; i < n; i++) {
574 compile_node(comp, pns_list->nodes[i]);
575 }
Damien Georgeb9791222014-01-23 00:34:21 +0000576 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100577 }
578}
Damien3a205172013-10-12 15:01:56 +0100579#endif
580
581// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100582STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100583#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100584 cpython_c_tuple(comp, pn, pns_list);
585#else
586 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000587 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100588 compile_node(comp, pn);
589 total += 1;
590 }
591 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000592 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100593 for (int i = 0; i < n; i++) {
594 compile_node(comp, pns_list->nodes[i]);
595 }
596 total += n;
597 }
Damien Georgeb9791222014-01-23 00:34:21 +0000598 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100599#endif
600}
Damien429d7192013-10-04 19:53:11 +0100601
Damien George969a6b32014-12-10 22:07:04 +0000602STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100603 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000604 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100605}
606
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200607STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000608 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
609 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100610}
611
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200612STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000613 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
614 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100615}
616
Damien3ef4abb2013-10-12 16:53:13 +0100617#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100618// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200619STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100620 if (node_is_const_false(pn)) {
621 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000622 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100623 }
624 return;
625 } else if (node_is_const_true(pn)) {
626 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000627 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100628 }
629 return;
Damiend99b0522013-12-21 18:17:45 +0000630 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
631 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
632 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
633 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100634 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100635 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100636 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100637 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100638 }
Damien3a205172013-10-12 15:01:56 +0100639 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000640 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100641 } else {
642 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100643 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100644 }
645 }
646 return;
Damiend99b0522013-12-21 18:17:45 +0000647 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100648 if (jump_if == false) {
649 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100650 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100651 }
652 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100653 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100654 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100655 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100656 }
Damien3a205172013-10-12 15:01:56 +0100657 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000658 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100659 }
660 return;
Damiend99b0522013-12-21 18:17:45 +0000661 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100662 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100663 return;
664 }
665 }
666
667 // nothing special, fall back to default compiling for node and jump
668 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000669 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100670}
Damien3a205172013-10-12 15:01:56 +0100671#endif
Damien429d7192013-10-04 19:53:11 +0100672
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200673STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100674#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100675 cpython_c_if_cond(comp, pn, jump_if, label, false);
676#else
677 if (node_is_const_false(pn)) {
678 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000679 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100680 }
681 return;
682 } else if (node_is_const_true(pn)) {
683 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000684 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100685 }
686 return;
Damiend99b0522013-12-21 18:17:45 +0000687 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
688 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
689 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
690 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100691 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000692 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100693 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100694 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000695 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100696 }
Damien George0b2fd912015-02-28 14:37:54 +0000697 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000698 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100699 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000700 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100701 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000702 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100703 }
704 }
705 return;
Damiend99b0522013-12-21 18:17:45 +0000706 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100707 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000708 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100709 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000710 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100711 }
Damiend99b0522013-12-21 18:17:45 +0000712 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100713 c_if_cond(comp, pns->nodes[0], !jump_if, label);
714 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100715 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
716 // cond is something in parenthesis
717 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
718 // empty tuple, acts as false for the condition
719 if (jump_if == false) {
720 EMIT_ARG(jump, label);
721 }
722 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
723 // non-empty tuple, acts as true for the condition
724 if (jump_if == true) {
725 EMIT_ARG(jump, label);
726 }
727 } else {
728 // parenthesis around 1 item, is just that item
729 c_if_cond(comp, pns->nodes[0], jump_if, label);
730 }
731 return;
Damien3a205172013-10-12 15:01:56 +0100732 }
733 }
734
735 // nothing special, fall back to default compiling for node and jump
736 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000737 EMIT_ARG(pop_jump_if, jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100738#endif
Damien429d7192013-10-04 19:53:11 +0100739}
740
741typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100742STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100743
Damien George6be0b0a2014-08-15 14:30:52 +0100744STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100745 if (assign_kind != ASSIGN_AUG_STORE) {
746 compile_node(comp, pns->nodes[0]);
747 }
748
Damiend99b0522013-12-21 18:17:45 +0000749 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
750 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
751 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
752 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100753 if (assign_kind != ASSIGN_AUG_STORE) {
754 for (int i = 0; i < n - 1; i++) {
755 compile_node(comp, pns1->nodes[i]);
756 }
757 }
Damiend99b0522013-12-21 18:17:45 +0000758 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
759 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100760 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000761 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100762 if (assign_kind == ASSIGN_AUG_STORE) {
763 EMIT(rot_three);
764 EMIT(store_subscr);
765 } else {
766 compile_node(comp, pns1->nodes[0]);
767 if (assign_kind == ASSIGN_AUG_LOAD) {
768 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100769 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100770 } else {
771 EMIT(store_subscr);
772 }
773 }
Damiend99b0522013-12-21 18:17:45 +0000774 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
775 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100776 if (assign_kind == ASSIGN_AUG_LOAD) {
777 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000778 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100779 } else {
780 if (assign_kind == ASSIGN_AUG_STORE) {
781 EMIT(rot_two);
782 }
Damien Georgeb9791222014-01-23 00:34:21 +0000783 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100784 }
785 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100786 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100787 }
788 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100789 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100790 }
791
Damiend99b0522013-12-21 18:17:45 +0000792 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100793 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100794 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100795
796 return;
797
798cannot_assign:
799 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100800}
801
Damien George0288cf02014-04-11 11:53:00 +0000802// 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 +0100803STATIC 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 +0000804 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
805
806 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000807 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000808 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
809 EMIT_ARG(unpack_ex, 0, num_tail);
810 have_star_index = 0;
811 }
Damien George963a5a32015-01-16 17:47:07 +0000812 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000813 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000814 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000815 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
816 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100817 } else {
Damien George9d181f62014-04-27 16:55:27 +0100818 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100819 return;
820 }
821 }
822 }
Damien George963a5a32015-01-16 17:47:07 +0000823 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000824 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100825 }
Damien George0288cf02014-04-11 11:53:00 +0000826 if (num_head != 0) {
827 if (0 == have_star_index) {
828 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100829 } else {
Damien George0288cf02014-04-11 11:53:00 +0000830 c_assign(comp, node_head, ASSIGN_STORE);
831 }
832 }
Damien George963a5a32015-01-16 17:47:07 +0000833 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000834 if (num_head + i == have_star_index) {
835 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
836 } else {
837 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100838 }
839 }
840}
841
842// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100843STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100844 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000845 assert(!MP_PARSE_NODE_IS_NULL(pn));
846 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000847 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000848 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100849 switch (assign_kind) {
850 case ASSIGN_STORE:
851 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000852 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100853 break;
854 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000855 default:
Damien George542bd6b2015-03-26 14:42:40 +0000856 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100857 break;
858 }
859 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100860 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100861 return;
862 }
863 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000864 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000865 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
866 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100867 case PN_power:
868 // lhs is an index or attribute
869 c_assign_power(comp, pns, assign_kind);
870 break;
871
872 case PN_testlist_star_expr:
873 case PN_exprlist:
874 // lhs is a tuple
875 if (assign_kind != ASSIGN_STORE) {
876 goto bad_aug;
877 }
Damien George0288cf02014-04-11 11:53:00 +0000878 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100879 break;
880
881 case PN_atom_paren:
882 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000883 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100884 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100885 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000886 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000887 if (assign_kind != ASSIGN_STORE) {
888 goto bad_aug;
889 }
Damiend99b0522013-12-21 18:17:45 +0000890 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100891 goto testlist_comp;
892 } else {
893 // parenthesis around 1 item, is just that item
894 pn = pns->nodes[0];
895 goto tail_recursion;
896 }
897 break;
898
899 case PN_atom_bracket:
900 // lhs is something in brackets
901 if (assign_kind != ASSIGN_STORE) {
902 goto bad_aug;
903 }
Damiend99b0522013-12-21 18:17:45 +0000904 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100905 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000906 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000907 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
908 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100909 goto testlist_comp;
910 } else {
911 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000912 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100913 }
914 break;
915
916 default:
Damien George9d181f62014-04-27 16:55:27 +0100917 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100918 }
919 return;
920
921 testlist_comp:
922 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000923 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
924 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
925 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100926 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000927 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000928 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000929 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100930 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000931 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
932 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000933 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100934 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100935 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100936 } else {
937 // sequence with 2 items
938 goto sequence_with_2_items;
939 }
940 } else {
941 // sequence with 2 items
942 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000943 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100944 }
945 return;
946 }
947 return;
948
Damien George9d181f62014-04-27 16:55:27 +0100949 cannot_assign:
950 compile_syntax_error(comp, pn, "can't assign to expression");
951 return;
952
Damien429d7192013-10-04 19:53:11 +0100953 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100954 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100955}
956
957// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100958// if we are not in CPython compatibility mode then:
959// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
960// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
961// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100962STATIC 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 +0100963 assert(n_pos_defaults >= 0);
964 assert(n_kw_defaults >= 0);
965
Damien429d7192013-10-04 19:53:11 +0100966 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000967 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100968 int nfree = 0;
969 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000970 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
971 id_info_t *id = &comp->scope_cur->id_info[i];
972 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
973 for (int j = 0; j < this_scope->id_info_len; j++) {
974 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100975 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000976#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100977 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000978#else
979 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000980 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000981#endif
Damien318aec62013-12-10 18:28:17 +0000982 nfree += 1;
983 }
984 }
Damien429d7192013-10-04 19:53:11 +0100985 }
986 }
987 }
Damien429d7192013-10-04 19:53:11 +0100988
989 // make the function/closure
990 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100991 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100992 } else {
Damien George3558f622014-04-20 17:50:40 +0100993 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100994 }
995}
996
Damien George6be0b0a2014-08-15 14:30:52 +0100997STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000998 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100999 comp->have_star = true;
1000 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001001 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1002 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001003 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001004 } else {
1005 // named star
Damien429d7192013-10-04 19:53:11 +01001006 }
Damien George8b19db02014-04-11 23:25:34 +01001007 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001008
1009 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001010 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001011 // TODO do we need to do anything with this?
1012
1013 } else {
1014 mp_parse_node_t pn_id;
1015 mp_parse_node_t pn_colon;
1016 mp_parse_node_t pn_equal;
1017 if (MP_PARSE_NODE_IS_ID(pn)) {
1018 // this parameter is just an id
1019
1020 pn_id = pn;
1021 pn_colon = MP_PARSE_NODE_NULL;
1022 pn_equal = MP_PARSE_NODE_NULL;
1023
Damien George0bb97132015-02-27 14:25:47 +00001024 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001025 // this parameter has a colon and/or equal specifier
1026
Damien George0bb97132015-02-27 14:25:47 +00001027 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1028
Damien Georgef41fdd02014-03-03 23:19:11 +00001029 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1030 pn_id = pns->nodes[0];
1031 pn_colon = pns->nodes[1];
1032 pn_equal = pns->nodes[2];
Damien Georgef41fdd02014-03-03 23:19:11 +00001033 }
1034
1035 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1036 // this parameter does not have a default value
1037
1038 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001039 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001040 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001041 return;
1042 }
1043
1044 } else {
1045 // this parameter has a default value
1046 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1047
Damien George8b19db02014-04-11 23:25:34 +01001048 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001049 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001050#if MICROPY_EMIT_CPYTHON
1051 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1052 compile_node(comp, pn_equal);
1053#else
Damien George69b89d22014-04-11 13:38:30 +00001054 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1055 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001056 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1057 // we need to do this here before we start building the map for the default keywords
1058 if (comp->num_default_params > 0) {
1059 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001060 } else {
1061 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001062 }
Damien George69b89d22014-04-11 13:38:30 +00001063 // first default dict param, so make the map
1064 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001065 }
Damien Georgef0778a72014-06-07 22:01:00 +01001066
1067 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001068 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001069 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001070 EMIT(store_map);
1071#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001072 } else {
Damien George69b89d22014-04-11 13:38:30 +00001073 comp->num_default_params += 1;
1074 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001075 }
1076 }
1077
1078 // TODO pn_colon not implemented
1079 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001080 }
1081}
1082
1083// leaves function object on stack
1084// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001085STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001086 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001087 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001088 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001089 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001090 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001091 }
1092
1093 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001094 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001095 uint old_num_dict_params = comp->num_dict_params;
1096 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001097
1098 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001099 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001100 comp->num_dict_params = 0;
1101 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001102 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001103
Damien Georgea91ac202014-10-05 19:01:34 +01001104 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001105 return MP_QSTR_NULL;
1106 }
1107
Damien Georgee337f1e2014-03-31 15:18:37 +01001108#if !MICROPY_EMIT_CPYTHON
1109 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001110 // the default keywords args may have already made the tuple; if not, do it now
1111 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001112 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001113 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001114 }
1115#endif
1116
Damien429d7192013-10-04 19:53:11 +01001117 // get the scope for this function
1118 scope_t *fscope = (scope_t*)pns->nodes[4];
1119
1120 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001121 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001122
1123 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001124 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001125 comp->num_dict_params = old_num_dict_params;
1126 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001127
1128 // return its name (the 'f' in "def f(...):")
1129 return fscope->simple_name;
1130}
1131
1132// leaves class object on stack
1133// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001134STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001135 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001136 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001137 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001138 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001139 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001140 }
1141
1142 EMIT(load_build_class);
1143
1144 // scope for this class
1145 scope_t *cscope = (scope_t*)pns->nodes[3];
1146
1147 // compile the class
1148 close_over_variables_etc(comp, cscope, 0, 0);
1149
1150 // get its name
Damien George968bf342014-04-27 19:12:05 +01001151 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001152
1153 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001154 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1155 mp_parse_node_t parents = pns->nodes[1];
1156 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1157 parents = MP_PARSE_NODE_NULL;
1158 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001159 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001160 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001161
1162 // return its name (the 'C' in class C(...):")
1163 return cscope->simple_name;
1164}
1165
Damien6cdd3af2013-10-05 18:08:26 +01001166// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001167STATIC 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 +00001168 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001169 return false;
1170 }
1171
1172 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001173 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001174 return true;
1175 }
1176
Damiend99b0522013-12-21 18:17:45 +00001177 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001178 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001179 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001180#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001181 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001182 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001183 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001184 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001185#endif
Damien3ef4abb2013-10-12 16:53:13 +01001186#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001187 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001188 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001189#endif
Damien6cdd3af2013-10-05 18:08:26 +01001190 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001191 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001192 }
1193
1194 return true;
1195}
1196
Damien George969a6b32014-12-10 22:07:04 +00001197STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001198 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001199 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001200 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +01001201
Damien6cdd3af2013-10-05 18:08:26 +01001202 // inherit emit options for this function/class definition
1203 uint emit_options = comp->scope_cur->emit_options;
1204
1205 // compile each decorator
1206 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001207 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001208 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1209 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001210
1211 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001212 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001213 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +01001214
1215 // check for built-in decorators
1216 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1217 // this was a built-in
1218 num_built_in_decorators += 1;
1219
1220 } else {
1221 // not a built-in, compile normally
1222
1223 // compile the decorator function
1224 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001225 for (int j = 1; j < name_len; j++) {
1226 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1227 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001228 }
1229
1230 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001231 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001232 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001233 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001234 compile_node(comp, pns_decorator->nodes[1]);
1235 }
Damien429d7192013-10-04 19:53:11 +01001236 }
1237 }
1238
1239 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001240 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001241 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001242 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001243 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001244 } else {
Damien George0bb97132015-02-27 14:25:47 +00001245 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1246 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001247 }
1248
1249 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001250 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001251 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001252 }
1253
1254 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +00001255 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +01001256}
1257
Damien George969a6b32014-12-10 22:07:04 +00001258STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001259 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001260 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +00001261 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +01001262}
1263
Damien George6be0b0a2014-08-15 14:30:52 +01001264STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001265 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00001266 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001267 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1268 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001269
1270 compile_node(comp, pns->nodes[0]); // base of the power node
1271
Damiend99b0522013-12-21 18:17:45 +00001272 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1273 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1274 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1275 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001276 for (int i = 0; i < n - 1; i++) {
1277 compile_node(comp, pns1->nodes[i]);
1278 }
Damiend99b0522013-12-21 18:17:45 +00001279 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1280 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001281 }
Damien Georgeaedf5832015-03-25 22:06:47 +00001282 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001283 compile_node(comp, pns1->nodes[0]);
1284 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001285 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1286 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001287 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001288 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001289 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001290 }
1291 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001292 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001293 }
1294
Damiend99b0522013-12-21 18:17:45 +00001295 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001296 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001297 }
Damiend99b0522013-12-21 18:17:45 +00001298 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1299 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1300 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1301 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001302 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1303
Damiend99b0522013-12-21 18:17:45 +00001304 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1305 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1306 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001307 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001308 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001309 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001310 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001311 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001312 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001313 c_del_stmt(comp, pns->nodes[0]);
1314 for (int i = 0; i < n; i++) {
1315 c_del_stmt(comp, pns1->nodes[i]);
1316 }
Damiend99b0522013-12-21 18:17:45 +00001317 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001318 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001319 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001320 } else {
1321 // sequence with 2 items
1322 goto sequence_with_2_items;
1323 }
1324 } else {
1325 // sequence with 2 items
1326 sequence_with_2_items:
1327 c_del_stmt(comp, pns->nodes[0]);
1328 c_del_stmt(comp, pns->nodes[1]);
1329 }
1330 } else {
1331 // tuple with 1 element
1332 c_del_stmt(comp, pn);
1333 }
1334 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001335 // TODO is there anything else to implement?
1336 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001337 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001338
1339 return;
1340
1341cannot_delete:
1342 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001343}
1344
Damien George969a6b32014-12-10 22:07:04 +00001345STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001346 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1347}
1348
Damien George969a6b32014-12-10 22:07:04 +00001349STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001350 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001351 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001352 }
Damien George090c9232014-10-17 14:08:49 +00001353 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001354 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001355}
1356
Damien George969a6b32014-12-10 22:07:04 +00001357STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001358 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001359 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001360 }
Damien George090c9232014-10-17 14:08:49 +00001361 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001362 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001363}
1364
Damien George969a6b32014-12-10 22:07:04 +00001365STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001366 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001367 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001368 return;
1369 }
Damiend99b0522013-12-21 18:17:45 +00001370 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001371 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001372 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001373 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001374 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001375 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1376 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 +01001377
Damien George6f355fd2014-04-10 14:11:31 +01001378 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001379 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1380 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1381 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001382 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001383 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1384 } else {
1385 compile_node(comp, pns->nodes[0]);
1386 }
1387 EMIT(return_value);
1388}
1389
Damien George969a6b32014-12-10 22:07:04 +00001390STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001391 compile_node(comp, pns->nodes[0]);
1392 EMIT(pop_top);
1393}
1394
Damien George969a6b32014-12-10 22:07:04 +00001395STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001396 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001397 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001398 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001399 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001400 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001401 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001402 compile_node(comp, pns->nodes[0]);
1403 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001404 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001405 } else {
1406 // raise x
1407 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001408 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001409 }
1410}
1411
Damien George635543c2014-04-10 12:56:52 +01001412// q_base holds the base of the name
1413// eg a -> q_base=a
1414// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001415STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001416 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001417 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1418 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001419 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001420 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001421 pn = pns->nodes[0];
1422 is_as = true;
1423 }
Damien George635543c2014-04-10 12:56:52 +01001424 if (MP_PARSE_NODE_IS_NULL(pn)) {
1425 // empty name (eg, from . import x)
1426 *q_base = MP_QSTR_;
1427 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1428 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001429 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001430 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001431 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001432 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001433 }
Damien George635543c2014-04-10 12:56:52 +01001434 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001435 } else {
1436 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001437 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001438 {
Damien429d7192013-10-04 19:53:11 +01001439 // a name of the form a.b.c
1440 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001441 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001442 }
Damiend99b0522013-12-21 18:17:45 +00001443 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001444 int len = n - 1;
1445 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001446 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001447 }
Damien George55baff42014-01-21 21:40:13 +00001448 byte *q_ptr;
1449 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001450 for (int i = 0; i < n; i++) {
1451 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001452 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001453 }
Damien George39dc1452014-10-03 19:52:22 +01001454 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001455 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001456 memcpy(str_dest, str_src, str_src_len);
1457 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001458 }
Damien George635543c2014-04-10 12:56:52 +01001459 qstr q_full = qstr_build_end(q_ptr);
1460 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001461 if (is_as) {
1462 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001463 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001464 }
1465 }
Damien429d7192013-10-04 19:53:11 +01001466 }
Damien429d7192013-10-04 19:53:11 +01001467 }
1468}
1469
Damien George6be0b0a2014-08-15 14:30:52 +01001470STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001471 EMIT_ARG(load_const_small_int, 0); // level 0 import
1472 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1473 qstr q_base;
1474 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001475 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001476}
1477
Damien George969a6b32014-12-10 22:07:04 +00001478STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001479 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1480}
1481
Damien George969a6b32014-12-10 22:07:04 +00001482STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001483 mp_parse_node_t pn_import_source = pns->nodes[0];
1484
1485 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1486 uint import_level = 0;
1487 do {
1488 mp_parse_node_t pn_rel;
1489 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)) {
1490 // This covers relative imports with dots only like "from .. import"
1491 pn_rel = pn_import_source;
1492 pn_import_source = MP_PARSE_NODE_NULL;
1493 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1494 // This covers relative imports starting with dot(s) like "from .foo import"
1495 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1496 pn_rel = pns_2b->nodes[0];
1497 pn_import_source = pns_2b->nodes[1];
1498 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1499 } else {
1500 // Not a relative import
1501 break;
1502 }
1503
1504 // get the list of . and/or ...'s
1505 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001506 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001507
1508 // count the total number of .'s
1509 for (int i = 0; i < n; i++) {
1510 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1511 import_level++;
1512 } else {
1513 // should be an MP_TOKEN_ELLIPSIS
1514 import_level += 3;
1515 }
1516 }
1517 } while (0);
1518
Damiend99b0522013-12-21 18:17:45 +00001519 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001520 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001521
1522 // build the "fromlist" tuple
1523#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001524 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001525#else
Damien George708c0732014-04-27 19:23:46 +01001526 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001527 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001528#endif
1529
1530 // do the import
Damien George635543c2014-04-10 12:56:52 +01001531 qstr dummy_q;
1532 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001533 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001534
Damien429d7192013-10-04 19:53:11 +01001535 } else {
Damien George635543c2014-04-10 12:56:52 +01001536 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001537
1538 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001539 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001540 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001541#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001542 {
1543 vstr_t *vstr = vstr_new();
1544 vstr_printf(vstr, "(");
1545 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001546 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1547 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1548 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001549 if (i > 0) {
1550 vstr_printf(vstr, ", ");
1551 }
1552 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001553 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001554 const byte *str = qstr_data(id2, &len);
1555 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001556 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001557 }
Damien02f89412013-12-12 15:13:36 +00001558 if (n == 1) {
1559 vstr_printf(vstr, ",");
1560 }
1561 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001562 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001563 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001564 }
Damiendb4c3612013-12-10 17:27:24 +00001565#else
1566 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001567 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1568 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1569 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001570 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001571 }
Damien Georgeb9791222014-01-23 00:34:21 +00001572 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001573#endif
1574
1575 // do the import
Damien George635543c2014-04-10 12:56:52 +01001576 qstr dummy_q;
1577 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001578 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001579 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1580 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1581 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001582 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001583 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001584 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001585 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001586 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001587 }
1588 }
1589 EMIT(pop_top);
1590 }
1591}
1592
Damien George584ba672014-12-21 17:26:45 +00001593STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1594 bool added;
1595 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1596 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001597 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001598 compile_syntax_error(comp, pn, "identifier already used");
1599 return;
1600 }
1601 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1602
1603 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1604 id_info = scope_find_global(comp->scope_cur, qst);
1605 if (id_info != NULL) {
1606 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1607 }
1608}
1609
Damien George969a6b32014-12-10 22:07:04 +00001610STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001611 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001612 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001613 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001614 for (int i = 0; i < n; i++) {
1615 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001616 }
1617 }
1618}
1619
Damien George584ba672014-12-21 17:26:45 +00001620STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1621 bool added;
1622 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1623 if (!added) {
Damien Georgeaedf5832015-03-25 22:06:47 +00001624 // TODO this is not compliant with CPython
Damien George584ba672014-12-21 17:26:45 +00001625 compile_syntax_error(comp, pn, "identifier already used");
1626 return;
1627 }
1628 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1629 if (id_info2 == NULL || !(id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) {
1630 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1631 return;
1632 }
1633 id_info->kind = ID_INFO_KIND_FREE;
1634 scope_close_over_in_parents(comp->scope_cur, qst);
1635}
1636
Damien George969a6b32014-12-10 22:07:04 +00001637STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001638 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001639 if (comp->scope_cur->kind == SCOPE_MODULE) {
1640 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1641 return;
1642 }
1643 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001644 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001645 for (int i = 0; i < n; i++) {
1646 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001647 }
1648 }
1649}
1650
Damien George969a6b32014-12-10 22:07:04 +00001651STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001652 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001653 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001654 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001655 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001656 // assertion message
1657 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001658 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001659 }
Damien Georgeb9791222014-01-23 00:34:21 +00001660 EMIT_ARG(raise_varargs, 1);
1661 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001662}
1663
Damien George969a6b32014-12-10 22:07:04 +00001664STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001665 // TODO proper and/or short circuiting
1666
Damien George6f355fd2014-04-10 14:11:31 +01001667 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001668
Damien George391db862014-10-17 17:57:33 +00001669 // optimisation: don't emit anything when "if False" (not in CPython)
1670 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1671 uint l_fail = comp_next_label(comp);
1672 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001673
Damien George391db862014-10-17 17:57:33 +00001674 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001675
Damien George391db862014-10-17 17:57:33 +00001676 // optimisation: skip everything else when "if True" (not in CPython)
1677 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1678 goto done;
1679 }
1680
1681 if (
1682 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1683 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1684 // optimisation: don't jump if last instruction was return
1685 && !EMIT(last_emit_was_return_value)
1686 ) {
1687 // jump over elif/else blocks
1688 EMIT_ARG(jump, l_end);
1689 }
1690
1691 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001692 }
1693
Damien George235f9b32014-10-17 17:30:16 +00001694 // compile elif blocks (if any)
1695 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001696 int n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
Damien George235f9b32014-10-17 17:30:16 +00001697 for (int i = 0; i < n_elif; i++) {
1698 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1699 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001700
Damien George391db862014-10-17 17:57:33 +00001701 // optimisation: don't emit anything when "if False" (not in CPython)
1702 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1703 uint l_fail = comp_next_label(comp);
1704 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001705
Damien George391db862014-10-17 17:57:33 +00001706 compile_node(comp, pns_elif->nodes[1]); // elif block
1707
1708 // optimisation: skip everything else when "elif True" (not in CPython)
1709 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1710 goto done;
1711 }
1712
1713 // optimisation: don't jump if last instruction was return
1714 if (!EMIT(last_emit_was_return_value)) {
1715 EMIT_ARG(jump, l_end);
1716 }
1717 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001718 }
1719 }
1720
1721 // compile else block
1722 compile_node(comp, pns->nodes[3]); // can be null
1723
Damien George391db862014-10-17 17:57:33 +00001724done:
Damien Georgeb9791222014-01-23 00:34:21 +00001725 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001726}
1727
Damien Georgecbddb272014-02-01 20:08:18 +00001728#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001729 uint16_t old_break_label = comp->break_label; \
1730 uint16_t old_continue_label = comp->continue_label; \
1731 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001732 uint break_label = comp_next_label(comp); \
1733 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001734 comp->break_label = break_label; \
1735 comp->continue_label = continue_label; \
1736 comp->break_continue_except_level = comp->cur_except_level;
1737
1738#define END_BREAK_CONTINUE_BLOCK \
1739 comp->break_label = old_break_label; \
1740 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001741 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001742
Damien George969a6b32014-12-10 22:07:04 +00001743STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001744 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001745
Damience89a212013-10-15 22:25:17 +01001746 // compared to CPython, we have an optimised version of while loops
1747#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001748 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001749 EMIT_ARG(setup_loop, break_label);
1750 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001751 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1752 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001753 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001754 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001755 }
Damien Georgeb9791222014-01-23 00:34:21 +00001756 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001757 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1758 // this is a small hack to agree with CPython
1759 if (!node_is_const_true(pns->nodes[0])) {
1760 EMIT(pop_block);
1761 }
Damience89a212013-10-15 22:25:17 +01001762#else
Damien George391db862014-10-17 17:57:33 +00001763 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1764 uint top_label = comp_next_label(comp);
1765 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1766 EMIT_ARG(jump, continue_label);
1767 }
1768 EMIT_ARG(label_assign, top_label);
1769 compile_node(comp, pns->nodes[1]); // body
1770 EMIT_ARG(label_assign, continue_label);
1771 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1772 }
Damience89a212013-10-15 22:25:17 +01001773#endif
1774
1775 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001776 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001777
1778 compile_node(comp, pns->nodes[2]); // else
1779
Damien Georgeb9791222014-01-23 00:34:21 +00001780 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001781}
1782
Damien George2ac4af62014-08-15 16:45:41 +01001783#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001784// This function compiles an optimised for-loop of the form:
1785// for <var> in range(<start>, <end>, <step>):
1786// <body>
1787// else:
1788// <else>
1789// <var> must be an identifier and <step> must be a small-int.
1790//
1791// Semantics of for-loop require:
1792// - final failing value should not be stored in the loop variable
1793// - if the loop never runs, the loop variable should never be assigned
1794// - assignments to <var>, <end> or <step> in the body do not alter the loop
1795// (<step> is a constant for us, so no need to worry about it changing)
1796//
1797// If <end> is a small-int, then the stack during the for-loop contains just
1798// the current value of <var>. Otherwise, the stack contains <end> then the
1799// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001800STATIC 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 +00001801 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001802
Damien George6f355fd2014-04-10 14:11:31 +01001803 uint top_label = comp_next_label(comp);
1804 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001805
Damien Georgee181c0d2014-12-12 17:19:56 +00001806 // put the end value on the stack if it's not a small-int constant
1807 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1808 if (end_on_stack) {
1809 compile_node(comp, pn_end);
1810 }
1811
1812 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001813 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001814
Damien Georgeb9791222014-01-23 00:34:21 +00001815 EMIT_ARG(jump, entry_label);
1816 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001817
Damien Georgec33ce602014-12-11 17:35:23 +00001818 // duplicate next value and store it to var
1819 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001820 c_assign(comp, pn_var, ASSIGN_STORE);
1821
Damienf3822fc2013-11-09 20:12:03 +00001822 // compile body
1823 compile_node(comp, pn_body);
1824
Damien Georgeb9791222014-01-23 00:34:21 +00001825 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001826
Damien Georgee181c0d2014-12-12 17:19:56 +00001827 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001828 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001829 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001830
Damien Georgeb9791222014-01-23 00:34:21 +00001831 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001832
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001833 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001834 if (end_on_stack) {
1835 EMIT(dup_top_two);
1836 EMIT(rot_two);
1837 } else {
1838 EMIT(dup_top);
1839 compile_node(comp, pn_end);
1840 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001841 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1842 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001843 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001844 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001845 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001846 }
Damien George63f38322015-02-28 15:04:06 +00001847 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001848
1849 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001850 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001851
1852 compile_node(comp, pn_else);
1853
Damien Georgeb9791222014-01-23 00:34:21 +00001854 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001855
1856 // discard final value of var that failed the loop condition
1857 EMIT(pop_top);
1858
1859 // discard <end> value if it's on the stack
1860 if (end_on_stack) {
1861 EMIT(pop_top);
1862 }
Damienf72fd0e2013-11-06 20:20:49 +00001863}
Damien George2ac4af62014-08-15 16:45:41 +01001864#endif
Damienf72fd0e2013-11-06 20:20:49 +00001865
Damien George969a6b32014-12-10 22:07:04 +00001866STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001867#if !MICROPY_EMIT_CPYTHON
1868 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1869 // this is actually slower, but uses no heap memory
1870 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001871 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 +00001872 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001873 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1874 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1875 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1876 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001877 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1878 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001879 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001880 mp_parse_node_t pn_range_start;
1881 mp_parse_node_t pn_range_end;
1882 mp_parse_node_t pn_range_step;
1883 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001884 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001885 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001886 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001887 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001888 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001889 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001890 } else if (n_args == 2) {
1891 pn_range_start = args[0];
1892 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001893 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001894 } else {
1895 pn_range_start = args[0];
1896 pn_range_end = args[1];
1897 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001898 // We need to know sign of step. This is possible only if it's constant
1899 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1900 optimize = false;
1901 }
Damienf72fd0e2013-11-06 20:20:49 +00001902 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001903 }
1904 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001905 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1906 return;
1907 }
1908 }
1909 }
1910#endif
1911
Damien Georgecbddb272014-02-01 20:08:18 +00001912 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001913 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001914
Damien George6f355fd2014-04-10 14:11:31 +01001915 uint pop_label = comp_next_label(comp);
1916 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001917
Damience89a212013-10-15 22:25:17 +01001918 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1919#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001920 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001921#endif
1922
Damien429d7192013-10-04 19:53:11 +01001923 compile_node(comp, pns->nodes[1]); // iterator
1924 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001925 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001926 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001927 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1928 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001929 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001930 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001931 }
Damien Georgeb9791222014-01-23 00:34:21 +00001932 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001933 EMIT(for_iter_end);
1934
1935 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001936 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001937
Damience89a212013-10-15 22:25:17 +01001938#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001939 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001940#endif
Damien429d7192013-10-04 19:53:11 +01001941
1942 compile_node(comp, pns->nodes[3]); // else (not tested)
1943
Damien Georgeb9791222014-01-23 00:34:21 +00001944 EMIT_ARG(label_assign, break_label);
1945 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001946}
1947
Damien George6be0b0a2014-08-15 14:30:52 +01001948STATIC 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 +01001949 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001950 uint l1 = comp_next_label(comp);
1951 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001952
Damien Georgeb9791222014-01-23 00:34:21 +00001953 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001954 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001955
Damien429d7192013-10-04 19:53:11 +01001956 compile_node(comp, pn_body); // body
1957 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001958 EMIT_ARG(jump, success_label); // jump over exception handler
1959
1960 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001961 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001962
Damien George6f355fd2014-04-10 14:11:31 +01001963 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001964
1965 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001966 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1967 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001968
1969 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001970 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001971
Damiend99b0522013-12-21 18:17:45 +00001972 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001973 // this is a catch all exception handler
1974 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001975 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001976 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001977 return;
1978 }
1979 } else {
1980 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001981 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1982 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1983 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1984 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001985 // handler binds the exception to a local
1986 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001987 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001988 }
1989 }
1990 EMIT(dup_top);
1991 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001992 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001993 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001994 }
1995
1996 EMIT(pop_top);
1997
1998 if (qstr_exception_local == 0) {
1999 EMIT(pop_top);
2000 } else {
Damien George542bd6b2015-03-26 14:42:40 +00002001 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002002 }
2003
2004 EMIT(pop_top);
2005
Damien George6f355fd2014-04-10 14:11:31 +01002006 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002007 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002008 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002009 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002010 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002011 }
2012 compile_node(comp, pns_except->nodes[1]);
2013 if (qstr_exception_local != 0) {
2014 EMIT(pop_block);
2015 }
2016 EMIT(pop_except);
2017 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002018 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2019 EMIT_ARG(label_assign, l3);
2020 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00002021 compile_store_id(comp, qstr_exception_local);
2022 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002023
Damien George8dcc0c72014-03-27 10:55:21 +00002024 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002025 EMIT(end_finally);
2026 }
Damien Georgeb9791222014-01-23 00:34:21 +00002027 EMIT_ARG(jump, l2);
2028 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002029 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002030 }
2031
Damien George8dcc0c72014-03-27 10:55:21 +00002032 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002033 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002034 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002035
Damien Georgeb9791222014-01-23 00:34:21 +00002036 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002037 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002038 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002039}
2040
Damien George6be0b0a2014-08-15 14:30:52 +01002041STATIC 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 +01002042 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002043
Damien Georgeb9791222014-01-23 00:34:21 +00002044 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002045 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002046
Damien429d7192013-10-04 19:53:11 +01002047 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002048 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002049 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002050 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002051 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002052 } else {
2053 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2054 }
2055 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002056 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2057 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002058 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002059
Damien George8dcc0c72014-03-27 10:55:21 +00002060 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002061 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002062}
2063
Damien George969a6b32014-12-10 22:07:04 +00002064STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00002065 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2066 {
Damiend99b0522013-12-21 18:17:45 +00002067 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2068 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002069 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002070 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2071 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002072 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002073 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002074 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002075 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002076 // no finally
2077 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2078 } else {
2079 // have finally
Damiend99b0522013-12-21 18:17:45 +00002080 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 +01002081 }
2082 } else {
2083 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002084 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00002085 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002086 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002087 }
Damien429d7192013-10-04 19:53:11 +01002088 }
2089}
2090
Damien George6be0b0a2014-08-15 14:30:52 +01002091STATIC 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 +01002092 if (n == 0) {
2093 // no more pre-bits, compile the body of the with
2094 compile_node(comp, body);
2095 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002096 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002097 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002098 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002099 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002100 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002101 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002102 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2103 } else {
2104 // this pre-bit is just an expression
2105 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002106 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002107 EMIT(pop_top);
2108 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002109 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002110 // compile additional pre-bits and the body
2111 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2112 // finish this with block
2113 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002114 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2115 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002116 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002117 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002118 EMIT(end_finally);
2119 }
2120}
2121
Damien George969a6b32014-12-10 22:07:04 +00002122STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002123 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002124 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002125 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01002126 assert(n > 0);
2127
2128 // compile in a nested fashion
2129 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2130}
2131
Damien George969a6b32014-12-10 22:07:04 +00002132STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002133 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002134 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2135 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00002136 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002137 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002138 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002139 EMIT(pop_top);
2140
Damien429d7192013-10-04 19:53:11 +01002141 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002142 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002143 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002144 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00002145 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
2146 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002147 // do nothing with a lonely constant
2148 } else {
2149 compile_node(comp, pns->nodes[0]); // just an expression
2150 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2151 }
Damien429d7192013-10-04 19:53:11 +01002152 }
2153 } else {
Damiend99b0522013-12-21 18:17:45 +00002154 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2155 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002156 if (kind == PN_expr_stmt_augassign) {
2157 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2158 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002159 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002160 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002161 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002162 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2163 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2164 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2165 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2166 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2167 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2168 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2169 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2170 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2171 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2172 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002173 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002174 }
Damien George7e5fb242014-02-01 22:18:47 +00002175 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002176 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2177 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002178 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2179 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002180 // following CPython, we store left-most first
2181 if (rhs > 0) {
2182 EMIT(dup_top);
2183 }
2184 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2185 for (int i = 0; i < rhs; i++) {
2186 if (i + 1 < rhs) {
2187 EMIT(dup_top);
2188 }
Damiend99b0522013-12-21 18:17:45 +00002189 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002190 }
Damien George0bb97132015-02-27 14:25:47 +00002191 } else {
2192 assert(kind == PN_expr_stmt_assign); // should be
Damien George42e0c592015-03-14 13:11:35 +00002193 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
2194 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002195 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2196 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2197 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002198 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002199 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2200 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002201 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2202 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2203 // can't optimise when it's a star expression on the lhs
2204 goto no_optimisation;
2205 }
Damien429d7192013-10-04 19:53:11 +01002206 compile_node(comp, pns10->nodes[0]); // rhs
2207 compile_node(comp, pns10->nodes[1]); // rhs
2208 EMIT(rot_two);
2209 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2210 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00002211 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2212 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002213 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2214 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2215 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002216 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002217 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2218 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002219 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2220 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2221 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2222 // can't optimise when it's a star expression on the lhs
2223 goto no_optimisation;
2224 }
Damien429d7192013-10-04 19:53:11 +01002225 compile_node(comp, pns10->nodes[0]); // rhs
2226 compile_node(comp, pns10->nodes[1]); // rhs
2227 compile_node(comp, pns10->nodes[2]); // rhs
2228 EMIT(rot_three);
2229 EMIT(rot_two);
2230 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2231 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2232 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2233 } else {
Damien George495d7812014-04-08 17:51:47 +01002234 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002235 compile_node(comp, pns1->nodes[0]); // rhs
2236 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2237 }
Damien429d7192013-10-04 19:53:11 +01002238 }
2239 }
2240}
2241
Damien George6be0b0a2014-08-15 14:30:52 +01002242STATIC 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 +00002243 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002244 compile_node(comp, pns->nodes[0]);
2245 for (int i = 1; i < num_nodes; i += 1) {
2246 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002247 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002248 }
2249}
2250
Damien George969a6b32014-12-10 22:07:04 +00002251STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002252 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2253 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002254
Damien George6f355fd2014-04-10 14:11:31 +01002255 uint l_fail = comp_next_label(comp);
2256 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002257 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2258 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002259 EMIT_ARG(jump, l_end);
2260 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002261 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002262 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002263 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002264}
2265
Damien George969a6b32014-12-10 22:07:04 +00002266STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002267 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002268 //mp_parse_node_t pn_params = pns->nodes[0];
2269 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002270
Damien George36db6bc2014-05-07 17:24:22 +01002271 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002272 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002273 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 +01002274 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002275 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002276 }
2277
2278 // get the scope for this lambda
2279 scope_t *this_scope = (scope_t*)pns->nodes[2];
2280
2281 // make the lambda
2282 close_over_variables_etc(comp, this_scope, 0, 0);
2283}
2284
Damien George7711afb2015-02-28 15:10:18 +00002285STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002286 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002287 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002288 for (int i = 0; i < n; i += 1) {
2289 compile_node(comp, pns->nodes[i]);
2290 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002291 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002292 }
2293 }
Damien Georgeb9791222014-01-23 00:34:21 +00002294 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002295}
2296
Damien George7711afb2015-02-28 15:10:18 +00002297STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2298 compile_or_and_test(comp, pns, true);
2299}
2300
Damien George969a6b32014-12-10 22:07:04 +00002301STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002302 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002303}
2304
Damien George969a6b32014-12-10 22:07:04 +00002305STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002306 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002307 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002308}
2309
Damien George969a6b32014-12-10 22:07:04 +00002310STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002311 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002312 compile_node(comp, pns->nodes[0]);
2313 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002314 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002315 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002316 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002317 }
2318 for (int i = 1; i + 1 < num_nodes; i += 2) {
2319 compile_node(comp, pns->nodes[i + 1]);
2320 if (i + 2 < num_nodes) {
2321 EMIT(dup_top);
2322 EMIT(rot_three);
2323 }
Damien George7e5fb242014-02-01 22:18:47 +00002324 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002325 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002326 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002327 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2328 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2329 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2330 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2331 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2332 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002333 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002334 }
2335 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002336 } else {
2337 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002338 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2339 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002340 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002341 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002342 } else {
2343 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002344 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002345 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002346 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002347 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002348 }
Damien429d7192013-10-04 19:53:11 +01002349 }
Damien429d7192013-10-04 19:53:11 +01002350 }
2351 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002352 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002353 }
2354 }
2355 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002356 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002357 EMIT_ARG(jump, l_end);
2358 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002359 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002360 EMIT(rot_two);
2361 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002362 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002363 }
2364}
2365
Damien George969a6b32014-12-10 22:07:04 +00002366STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002367 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002368}
2369
Damien George969a6b32014-12-10 22:07:04 +00002370STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002371 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002372}
2373
Damien George969a6b32014-12-10 22:07:04 +00002374STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002375 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002376}
2377
Damien George969a6b32014-12-10 22:07:04 +00002378STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002379 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002380}
2381
Damien George969a6b32014-12-10 22:07:04 +00002382STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002383 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002384 compile_node(comp, pns->nodes[0]);
2385 for (int i = 1; i + 1 < num_nodes; i += 2) {
2386 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002387 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002388 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002389 } else {
Damien George0bb97132015-02-27 14:25:47 +00002390 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2391 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002392 }
2393 }
2394}
2395
Damien George969a6b32014-12-10 22:07:04 +00002396STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002397 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002398 compile_node(comp, pns->nodes[0]);
2399 for (int i = 1; i + 1 < num_nodes; i += 2) {
2400 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002401 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002402 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002403 } else {
Damien George0bb97132015-02-27 14:25:47 +00002404 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2405 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002406 }
2407 }
2408}
2409
Damien George969a6b32014-12-10 22:07:04 +00002410STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002411 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002412 compile_node(comp, pns->nodes[0]);
2413 for (int i = 1; i + 1 < num_nodes; i += 2) {
2414 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002415 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002416 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002417 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002418 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002419 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002420 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002421 } else {
Damien George0bb97132015-02-27 14:25:47 +00002422 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2423 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002424 }
2425 }
2426}
2427
Damien George969a6b32014-12-10 22:07:04 +00002428STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002429 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002430 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002431 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002432 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002433 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002434 } else {
Damien George0bb97132015-02-27 14:25:47 +00002435 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2436 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002437 }
2438}
2439
Damien George969a6b32014-12-10 22:07:04 +00002440STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002441 // this is to handle special super() call
2442 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2443
2444 compile_generic_all_nodes(comp, pns);
2445}
2446
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002447STATIC 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 +01002448 // function to call is on top of stack
2449
Damien George35e2a4e2014-02-05 00:51:47 +00002450#if !MICROPY_EMIT_CPYTHON
2451 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002452 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George542bd6b2015-03-26 14:42:40 +00002453 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002454 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002455 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002456 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002457 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002458 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002459 EMIT_ARG(call_function, 2, 0, 0);
2460 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002461 }
2462 }
Damien George01039b52014-12-21 17:44:27 +00002463 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002464 return;
2465 }
2466#endif
2467
Damien George2c0842b2014-04-27 16:46:51 +01002468 // get the list of arguments
2469 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002470 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002471
Damien George2c0842b2014-04-27 16:46:51 +01002472 // compile the arguments
2473 // Rather than calling compile_node on the list, we go through the list of args
2474 // explicitly here so that we can count the number of arguments and give sensible
2475 // error messages.
2476 int n_positional = n_positional_extra;
2477 uint n_keyword = 0;
2478 uint star_flags = 0;
2479 for (int i = 0; i < n_args; i++) {
2480 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2481 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2482 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2483 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2484 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2485 return;
2486 }
2487 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2488 compile_node(comp, pns_arg->nodes[0]);
2489 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2490 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2491 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2492 return;
2493 }
2494 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2495 compile_node(comp, pns_arg->nodes[0]);
2496 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2497 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2498 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2499 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2500 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2501 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2502 return;
2503 }
Damien George968bf342014-04-27 19:12:05 +01002504 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002505 compile_node(comp, pns2->nodes[0]);
2506 n_keyword += 1;
2507 } else {
2508 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2509 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2510 n_positional++;
2511 }
2512 } else {
2513 goto normal_argument;
2514 }
2515 } else {
2516 normal_argument:
2517 if (n_keyword > 0) {
2518 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2519 return;
2520 }
2521 compile_node(comp, args[i]);
2522 n_positional++;
2523 }
Damien429d7192013-10-04 19:53:11 +01002524 }
2525
Damien George2c0842b2014-04-27 16:46:51 +01002526 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002527 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002528 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002529 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002530 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002531 }
Damien429d7192013-10-04 19:53:11 +01002532}
2533
Damien George969a6b32014-12-10 22:07:04 +00002534STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002535 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002536 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002537 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 +01002538 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002539 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2540 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002541 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002542 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002543 i += 1;
2544 } else {
2545 compile_node(comp, pns->nodes[i]);
2546 }
Damien George35e2a4e2014-02-05 00:51:47 +00002547 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002548 }
2549}
2550
Damien George969a6b32014-12-10 22:07:04 +00002551STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002552 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002553 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002554}
2555
Damien George969a6b32014-12-10 22:07:04 +00002556STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002557 // a list of strings
Damien63321742013-12-10 17:41:49 +00002558
2559 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002560 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002561 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002562 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002563 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002564 int pn_kind;
2565 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2566 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2567 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2568 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2569 } else {
2570 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2571 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002572 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2573 pn_kind = MP_PARSE_NODE_STRING;
2574 } else {
2575 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2576 pn_kind = MP_PARSE_NODE_BYTES;
2577 }
Damien George40f3c022014-07-03 13:25:24 +01002578 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002579 }
Damien63321742013-12-10 17:41:49 +00002580 if (i == 0) {
2581 string_kind = pn_kind;
2582 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002583 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002584 return;
2585 }
Damien429d7192013-10-04 19:53:11 +01002586 }
Damien63321742013-12-10 17:41:49 +00002587
Damien George65ef6b72015-01-14 21:17:27 +00002588 // if we are not in the last pass, just load a dummy object
2589 if (comp->pass != MP_PASS_EMIT) {
2590 EMIT_ARG(load_const_obj, mp_const_none);
2591 return;
2592 }
2593
Damien63321742013-12-10 17:41:49 +00002594 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002595 vstr_t vstr;
2596 vstr_init_len(&vstr, n_bytes);
2597 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002598 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002599 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002600 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002601 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2602 memcpy(s_dest, s, s_len);
2603 s_dest += s_len;
2604 } else {
2605 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002606 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2607 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002608 }
Damien63321742013-12-10 17:41:49 +00002609 }
Damien George65ef6b72015-01-14 21:17:27 +00002610
2611 // load the object
Damien George05005f62015-01-21 22:48:37 +00002612 EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr));
Damien429d7192013-10-04 19:53:11 +01002613}
2614
2615// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002616STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002617 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2618 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2619 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002620
Damien George36db6bc2014-05-07 17:24:22 +01002621 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002622 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002623 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 +01002624 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002625 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002626 }
2627
2628 // get the scope for this comprehension
2629 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2630
2631 // compile the comprehension
2632 close_over_variables_etc(comp, this_scope, 0, 0);
2633
2634 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2635 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002636 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002637}
2638
Damien George969a6b32014-12-10 22:07:04 +00002639STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002640 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002641 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002642 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2643 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2644 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2645 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2646 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2647 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2648 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002649 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002650 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002651 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002652 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002653 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002654 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002655 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002656 // generator expression
2657 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2658 } else {
2659 // tuple with 2 items
2660 goto tuple_with_2_items;
2661 }
2662 } else {
2663 // tuple with 2 items
2664 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002665 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002666 }
2667 } else {
2668 // parenthesis around a single item, is just that item
2669 compile_node(comp, pns->nodes[0]);
2670 }
2671}
2672
Damien George969a6b32014-12-10 22:07:04 +00002673STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002674 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002675 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002676 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002677 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2678 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2679 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2680 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2681 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002682 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002683 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002684 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002685 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002686 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002687 // list of many items
2688 compile_node(comp, pns2->nodes[0]);
2689 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002690 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002691 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002692 // list comprehension
2693 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2694 } else {
2695 // list with 2 items
2696 goto list_with_2_items;
2697 }
2698 } else {
2699 // list with 2 items
2700 list_with_2_items:
2701 compile_node(comp, pns2->nodes[0]);
2702 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002703 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002704 }
2705 } else {
2706 // list with 1 item
2707 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002708 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002709 }
2710}
2711
Damien George969a6b32014-12-10 22:07:04 +00002712STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002713 mp_parse_node_t pn = pns->nodes[0];
2714 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002715 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002716 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002717 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2718 pns = (mp_parse_node_struct_t*)pn;
2719 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002720 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002721 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002722 compile_node(comp, pn);
2723 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002724 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2725 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2726 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2727 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002728 // dict/set with multiple elements
2729
2730 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002731 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002732 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002733
2734 // first element sets whether it's a dict or set
2735 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002736 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002737 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002738 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002739 compile_node(comp, pns->nodes[0]);
2740 EMIT(store_map);
2741 is_dict = true;
2742 } else {
2743 // a set
2744 compile_node(comp, pns->nodes[0]); // 1st value of set
2745 is_dict = false;
2746 }
2747
2748 // process rest of elements
2749 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002750 mp_parse_node_t pn_i = nodes[i];
2751 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2752 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002753 if (is_dict) {
2754 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002755 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002756 return;
2757 }
2758 EMIT(store_map);
2759 } else {
2760 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002761 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002762 return;
2763 }
2764 }
2765 }
2766
Damien Georgee37dcaa2014-12-27 17:07:16 +00002767 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002768 // if it's a set, build it
2769 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002770 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002771 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002772 #endif
Damien George0bb97132015-02-27 14:25:47 +00002773 } else {
2774 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002775 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002776 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002777 // a dictionary comprehension
2778 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2779 } else {
2780 // a set comprehension
2781 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2782 }
Damien429d7192013-10-04 19:53:11 +01002783 }
2784 } else {
2785 // set with one element
2786 goto set_with_one_element;
2787 }
2788 } else {
2789 // set with one element
2790 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002791 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002792 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002793 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002794 #else
2795 assert(0);
2796 #endif
Damien429d7192013-10-04 19:53:11 +01002797 }
2798}
2799
Damien George969a6b32014-12-10 22:07:04 +00002800STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002801 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002802}
2803
Damien George969a6b32014-12-10 22:07:04 +00002804STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002805 // object who's index we want is on top of stack
2806 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002807 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002808}
2809
Damien George969a6b32014-12-10 22:07:04 +00002810STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002811 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002812 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002813}
2814
Damien George83204f32014-12-27 17:20:41 +00002815#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002816STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002817 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2818 mp_parse_node_t pn = pns->nodes[0];
2819 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002820 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002821 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2822 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002823 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2824 pns = (mp_parse_node_struct_t*)pn;
2825 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002826 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002827 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002828 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002829 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002830 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002831 } else {
2832 // [?::x]
2833 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002834 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002835 }
Damiend99b0522013-12-21 18:17:45 +00002836 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002837 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002838 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2839 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2840 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2841 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002842 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002843 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002844 } else {
2845 // [?:x:x]
2846 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002847 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002848 }
2849 } else {
2850 // [?:x]
2851 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002852 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002853 }
2854 } else {
2855 // [?:x]
2856 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002857 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002858 }
2859}
2860
Damien George969a6b32014-12-10 22:07:04 +00002861STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002862 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002863 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2864 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002865}
2866
Damien George969a6b32014-12-10 22:07:04 +00002867STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002868 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002869 compile_subscript_3_helper(comp, pns);
2870}
Damien George83204f32014-12-27 17:20:41 +00002871#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002872
Damien George969a6b32014-12-10 22:07:04 +00002873STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002874 // if this is called then we are compiling a dict key:value pair
2875 compile_node(comp, pns->nodes[1]); // value
2876 compile_node(comp, pns->nodes[0]); // key
2877}
2878
Damien George969a6b32014-12-10 22:07:04 +00002879STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002880 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002881 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002882 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002883}
2884
Damien George969a6b32014-12-10 22:07:04 +00002885STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002886 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002887 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002888 return;
2889 }
Damiend99b0522013-12-21 18:17:45 +00002890 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002892 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002893 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2894 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002895 compile_node(comp, pns->nodes[0]);
2896 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002897 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002898 EMIT(yield_from);
2899 } else {
2900 compile_node(comp, pns->nodes[0]);
2901 EMIT(yield_value);
2902 }
2903}
2904
Damien George65ef6b72015-01-14 21:17:27 +00002905STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2906 // only create and load the actual str object on the last pass
2907 if (comp->pass != MP_PASS_EMIT) {
2908 EMIT_ARG(load_const_obj, mp_const_none);
2909 } else {
2910 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2911 }
2912}
2913
2914STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2915 // only create and load the actual bytes object on the last pass
2916 if (comp->pass != MP_PASS_EMIT) {
2917 EMIT_ARG(load_const_obj, mp_const_none);
2918 } else {
2919 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2920 }
2921}
2922
Damien George7d414a12015-02-08 01:57:40 +00002923STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2924 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2925}
2926
Damiend99b0522013-12-21 18:17:45 +00002927typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002928STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002929#define nc NULL
2930#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002931#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002932#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002933#undef nc
2934#undef c
2935#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002936 NULL,
2937 compile_string,
2938 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002939 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002940};
2941
Damien George6be0b0a2014-08-15 14:30:52 +01002942STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002943 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002944 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002945 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002946 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002947 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002948 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002949 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002950 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002951 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002952 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2953 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damien Georged2d64f02015-01-14 21:32:42 +00002954 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002955 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002956 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002957 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002958 // do nothing
2959 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002960 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002961 }
2962 break;
Damien429d7192013-10-04 19:53:11 +01002963 }
2964 } else {
Damiend99b0522013-12-21 18:17:45 +00002965 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002966 EMIT_ARG(set_line_number, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002967 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2968 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002969#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002970 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2971 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002972#endif
Damien George65ef6b72015-01-14 21:17:27 +00002973 compile_syntax_error(comp, pn, "internal compiler error");
2974 } else {
2975 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002976 }
2977 }
2978}
2979
Damien George2ac4af62014-08-15 16:45:41 +01002980STATIC void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star) {
Damien429d7192013-10-04 19:53:11 +01002981 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002982 qstr param_name = MP_QSTR_NULL;
2983 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002984 if (MP_PARSE_NODE_IS_ID(pn)) {
2985 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002986 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002987 // comes after a star, so counts as a keyword-only parameter
2988 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002989 } else {
Damien George2827d622014-04-27 15:50:52 +01002990 // comes before a star, so counts as a positional parameter
2991 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002992 }
Damienb14de212013-10-06 00:28:28 +01002993 } else {
Damiend99b0522013-12-21 18:17:45 +00002994 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2995 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2996 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2997 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002998 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002999 // comes after a star, so counts as a keyword-only parameter
3000 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003001 } else {
Damien George2827d622014-04-27 15:50:52 +01003002 // comes before a star, so counts as a positional parameter
3003 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003004 }
Damiend99b0522013-12-21 18:17:45 +00003005 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003006 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003007 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003008 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003009 // bare star
3010 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003011 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003012 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003013 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003014 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003015 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00003016 } else {
3017 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01003018 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003019 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003020 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3021 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003022 }
Damien George0bb97132015-02-27 14:25:47 +00003023 } else {
3024 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00003025 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003026 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003027 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003028 }
Damien429d7192013-10-04 19:53:11 +01003029 }
3030
Damien George2827d622014-04-27 15:50:52 +01003031 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003032 bool added;
3033 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3034 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003035 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003036 return;
3037 }
Damien429d7192013-10-04 19:53:11 +01003038 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003039 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003040 }
3041}
3042
Damien George2827d622014-04-27 15:50:52 +01003043STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003044 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003045}
3046
Damien George2827d622014-04-27 15:50:52 +01003047STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003048 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3049}
3050
3051STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3052 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3053 // no annotation
3054 return;
3055 }
3056
3057 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3058 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3059 // named parameter with possible annotation
3060 // fallthrough
3061 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3062 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3063 // named star with possible annotation
3064 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3065 // fallthrough
3066 } else {
3067 // no annotation
3068 return;
3069 }
3070 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3071 // double star with possible annotation
3072 // fallthrough
3073 } else {
3074 // no annotation
3075 return;
3076 }
3077
3078 mp_parse_node_t pn_annotation = pns->nodes[1];
3079
3080 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3081 #if MICROPY_EMIT_NATIVE
3082 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3083 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3084 assert(id_info != NULL);
3085
3086 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3087 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3088 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3089 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3090 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003091 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003092 }
3093 }
3094 #endif // MICROPY_EMIT_NATIVE
3095 }
Damien429d7192013-10-04 19:53:11 +01003096}
3097
Damien George6be0b0a2014-08-15 14:30:52 +01003098STATIC 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 +01003099 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003100 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003101 // no more nested if/for; compile inner expression
3102 compile_node(comp, pn_inner_expr);
3103 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003104 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003105 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003106 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003107 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003108 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003109 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003110 #endif
Damien429d7192013-10-04 19:53:11 +01003111 } else {
3112 EMIT(yield_value);
3113 EMIT(pop_top);
3114 }
Damiend99b0522013-12-21 18:17:45 +00003115 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003116 // if condition
Damiend99b0522013-12-21 18:17:45 +00003117 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003118 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3119 pn_iter = pns_comp_if->nodes[1];
3120 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00003121 } else {
3122 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01003123 // for loop
Damiend99b0522013-12-21 18:17:45 +00003124 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003125 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003126 uint l_end2 = comp_next_label(comp);
3127 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003128 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003129 EMIT_ARG(label_assign, l_top2);
3130 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003131 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3132 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 +00003133 EMIT_ARG(jump, l_top2);
3134 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003135 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01003136 }
3137}
3138
Damien George1463c1f2014-04-25 23:52:57 +01003139STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3140#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003141 // see http://www.python.org/dev/peps/pep-0257/
3142
3143 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003144 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003145 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003146 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003147 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003148 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3149 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003150 for (int i = 0; i < num_nodes; i++) {
3151 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003152 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 +00003153 // not a newline, so this is the first statement; finish search
3154 break;
3155 }
3156 }
3157 // 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 +00003158 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003159 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003160 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003161 } else {
3162 return;
3163 }
3164
3165 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003166 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3167 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003168 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3169 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3170 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3171 // compile the doc string
3172 compile_node(comp, pns->nodes[0]);
3173 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00003174 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003175 }
3176 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003177#else
3178 (void)comp;
3179 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003180#endif
Damien429d7192013-10-04 19:53:11 +01003181}
3182
Damien George1463c1f2014-04-25 23:52:57 +01003183STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003184 comp->pass = pass;
3185 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003186 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003187 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003188
Damien George36db6bc2014-05-07 17:24:22 +01003189 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003190 // reset maximum stack sizes in scope
3191 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003192 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003193 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003194 }
3195
Damien5ac1b2e2013-10-18 19:58:12 +01003196#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003197 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003198 scope_print_info(scope);
3199 }
Damien5ac1b2e2013-10-18 19:58:12 +01003200#endif
Damien429d7192013-10-04 19:53:11 +01003201
3202 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003203 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3204 assert(scope->kind == SCOPE_MODULE);
3205 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3206 compile_node(comp, pns->nodes[0]); // compile the expression
3207 EMIT(return_value);
3208 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003209 if (!comp->is_repl) {
3210 check_for_doc_string(comp, scope->pn);
3211 }
Damien429d7192013-10-04 19:53:11 +01003212 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003213 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003214 EMIT(return_value);
3215 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003216 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3217 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3218 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003219
3220 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003221 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003222 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003223 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003224 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003225 } else {
3226 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003227
Damien George2ac4af62014-08-15 16:45:41 +01003228 // argument annotations
3229 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3230
3231 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003232 mp_parse_node_t pn_annotation = pns->nodes[2];
3233 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3234 #if MICROPY_EMIT_NATIVE
3235 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3236 // nodes[2] can be null or a test-expr
3237 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3238 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3239 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3240 } else {
3241 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3242 }
Damien George2ac4af62014-08-15 16:45:41 +01003243 }
Damien Georgea5190a72014-08-15 22:39:08 +01003244 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003245 }
Damien George2ac4af62014-08-15 16:45:41 +01003246 }
Damien429d7192013-10-04 19:53:11 +01003247
3248 compile_node(comp, pns->nodes[3]); // 3 is function body
3249 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003250 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003251 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003252 EMIT(return_value);
3253 }
3254 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003255 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3256 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3257 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003258
3259 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003260 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003261 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003262 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003263 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3264 }
3265
3266 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003267
3268 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3269 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3270 EMIT(pop_top);
3271 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3272 }
Damien429d7192013-10-04 19:53:11 +01003273 EMIT(return_value);
3274 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3275 // a bit of a hack at the moment
3276
Damiend99b0522013-12-21 18:17:45 +00003277 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3278 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3279 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3280 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3281 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003282
Damien George708c0732014-04-27 19:23:46 +01003283 // We need a unique name for the comprehension argument (the iterator).
3284 // CPython uses .0, but we should be able to use anything that won't
3285 // clash with a user defined variable. Best to use an existing qstr,
3286 // so we use the blank qstr.
3287#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003288 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003289#else
3290 qstr qstr_arg = MP_QSTR_;
3291#endif
Damien George36db6bc2014-05-07 17:24:22 +01003292 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003293 bool added;
3294 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3295 assert(added);
3296 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003297 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003298 }
3299
3300 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003301 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003302 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003303 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003304 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003305 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003306 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003307 #endif
Damien429d7192013-10-04 19:53:11 +01003308 }
3309
Damien George6f355fd2014-04-10 14:11:31 +01003310 uint l_end = comp_next_label(comp);
3311 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00003312 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00003313 EMIT_ARG(label_assign, l_top);
3314 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003315 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3316 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003317 EMIT_ARG(jump, l_top);
3318 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003319 EMIT(for_iter_end);
3320
3321 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003322 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003323 }
3324 EMIT(return_value);
3325 } else {
3326 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003327 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3328 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3329 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003330
Damien George36db6bc2014-05-07 17:24:22 +01003331 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003332 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003333 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003334 assert(added);
3335 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003336 }
3337
Damien George542bd6b2015-03-26 14:42:40 +00003338 compile_load_id(comp, MP_QSTR___name__);
3339 compile_store_id(comp, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003340 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), false); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003341 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003342
3343 check_for_doc_string(comp, pns->nodes[2]);
3344 compile_node(comp, pns->nodes[2]); // 2 is class body
3345
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003346 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003347 assert(id != NULL);
3348 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003349 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003350 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003351#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003352 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003353#else
Damien George542bd6b2015-03-26 14:42:40 +00003354 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003355#endif
Damien429d7192013-10-04 19:53:11 +01003356 }
3357 EMIT(return_value);
3358 }
3359
Damien415eb6f2013-10-05 12:19:06 +01003360 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003361
3362 // make sure we match all the exception levels
3363 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003364}
3365
Damien George094d4502014-04-02 17:31:27 +01003366#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003367// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003368STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003369 comp->pass = pass;
3370 comp->scope_cur = scope;
3371 comp->next_label = 1;
3372
3373 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003374 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003375 return;
3376 }
3377
Damien George36db6bc2014-05-07 17:24:22 +01003378 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003379 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003380 }
3381
Damien826005c2013-10-05 23:17:28 +01003382 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003383 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3384 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3385 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003386
Damiend99b0522013-12-21 18:17:45 +00003387 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003388
Damiena2f2f7d2013-10-06 00:14:13 +01003389 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003390 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003391 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003392 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003393 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003394 if (comp->compile_error != MP_OBJ_NULL) {
3395 goto inline_asm_error;
3396 }
Damiena2f2f7d2013-10-06 00:14:13 +01003397 }
3398
Damiend99b0522013-12-21 18:17:45 +00003399 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003400
Damiend99b0522013-12-21 18:17:45 +00003401 mp_parse_node_t pn_body = pns->nodes[3]; // body
3402 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003403 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003404
Damien Georgecbd2f742014-01-19 11:48:48 +00003405 /*
Damien George36db6bc2014-05-07 17:24:22 +01003406 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003407 //printf("----\n");
3408 scope_print_info(scope);
3409 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003410 */
Damien826005c2013-10-05 23:17:28 +01003411
3412 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003413 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3414 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003415 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3416 // no instructions
3417 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003418 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003419 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003420 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003421 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003422 return;
3423 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003424
3425 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003426 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003427 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3428 goto not_an_instruction;
3429 }
Damiend99b0522013-12-21 18:17:45 +00003430 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00003431 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
3432 goto not_an_instruction;
3433 }
3434 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3435 goto not_an_instruction;
3436 }
3437 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3438 goto not_an_instruction;
3439 }
Damiend99b0522013-12-21 18:17:45 +00003440 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003441
3442 // parse node looks like an instruction
3443 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003444 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3445 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3446 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003447 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003448
3449 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003450 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003451 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003452 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003453 return;
3454 }
Damien George6f355fd2014-04-10 14:11:31 +01003455 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003456 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003457 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3458 compile_syntax_error(comp, nodes[i], "label redefined");
3459 return;
3460 }
Damien826005c2013-10-05 23:17:28 +01003461 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003462 } else if (op == MP_QSTR_align) {
3463 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003464 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003465 return;
3466 }
Damien George36db6bc2014-05-07 17:24:22 +01003467 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003468 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3469 }
3470 } else if (op == MP_QSTR_data) {
3471 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003472 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003473 return;
3474 }
Damien George36db6bc2014-05-07 17:24:22 +01003475 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003476 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003477 for (uint j = 1; j < n_args; j++) {
3478 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003479 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003480 return;
3481 }
Damien George50912e72015-01-20 11:55:10 +00003482 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003483 }
3484 }
Damien826005c2013-10-05 23:17:28 +01003485 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003486 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003487 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003488 }
3489 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003490
3491 if (comp->compile_error != MP_OBJ_NULL) {
3492 pns = pns2; // this is the parse node that had the error
3493 goto inline_asm_error;
3494 }
Damien826005c2013-10-05 23:17:28 +01003495 }
3496
Damien George36db6bc2014-05-07 17:24:22 +01003497 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003498 EMIT_INLINE_ASM(end_pass);
3499 }
3500
3501 if (comp->compile_error != MP_OBJ_NULL) {
3502 // inline assembler had an error; add traceback to its exception
3503 inline_asm_error:
3504 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, (mp_uint_t)pns->source_line, comp->scope_cur->simple_name);
Damienb05d7072013-10-05 13:37:10 +01003505 }
Damien429d7192013-10-04 19:53:11 +01003506}
Damien George094d4502014-04-02 17:31:27 +01003507#endif
Damien429d7192013-10-04 19:53:11 +01003508
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003509STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003510#if !MICROPY_EMIT_CPYTHON
3511 // in Micro Python we put the *x parameter after all other parameters (except **y)
3512 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3513 id_info_t *id_param = NULL;
3514 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3515 id_info_t *id = &scope->id_info[i];
3516 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3517 if (id_param != NULL) {
3518 // swap star param with last param
3519 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3520 }
3521 break;
3522 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3523 id_param = id;
3524 }
3525 }
3526 }
3527#endif
3528
Damien429d7192013-10-04 19:53:11 +01003529 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003530 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003531 scope->num_locals = 0;
3532 for (int i = 0; i < scope->id_info_len; i++) {
3533 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003534 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003535 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3536 continue;
3537 }
3538 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3539 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3540 }
Damien George2827d622014-04-27 15:50:52 +01003541 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003542 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003543 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003544 }
3545 }
3546
3547 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003548#if MICROPY_EMIT_CPYTHON
3549 int num_cell = 0;
3550#endif
Damien9ecbcff2013-12-11 00:41:43 +00003551 for (int i = 0; i < scope->id_info_len; i++) {
3552 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003553#if MICROPY_EMIT_CPYTHON
3554 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003555 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003556 id->local_num = num_cell;
3557 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003558 }
Damien George6baf76e2013-12-30 22:32:17 +00003559#else
3560 // in Micro Python the cells come right after the fast locals
3561 // parameters are not counted here, since they remain at the start
3562 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003563 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003564 id->local_num = scope->num_locals;
3565 scope->num_locals += 1;
3566 }
3567#endif
Damien9ecbcff2013-12-11 00:41:43 +00003568 }
Damien9ecbcff2013-12-11 00:41:43 +00003569
3570 // compute the index of free vars (freevars[idx] in CPython)
3571 // make sure they are in the order of the parent scope
3572 if (scope->parent != NULL) {
3573 int num_free = 0;
3574 for (int i = 0; i < scope->parent->id_info_len; i++) {
3575 id_info_t *id = &scope->parent->id_info[i];
3576 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3577 for (int j = 0; j < scope->id_info_len; j++) {
3578 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003579 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003580 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003581#if MICROPY_EMIT_CPYTHON
3582 // in CPython the frees are numbered after the cells
3583 id2->local_num = num_cell + num_free;
3584#else
3585 // in Micro Python the frees come first, before the params
3586 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003587#endif
3588 num_free += 1;
3589 }
3590 }
3591 }
Damien429d7192013-10-04 19:53:11 +01003592 }
Damien George6baf76e2013-12-30 22:32:17 +00003593#if !MICROPY_EMIT_CPYTHON
3594 // in Micro Python shift all other locals after the free locals
3595 if (num_free > 0) {
3596 for (int i = 0; i < scope->id_info_len; i++) {
3597 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003598 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003599 id->local_num += num_free;
3600 }
3601 }
Damien George2827d622014-04-27 15:50:52 +01003602 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 +00003603 scope->num_locals += num_free;
3604 }
3605#endif
Damien429d7192013-10-04 19:53:11 +01003606 }
3607
Damien George8725f8f2014-02-15 19:33:11 +00003608 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003609
3610#if MICROPY_EMIT_CPYTHON
3611 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003612 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) {
3613 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003614 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003615 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003616 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 +00003617 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003618 }
3619 }
Damien George882b3632014-04-02 15:56:31 +01003620#endif
3621
Damien429d7192013-10-04 19:53:11 +01003622 int num_free = 0;
3623 for (int i = 0; i < scope->id_info_len; i++) {
3624 id_info_t *id = &scope->id_info[i];
3625 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3626 num_free += 1;
3627 }
3628 }
3629 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003630 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003631 }
3632}
3633
Damien George65cad122014-04-06 11:48:15 +01003634mp_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 +01003635 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003636 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003637 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003638 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003639
Damien George62a3a282015-03-01 12:04:05 +00003640 // create the module scope
3641 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3642
3643 // optimise constants (scope must be set for error messages to work)
3644 comp->scope_cur = module_scope;
Damien Georgeffae48d2014-05-08 15:58:39 +00003645 mp_map_t consts;
3646 mp_map_init(&consts, 0);
Damien George62a3a282015-03-01 12:04:05 +00003647 module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
Damien Georgeffae48d2014-05-08 15:58:39 +00003648 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003649
Damien Georgea210c772015-03-26 15:49:53 +00003650 // create standard emitter; it's used at least for MP_PASS_SCOPE
3651 #if MICROPY_EMIT_CPYTHON
3652 emit_t *emit_cpython = emit_cpython_new();
3653 #else
3654 emit_t *emit_bc = emit_bc_new();
3655 #endif
3656
Damien826005c2013-10-05 23:17:28 +01003657 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003658 #if MICROPY_EMIT_CPYTHON
3659 comp->emit = emit_cpython;
3660 comp->emit_method_table = &emit_cpython_method_table;
3661 #else
3662 comp->emit = emit_bc;
3663 comp->emit_method_table = &emit_bc_method_table;
3664 #endif
Damien Georgea77ffe62015-03-14 12:59:31 +00003665 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003666 comp->emit_inline_asm = NULL;
3667 comp->emit_inline_asm_method_table = NULL;
Damien Georgea77ffe62015-03-14 12:59:31 +00003668 #endif
Damien826005c2013-10-05 23:17:28 +01003669 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003670 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003671 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003672#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003673 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003674 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003675#endif
Damien826005c2013-10-05 23:17:28 +01003676 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003677 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003678 }
3679
3680 // update maximim number of labels needed
3681 if (comp->next_label > max_num_labels) {
3682 max_num_labels = comp->next_label;
3683 }
Damien429d7192013-10-04 19:53:11 +01003684 }
3685
Damien826005c2013-10-05 23:17:28 +01003686 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003687 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003688 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003689 }
3690
Damien Georgea210c772015-03-26 15:49:53 +00003691 // set max number of labels now that it's calculated
3692 #if MICROPY_EMIT_CPYTHON
3693 emit_cpython_set_max_num_labels(emit_cpython, max_num_labels);
3694 #else
3695 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
3696 #endif
3697
Damien826005c2013-10-05 23:17:28 +01003698 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003699#if !MICROPY_EMIT_CPYTHON
Damien Georgee67ed5d2014-01-04 13:55:24 +00003700#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003701 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003702#endif
Damien3ef4abb2013-10-12 16:53:13 +01003703#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003704 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003705#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003706#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003707 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003708 if (false) {
3709 // dummy
3710
Damien3ef4abb2013-10-12 16:53:13 +01003711#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003712 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003713 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003714 if (emit_inline_thumb == NULL) {
3715 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3716 }
3717 comp->emit = NULL;
3718 comp->emit_method_table = NULL;
3719 comp->emit_inline_asm = emit_inline_thumb;
3720 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003721 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003722 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003723 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003724 }
Damienc025ebb2013-10-12 14:30:21 +01003725#endif
3726
Damien826005c2013-10-05 23:17:28 +01003727 } else {
Damienc025ebb2013-10-12 14:30:21 +01003728
3729 // choose the emit type
3730
Damien3ef4abb2013-10-12 16:53:13 +01003731#if MICROPY_EMIT_CPYTHON
Damien Georgea210c772015-03-26 15:49:53 +00003732 comp->emit = emit_cpython;
Damienc025ebb2013-10-12 14:30:21 +01003733 comp->emit_method_table = &emit_cpython_method_table;
3734#else
Damien826005c2013-10-05 23:17:28 +01003735 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003736
3737#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003738 case MP_EMIT_OPT_NATIVE_PYTHON:
3739 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003740#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003741 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003742 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003743 }
Damien13ed3a62013-10-08 09:05:10 +01003744 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003745#elif MICROPY_EMIT_X86
3746 if (emit_native == NULL) {
3747 emit_native = emit_native_x86_new(max_num_labels);
3748 }
3749 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003750#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003751 if (emit_native == NULL) {
3752 emit_native = emit_native_thumb_new(max_num_labels);
3753 }
3754 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003755#elif MICROPY_EMIT_ARM
3756 if (emit_native == NULL) {
3757 emit_native = emit_native_arm_new(max_num_labels);
3758 }
3759 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003760#endif
3761 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003762 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003763 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003764#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003765
Damien826005c2013-10-05 23:17:28 +01003766 default:
Damien826005c2013-10-05 23:17:28 +01003767 comp->emit = emit_bc;
3768 comp->emit_method_table = &emit_bc_method_table;
3769 break;
3770 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003771#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003772
Damien George1e1779e2015-01-14 00:20:28 +00003773 // need a pass to compute stack size
3774 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3775
Damien George36db6bc2014-05-07 17:24:22 +01003776 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003777 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003778 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3779 }
3780
3781 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003782 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003783 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003784 }
Damien6cdd3af2013-10-05 18:08:26 +01003785 }
Damien429d7192013-10-04 19:53:11 +01003786 }
3787
Damien George41d02b62014-01-24 22:42:28 +00003788 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003789
3790#if MICROPY_EMIT_CPYTHON
3791 emit_cpython_free(emit_cpython);
3792#else
3793 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003794#if MICROPY_EMIT_NATIVE
3795 if (emit_native != NULL) {
3796#if MICROPY_EMIT_X64
3797 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003798#elif MICROPY_EMIT_X86
3799 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003800#elif MICROPY_EMIT_THUMB
3801 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003802#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003803 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003804#endif
3805 }
3806#endif
3807#if MICROPY_EMIT_INLINE_THUMB
3808 if (emit_inline_thumb != NULL) {
3809 emit_inline_thumb_free(emit_inline_thumb);
3810 }
3811#endif
Damien Georgea210c772015-03-26 15:49:53 +00003812#endif // MICROPY_EMIT_CPYTHON
Damien George41d02b62014-01-24 22:42:28 +00003813
Damien George52b5d762014-09-23 15:31:56 +00003814 // free the parse tree
Damien George62a3a282015-03-01 12:04:05 +00003815 mp_parse_node_free(module_scope->pn);
Damien George52b5d762014-09-23 15:31:56 +00003816
Damien George41d02b62014-01-24 22:42:28 +00003817 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003818 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003819 for (scope_t *s = module_scope; s;) {
3820 scope_t *next = s->next;
3821 scope_free(s);
3822 s = next;
3823 }
Damien5ac1b2e2013-10-18 19:58:12 +01003824
Damien George41d02b62014-01-24 22:42:28 +00003825 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003826 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003827 m_del_obj(compiler_t, comp);
3828
Damien Georgea91ac202014-10-05 19:01:34 +01003829 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003830 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003831 } else {
3832#if MICROPY_EMIT_CPYTHON
3833 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003834 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003835 return mp_const_true;
3836#else
3837 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003838 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003839#endif
3840 }
Damien429d7192013-10-04 19:53:11 +01003841}