blob: cdd3a5b03076fa51e08802d559baccd44d113301 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
34#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000035#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010037#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010038#include "parse.h"
Damiend99b0522013-12-21 18:17:45 +000039#include "runtime0.h"
Damien George1fb03172014-01-03 14:22:03 +000040#include "obj.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010041#include "emitglue.h"
42#include "scope.h"
43#include "emit.h"
Damien George1fb03172014-01-03 14:22:03 +000044#include "compile.h"
45#include "runtime.h"
Damien Georgea26dc502014-04-12 17:54:52 +010046#include "builtin.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000047#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010048
49// TODO need to mangle __attr names
50
Damience89a212013-10-15 22:25:17 +010051#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
52
Damien429d7192013-10-04 19:53:11 +010053typedef enum {
54 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000055#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010056#include "grammar.h"
57#undef DEF_RULE
58 PN_maximum_number_of,
59} pn_kind_t;
60
Damien Georgeb9791222014-01-23 00:34:21 +000061#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
62#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
63#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
64#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 +010065
66typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000067 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010068 uint8_t is_repl;
69 uint8_t pass; // holds enum type pass_kind_t
70 uint8_t had_error; // try to keep compiler clean from nlr
71 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010072
Damien George6f355fd2014-04-10 14:11:31 +010073 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010074
Damien George6f355fd2014-04-10 14:11:31 +010075 uint break_label;
76 uint continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000077 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010078 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010079
Damien George8b19db02014-04-11 23:25:34 +010080 uint8_t have_star;
Damien George69b89d22014-04-11 13:38:30 +000081 uint16_t num_dict_params;
82 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010083
84 scope_t *scope_head;
85 scope_t *scope_cur;
86
Damien6cdd3af2013-10-05 18:08:26 +010087 emit_t *emit; // current emitter
88 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010089
90 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
91 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +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 Georgef41fdd02014-03-03 23:19:11 +000095 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010096 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
97 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
98 } else {
99 printf(" File \"%s\"\n", qstr_str(comp->source_file));
100 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000101 printf("SyntaxError: %s\n", msg);
102 comp->had_error = true;
103}
104
Damien George57e99eb2014-04-10 22:42:11 +0100105STATIC const mp_map_elem_t mp_constants_table[] = {
106 // Extra constants as defined by a port
107 MICROPY_EXTRA_CONSTANTS
108};
109
110STATIC const mp_map_t mp_constants_map = {
111 .all_keys_are_qstrs = 1,
112 .table_is_fixed_array = 1,
Damien George6d3c5e42014-04-26 10:47:29 +0100113 .used = ARRAY_SIZE(mp_constants_table),
114 .alloc = ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +0100115 .table = (mp_map_elem_t*)mp_constants_table,
116};
117
Damien Georgeffae48d2014-05-08 15:58:39 +0000118// this function is essentially a simple preprocessor
119STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
120 if (0) {
121 // dummy
122#if MICROPY_ENABLE_CONST
123 } else if (MP_PARSE_NODE_IS_ID(pn)) {
124 // lookup identifier in table of dynamic constants
125 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
126 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
127 if (elem != NULL) {
128 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
129 }
130#endif
131 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000132 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100133
Damien Georgeffae48d2014-05-08 15:58:39 +0000134 // fold some parse nodes before folding their arguments
135 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
136#if MICROPY_ENABLE_CONST
137 case PN_expr_stmt:
138 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
139 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
140 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
141 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
142 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
143 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
144 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
145 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
146 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
147 ) {
148 // code to assign dynamic constants: id = const(value)
149
150 // get the id
151 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
152
153 // get the value
154 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
155 pn_value = fold_constants(comp, pn_value, consts);
156 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
157 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
158 break;
159 }
160 machine_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
161
162 // store the value in the table of dynamic constants
163 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
164 if (elem->value != MP_OBJ_NULL) {
165 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
166 break;
167 }
168 elem->value = MP_OBJ_NEW_SMALL_INT(value);
169
170 // replace const(value) with value
171 pns1->nodes[0] = pn_value;
172
173 // finished folding this assignment
174 return pn;
175 }
176 }
177 }
178 break;
179#endif
Damien429d7192013-10-04 19:53:11 +0100180 }
181
Damien Georgeffae48d2014-05-08 15:58:39 +0000182 // fold arguments
183 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
184 for (int i = 0; i < n; i++) {
185 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
186 }
187
188 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000189 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100190 case PN_atom_paren:
191 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
192 // (int)
193 pn = pns->nodes[0];
194 }
195 break;
196
197 case PN_expr:
198 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
199 // int | int
200 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
201 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
202 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
203 }
204 break;
205
206 case PN_and_expr:
207 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
208 // int & int
209 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
210 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
211 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
212 }
213 break;
214
Damien429d7192013-10-04 19:53:11 +0100215 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000216 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100217 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
218 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000219 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100220 // int << int
221 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
222 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
223 }
Damiend99b0522013-12-21 18:17:45 +0000224 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100225 // int >> int
Damiend99b0522013-12-21 18:17:45 +0000226 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100227 } else {
228 // shouldn't happen
229 assert(0);
230 }
231 }
232 break;
233
234 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100235 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000236 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200237 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
238 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000239 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100240 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000241 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000242 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100243 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000244 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100245 } else {
246 // shouldn't happen
247 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100248 }
Damien Georgeaf272592014-04-04 11:21:58 +0000249 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100250 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000251 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100252 }
253 }
254 break;
255
256 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000257 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien Georgeaf272592014-04-04 11:21:58 +0000258 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
259 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000260 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100261 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000262 if (!mp_small_int_mul_overflow(arg0, arg1)) {
263 arg0 *= arg1;
264 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
265 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
266 }
267 }
Damiend99b0522013-12-21 18:17:45 +0000268 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100269 // int / int
270 // pass
Damiend99b0522013-12-21 18:17:45 +0000271 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100272 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000273 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000274 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300275 if (arg1 != 0) {
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_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300278 }
Damien429d7192013-10-04 19:53:11 +0100279 } else {
280 // shouldn't happen
281 assert(0);
282 }
283 }
284 break;
285
286 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000287 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200288 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000289 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100290 // +int
Damiend99b0522013-12-21 18:17:45 +0000291 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
292 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100293 // -int
Damiend99b0522013-12-21 18:17:45 +0000294 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
295 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100296 // ~int
Damiend99b0522013-12-21 18:17:45 +0000297 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100298 } else {
299 // shouldn't happen
300 assert(0);
301 }
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
324 } 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])) {
325 // id.id
326 // look it up in constant table, see if it can be replaced with an integer
327 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
328 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
329 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
330 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
331 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
332 if (elem != NULL) {
333 mp_obj_t dest[2];
334 mp_load_method_maybe(elem->value, q_attr, dest);
335 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
336 machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
337 if (MP_PARSE_FITS_SMALL_INT(val)) {
338 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
339 }
340 }
341 }
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 George2c0842b2014-04-27 16:46:51 +0100351void 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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200386STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000387 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
388 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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200398STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000399 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100400 *nodes = NULL;
401 return 0;
Damiend99b0522013-12-21 18:17:45 +0000402 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100403 *nodes = pn;
404 return 1;
405 } else {
Damiend99b0522013-12-21 18:17:45 +0000406 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
407 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100408 *nodes = pn;
409 return 1;
410 } else {
411 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000412 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100413 }
414 }
415}
416
Damiend99b0522013-12-21 18:17:45 +0000417void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100418}
419
Damiend99b0522013-12-21 18:17:45 +0000420void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
421 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100422 for (int i = 0; i < num_nodes; i++) {
423 compile_node(comp, pns->nodes[i]);
424 }
425}
426
Damien3ef4abb2013-10-12 16:53:13 +0100427#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200428STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000429 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100430 return false;
431 }
Damiend99b0522013-12-21 18:17:45 +0000432 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100433 return false;
434 }
435 return true;
436}
437
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200438STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000439 uint len;
440 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000441 bool has_single_quote = false;
442 bool has_double_quote = false;
443 for (int i = 0; i < len; i++) {
444 if (str[i] == '\'') {
445 has_single_quote = true;
446 } else if (str[i] == '"') {
447 has_double_quote = true;
448 }
449 }
450 if (bytes) {
451 vstr_printf(vstr, "b");
452 }
453 bool quote_single = false;
454 if (has_single_quote && !has_double_quote) {
455 vstr_printf(vstr, "\"");
456 } else {
457 quote_single = true;
458 vstr_printf(vstr, "'");
459 }
460 for (int i = 0; i < len; i++) {
461 if (str[i] == '\n') {
462 vstr_printf(vstr, "\\n");
463 } else if (str[i] == '\\') {
464 vstr_printf(vstr, "\\\\");
465 } else if (str[i] == '\'' && quote_single) {
466 vstr_printf(vstr, "\\'");
467 } else {
468 vstr_printf(vstr, "%c", str[i]);
469 }
470 }
471 if (has_single_quote && !has_double_quote) {
472 vstr_printf(vstr, "\"");
473 } else {
474 vstr_printf(vstr, "'");
475 }
476}
477
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200478STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000479 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200480 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
481 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
482 return;
483 }
484
Damiend99b0522013-12-21 18:17:45 +0000485 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
486 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
487 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000488 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
489 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
490 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
491 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
492 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100493 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000494 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
495 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
496 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100497 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100498 }
499 break;
500 default: assert(0);
501 }
502}
503
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200504STATIC 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 +0100505 int n = 0;
506 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000507 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100508 }
509 int total = n;
510 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000511 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100512 total += 1;
Damien3a205172013-10-12 15:01:56 +0100513 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100514 is_const = false;
515 }
516 }
517 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100518 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100519 is_const = false;
520 break;
521 }
522 }
523 if (total > 0 && is_const) {
524 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000525 vstr_t *vstr = vstr_new();
526 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000527 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000528 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100529 need_comma = true;
530 }
531 for (int i = 0; i < n; i++) {
532 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000533 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100534 }
Damien02f89412013-12-12 15:13:36 +0000535 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100536 need_comma = true;
537 }
538 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000539 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100540 } else {
Damien02f89412013-12-12 15:13:36 +0000541 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100542 }
Damien Georgeb9791222014-01-23 00:34:21 +0000543 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000544 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100545 } else {
Damiend99b0522013-12-21 18:17:45 +0000546 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100547 compile_node(comp, pn);
548 }
549 for (int i = 0; i < n; i++) {
550 compile_node(comp, pns_list->nodes[i]);
551 }
Damien Georgeb9791222014-01-23 00:34:21 +0000552 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100553 }
554}
Damien3a205172013-10-12 15:01:56 +0100555#endif
556
557// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100558STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100559#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100560 cpython_c_tuple(comp, pn, pns_list);
561#else
562 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000563 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100564 compile_node(comp, pn);
565 total += 1;
566 }
567 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000568 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100569 for (int i = 0; i < n; i++) {
570 compile_node(comp, pns_list->nodes[i]);
571 }
572 total += n;
573 }
Damien Georgeb9791222014-01-23 00:34:21 +0000574 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100575#endif
576}
Damien429d7192013-10-04 19:53:11 +0100577
Damiend99b0522013-12-21 18:17:45 +0000578void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100579 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000580 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100581}
582
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200583STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000584 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200585 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100586}
587
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200588STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200589 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100590}
591
Damien3ef4abb2013-10-12 16:53:13 +0100592#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100593// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200594STATIC 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 +0100595 if (node_is_const_false(pn)) {
596 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000597 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100598 }
599 return;
600 } else if (node_is_const_true(pn)) {
601 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000602 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100603 }
604 return;
Damiend99b0522013-12-21 18:17:45 +0000605 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
606 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
607 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
608 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100609 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100610 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100611 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100612 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100613 }
Damien3a205172013-10-12 15:01:56 +0100614 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000615 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100616 } else {
617 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100618 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100619 }
620 }
621 return;
Damiend99b0522013-12-21 18:17:45 +0000622 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100623 if (jump_if == false) {
624 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100625 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100626 }
627 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100628 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100629 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100630 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100631 }
Damien3a205172013-10-12 15:01:56 +0100632 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000633 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100634 }
635 return;
Damiend99b0522013-12-21 18:17:45 +0000636 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100637 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100638 return;
639 }
640 }
641
642 // nothing special, fall back to default compiling for node and jump
643 compile_node(comp, pn);
644 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000645 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100646 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000647 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100648 }
649}
Damien3a205172013-10-12 15:01:56 +0100650#endif
Damien429d7192013-10-04 19:53:11 +0100651
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200652STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100653#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100654 cpython_c_if_cond(comp, pn, jump_if, label, false);
655#else
656 if (node_is_const_false(pn)) {
657 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000658 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100659 }
660 return;
661 } else if (node_is_const_true(pn)) {
662 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000663 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100664 }
665 return;
Damiend99b0522013-12-21 18:17:45 +0000666 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
667 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
668 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
669 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100670 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100671 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100672 for (int i = 0; i < n - 1; i++) {
673 c_if_cond(comp, pns->nodes[i], true, label2);
674 }
675 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000676 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100677 } else {
678 for (int i = 0; i < n; i++) {
679 c_if_cond(comp, pns->nodes[i], true, label);
680 }
681 }
682 return;
Damiend99b0522013-12-21 18:17:45 +0000683 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100684 if (jump_if == false) {
685 for (int i = 0; i < n; i++) {
686 c_if_cond(comp, pns->nodes[i], false, label);
687 }
688 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100689 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100690 for (int i = 0; i < n - 1; i++) {
691 c_if_cond(comp, pns->nodes[i], false, label2);
692 }
693 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000694 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100695 }
696 return;
Damiend99b0522013-12-21 18:17:45 +0000697 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100698 c_if_cond(comp, pns->nodes[0], !jump_if, label);
699 return;
700 }
701 }
702
703 // nothing special, fall back to default compiling for node and jump
704 compile_node(comp, pn);
705 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000706 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100707 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000708 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100709 }
710#endif
Damien429d7192013-10-04 19:53:11 +0100711}
712
713typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000714void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100715
Damiend99b0522013-12-21 18:17:45 +0000716void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100717 if (assign_kind != ASSIGN_AUG_STORE) {
718 compile_node(comp, pns->nodes[0]);
719 }
720
Damiend99b0522013-12-21 18:17:45 +0000721 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
722 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
723 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
724 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100725 if (assign_kind != ASSIGN_AUG_STORE) {
726 for (int i = 0; i < n - 1; i++) {
727 compile_node(comp, pns1->nodes[i]);
728 }
729 }
Damiend99b0522013-12-21 18:17:45 +0000730 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
731 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100732 }
Damiend99b0522013-12-21 18:17:45 +0000733 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100734 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000735 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100736 if (assign_kind == ASSIGN_AUG_STORE) {
737 EMIT(rot_three);
738 EMIT(store_subscr);
739 } else {
740 compile_node(comp, pns1->nodes[0]);
741 if (assign_kind == ASSIGN_AUG_LOAD) {
742 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100743 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100744 } else {
745 EMIT(store_subscr);
746 }
747 }
Damiend99b0522013-12-21 18:17:45 +0000748 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
749 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100750 if (assign_kind == ASSIGN_AUG_LOAD) {
751 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000752 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100753 } else {
754 if (assign_kind == ASSIGN_AUG_STORE) {
755 EMIT(rot_two);
756 }
Damien Georgeb9791222014-01-23 00:34:21 +0000757 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100758 }
759 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100760 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100761 }
762 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100763 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100764 }
765
Damiend99b0522013-12-21 18:17:45 +0000766 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100767 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100768 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100769
770 return;
771
772cannot_assign:
773 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100774}
775
Damien George0288cf02014-04-11 11:53:00 +0000776// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
777void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
778 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
779
780 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100781 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000782 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
783 EMIT_ARG(unpack_ex, 0, num_tail);
784 have_star_index = 0;
785 }
786 for (int i = 0; i < num_tail; i++) {
787 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100788 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000789 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
790 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100791 } else {
Damien George9d181f62014-04-27 16:55:27 +0100792 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100793 return;
794 }
795 }
796 }
797 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000798 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100799 }
Damien George0288cf02014-04-11 11:53:00 +0000800 if (num_head != 0) {
801 if (0 == have_star_index) {
802 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100803 } else {
Damien George0288cf02014-04-11 11:53:00 +0000804 c_assign(comp, node_head, ASSIGN_STORE);
805 }
806 }
807 for (int i = 0; i < num_tail; i++) {
808 if (num_head + i == have_star_index) {
809 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
810 } else {
811 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100812 }
813 }
814}
815
816// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000817void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100818 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000819 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100820 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000821 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
822 if (MP_PARSE_NODE_IS_ID(pn)) {
823 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100824 switch (assign_kind) {
825 case ASSIGN_STORE:
826 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000827 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100828 break;
829 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000830 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100831 break;
832 }
833 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100834 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100835 return;
836 }
837 } else {
Damiend99b0522013-12-21 18:17:45 +0000838 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
839 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100840 case PN_power:
841 // lhs is an index or attribute
842 c_assign_power(comp, pns, assign_kind);
843 break;
844
845 case PN_testlist_star_expr:
846 case PN_exprlist:
847 // lhs is a tuple
848 if (assign_kind != ASSIGN_STORE) {
849 goto bad_aug;
850 }
Damien George0288cf02014-04-11 11:53:00 +0000851 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100852 break;
853
854 case PN_atom_paren:
855 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000856 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100857 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100858 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000859 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
860 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100861 goto testlist_comp;
862 } else {
863 // parenthesis around 1 item, is just that item
864 pn = pns->nodes[0];
865 goto tail_recursion;
866 }
867 break;
868
869 case PN_atom_bracket:
870 // lhs is something in brackets
871 if (assign_kind != ASSIGN_STORE) {
872 goto bad_aug;
873 }
Damiend99b0522013-12-21 18:17:45 +0000874 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100875 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000876 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000877 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
878 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100879 goto testlist_comp;
880 } else {
881 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000882 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100883 }
884 break;
885
886 default:
Damien George9d181f62014-04-27 16:55:27 +0100887 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100888 }
889 return;
890
891 testlist_comp:
892 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000893 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
894 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
895 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100896 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000897 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000898 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000899 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100900 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000901 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
902 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000903 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100904 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100905 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100906 } else {
907 // sequence with 2 items
908 goto sequence_with_2_items;
909 }
910 } else {
911 // sequence with 2 items
912 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000913 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100914 }
915 return;
916 }
917 return;
918
Damien George9d181f62014-04-27 16:55:27 +0100919 cannot_assign:
920 compile_syntax_error(comp, pn, "can't assign to expression");
921 return;
922
Damien429d7192013-10-04 19:53:11 +0100923 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100924 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100925}
926
927// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100928// if we are not in CPython compatibility mode then:
929// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
930// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
931// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100932void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
933 assert(n_pos_defaults >= 0);
934 assert(n_kw_defaults >= 0);
935
Damien429d7192013-10-04 19:53:11 +0100936 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000937 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100938 int nfree = 0;
939 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000940 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
941 id_info_t *id = &comp->scope_cur->id_info[i];
942 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
943 for (int j = 0; j < this_scope->id_info_len; j++) {
944 id_info_t *id2 = &this_scope->id_info[j];
945 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000946#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000947 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000948#else
949 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100950 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000951#endif
Damien318aec62013-12-10 18:28:17 +0000952 nfree += 1;
953 }
954 }
Damien429d7192013-10-04 19:53:11 +0100955 }
956 }
957 }
Damien429d7192013-10-04 19:53:11 +0100958
959 // make the function/closure
960 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100961 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100962 } else {
Damien George3558f622014-04-20 17:50:40 +0100963 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100964 }
965}
966
Damiend99b0522013-12-21 18:17:45 +0000967void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000968 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100969 comp->have_star = true;
970 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000971 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
972 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100973 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100974 } else {
975 // named star
Damien429d7192013-10-04 19:53:11 +0100976 }
Damien George8b19db02014-04-11 23:25:34 +0100977 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000978
979 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100980 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000981 // TODO do we need to do anything with this?
982
983 } else {
984 mp_parse_node_t pn_id;
985 mp_parse_node_t pn_colon;
986 mp_parse_node_t pn_equal;
987 if (MP_PARSE_NODE_IS_ID(pn)) {
988 // this parameter is just an id
989
990 pn_id = pn;
991 pn_colon = MP_PARSE_NODE_NULL;
992 pn_equal = MP_PARSE_NODE_NULL;
993
994 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
995 // this parameter has a colon and/or equal specifier
996
997 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
998 pn_id = pns->nodes[0];
999 pn_colon = pns->nodes[1];
1000 pn_equal = pns->nodes[2];
1001
1002 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001003 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001004 assert(0);
1005 return;
1006 }
1007
1008 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1009 // this parameter does not have a default value
1010
1011 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001012 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001013 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001014 return;
1015 }
1016
1017 } else {
1018 // this parameter has a default value
1019 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1020
Damien George8b19db02014-04-11 23:25:34 +01001021 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001022 comp->num_dict_params += 1;
Damien Georgee337f1e2014-03-31 15:18:37 +01001023#if !MICROPY_EMIT_CPYTHON
Damien George69b89d22014-04-11 13:38:30 +00001024 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1025 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001026 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1027 // we need to do this here before we start building the map for the default keywords
1028 if (comp->num_default_params > 0) {
1029 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001030 } else {
1031 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001032 }
Damien George69b89d22014-04-11 13:38:30 +00001033 // first default dict param, so make the map
1034 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001035 }
Damien George69b89d22014-04-11 13:38:30 +00001036#endif
Damien George968bf342014-04-27 19:12:05 +01001037 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001038 compile_node(comp, pn_equal);
1039#if !MICROPY_EMIT_CPYTHON
1040 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1041 EMIT(store_map);
1042#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001043 } else {
Damien George69b89d22014-04-11 13:38:30 +00001044 comp->num_default_params += 1;
1045 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001046 }
1047 }
1048
1049 // TODO pn_colon not implemented
1050 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001051 }
1052}
1053
1054// leaves function object on stack
1055// returns function name
Damiend99b0522013-12-21 18:17:45 +00001056qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001057 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001058 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001059 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001060 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001061 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001062 }
1063
1064 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001065 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001066 uint old_num_dict_params = comp->num_dict_params;
1067 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001068
1069 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001070 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001071 comp->num_dict_params = 0;
1072 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001073 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001074
1075 if (comp->had_error) {
1076 return MP_QSTR_NULL;
1077 }
1078
Damien Georgee337f1e2014-03-31 15:18:37 +01001079#if !MICROPY_EMIT_CPYTHON
1080 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001081 // the default keywords args may have already made the tuple; if not, do it now
1082 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001083 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001084 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001085 }
1086#endif
1087
Damien429d7192013-10-04 19:53:11 +01001088 // get the scope for this function
1089 scope_t *fscope = (scope_t*)pns->nodes[4];
1090
1091 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001092 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001093
1094 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001095 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001096 comp->num_dict_params = old_num_dict_params;
1097 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001098
1099 // return its name (the 'f' in "def f(...):")
1100 return fscope->simple_name;
1101}
1102
1103// leaves class object on stack
1104// returns class name
Damiend99b0522013-12-21 18:17:45 +00001105qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001106 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001107 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001108 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001109 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001110 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001111 }
1112
1113 EMIT(load_build_class);
1114
1115 // scope for this class
1116 scope_t *cscope = (scope_t*)pns->nodes[3];
1117
1118 // compile the class
1119 close_over_variables_etc(comp, cscope, 0, 0);
1120
1121 // get its name
Damien George968bf342014-04-27 19:12:05 +01001122 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001123
1124 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001125 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1126 mp_parse_node_t parents = pns->nodes[1];
1127 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1128 parents = MP_PARSE_NODE_NULL;
1129 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001130 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001131 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001132
1133 // return its name (the 'C' in class C(...):")
1134 return cscope->simple_name;
1135}
1136
Damien6cdd3af2013-10-05 18:08:26 +01001137// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001138STATIC 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 +00001139 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001140 return false;
1141 }
1142
1143 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001144 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001145 return true;
1146 }
1147
Damiend99b0522013-12-21 18:17:45 +00001148 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001149 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001150 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001151#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001152 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001153 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001154 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001155 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001156#endif
Damien3ef4abb2013-10-12 16:53:13 +01001157#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001158 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001159 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001160#endif
Damien6cdd3af2013-10-05 18:08:26 +01001161 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001162 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001163 }
1164
1165 return true;
1166}
1167
Damiend99b0522013-12-21 18:17:45 +00001168void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001169 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001170 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001171 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1172
Damien6cdd3af2013-10-05 18:08:26 +01001173 // inherit emit options for this function/class definition
1174 uint emit_options = comp->scope_cur->emit_options;
1175
1176 // compile each decorator
1177 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001178 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001179 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1180 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001181
1182 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001183 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001184 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1185
1186 // check for built-in decorators
1187 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1188 // this was a built-in
1189 num_built_in_decorators += 1;
1190
1191 } else {
1192 // not a built-in, compile normally
1193
1194 // compile the decorator function
1195 compile_node(comp, name_nodes[0]);
1196 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001197 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001198 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001199 }
1200
1201 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001202 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001203 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001204 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001205 compile_node(comp, pns_decorator->nodes[1]);
1206 }
Damien429d7192013-10-04 19:53:11 +01001207 }
1208 }
1209
1210 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001211 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001212 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001213 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001214 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001215 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001216 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001217 } else {
1218 // shouldn't happen
1219 assert(0);
1220 }
1221
1222 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001223 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001224 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001225 }
1226
1227 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001228 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001229}
1230
Damiend99b0522013-12-21 18:17:45 +00001231void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001232 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001233 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001234 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001235}
1236
Damiend99b0522013-12-21 18:17:45 +00001237void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1238 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001239 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001240 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1241 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001242
1243 compile_node(comp, pns->nodes[0]); // base of the power node
1244
Damiend99b0522013-12-21 18:17:45 +00001245 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1246 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1247 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1248 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001249 for (int i = 0; i < n - 1; i++) {
1250 compile_node(comp, pns1->nodes[i]);
1251 }
Damiend99b0522013-12-21 18:17:45 +00001252 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1253 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001254 }
Damiend99b0522013-12-21 18:17:45 +00001255 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001256 // can't delete function calls
1257 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001258 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001259 compile_node(comp, pns1->nodes[0]);
1260 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001261 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1262 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001263 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001264 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001265 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001266 }
1267 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001268 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001269 }
1270
Damiend99b0522013-12-21 18:17:45 +00001271 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001272 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001273 }
Damiend99b0522013-12-21 18:17:45 +00001274 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1275 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1276 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1277 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001278 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1279
Damiend99b0522013-12-21 18:17:45 +00001280 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1281 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1282 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001283 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001284 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001285 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001286 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001287 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001288 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001289 c_del_stmt(comp, pns->nodes[0]);
1290 for (int i = 0; i < n; i++) {
1291 c_del_stmt(comp, pns1->nodes[i]);
1292 }
Damiend99b0522013-12-21 18:17:45 +00001293 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001294 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001295 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001296 } else {
1297 // sequence with 2 items
1298 goto sequence_with_2_items;
1299 }
1300 } else {
1301 // sequence with 2 items
1302 sequence_with_2_items:
1303 c_del_stmt(comp, pns->nodes[0]);
1304 c_del_stmt(comp, pns->nodes[1]);
1305 }
1306 } else {
1307 // tuple with 1 element
1308 c_del_stmt(comp, pn);
1309 }
1310 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001311 // TODO is there anything else to implement?
1312 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001313 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001314
1315 return;
1316
1317cannot_delete:
1318 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001319}
1320
Damiend99b0522013-12-21 18:17:45 +00001321void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001322 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1323}
1324
Damiend99b0522013-12-21 18:17:45 +00001325void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001326 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001327 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001328 }
Damien Georgecbddb272014-02-01 20:08:18 +00001329 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001330}
1331
Damiend99b0522013-12-21 18:17:45 +00001332void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001333 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001334 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001335 }
Damien Georgecbddb272014-02-01 20:08:18 +00001336 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001337}
1338
Damiend99b0522013-12-21 18:17:45 +00001339void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001340 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001341 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001342 return;
1343 }
Damiend99b0522013-12-21 18:17:45 +00001344 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001345 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001346 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001347 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001348 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001349 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1350 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 +01001351
Damien George6f355fd2014-04-10 14:11:31 +01001352 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001353 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1354 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1355 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001356 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001357 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1358 } else {
1359 compile_node(comp, pns->nodes[0]);
1360 }
1361 EMIT(return_value);
1362}
1363
Damiend99b0522013-12-21 18:17:45 +00001364void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001365 compile_node(comp, pns->nodes[0]);
1366 EMIT(pop_top);
1367}
1368
Damiend99b0522013-12-21 18:17:45 +00001369void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1370 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001371 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001372 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001373 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001374 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001375 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001376 compile_node(comp, pns->nodes[0]);
1377 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001378 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001379 } else {
1380 // raise x
1381 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001382 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001383 }
1384}
1385
Damien George635543c2014-04-10 12:56:52 +01001386// q_base holds the base of the name
1387// eg a -> q_base=a
1388// a.b.c -> q_base=a
1389void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001390 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001391 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1392 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001393 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001394 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001395 pn = pns->nodes[0];
1396 is_as = true;
1397 }
Damien George635543c2014-04-10 12:56:52 +01001398 if (MP_PARSE_NODE_IS_NULL(pn)) {
1399 // empty name (eg, from . import x)
1400 *q_base = MP_QSTR_;
1401 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1402 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001403 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001404 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001405 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001406 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001407 }
Damien George635543c2014-04-10 12:56:52 +01001408 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001409 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1410 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1411 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001412 // a name of the form a.b.c
1413 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001414 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001415 }
Damiend99b0522013-12-21 18:17:45 +00001416 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001417 int len = n - 1;
1418 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001419 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001420 }
Damien George55baff42014-01-21 21:40:13 +00001421 byte *q_ptr;
1422 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001423 for (int i = 0; i < n; i++) {
1424 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001425 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001426 }
Damien George55baff42014-01-21 21:40:13 +00001427 uint str_src_len;
1428 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001429 memcpy(str_dest, str_src, str_src_len);
1430 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001431 }
Damien George635543c2014-04-10 12:56:52 +01001432 qstr q_full = qstr_build_end(q_ptr);
1433 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001434 if (is_as) {
1435 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001436 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001437 }
1438 }
1439 } else {
Damien George635543c2014-04-10 12:56:52 +01001440 // shouldn't happen
1441 assert(0);
Damien429d7192013-10-04 19:53:11 +01001442 }
1443 } else {
Damien George635543c2014-04-10 12:56:52 +01001444 // shouldn't happen
1445 assert(0);
Damien429d7192013-10-04 19:53:11 +01001446 }
1447}
1448
Damiend99b0522013-12-21 18:17:45 +00001449void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001450 EMIT_ARG(load_const_small_int, 0); // level 0 import
1451 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1452 qstr q_base;
1453 do_import_name(comp, pn, &q_base);
1454 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001455}
1456
Damiend99b0522013-12-21 18:17:45 +00001457void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001458 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1459}
1460
Damiend99b0522013-12-21 18:17:45 +00001461void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001462 mp_parse_node_t pn_import_source = pns->nodes[0];
1463
1464 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1465 uint import_level = 0;
1466 do {
1467 mp_parse_node_t pn_rel;
1468 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)) {
1469 // This covers relative imports with dots only like "from .. import"
1470 pn_rel = pn_import_source;
1471 pn_import_source = MP_PARSE_NODE_NULL;
1472 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1473 // This covers relative imports starting with dot(s) like "from .foo import"
1474 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1475 pn_rel = pns_2b->nodes[0];
1476 pn_import_source = pns_2b->nodes[1];
1477 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1478 } else {
1479 // Not a relative import
1480 break;
1481 }
1482
1483 // get the list of . and/or ...'s
1484 mp_parse_node_t *nodes;
1485 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1486
1487 // count the total number of .'s
1488 for (int i = 0; i < n; i++) {
1489 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1490 import_level++;
1491 } else {
1492 // should be an MP_TOKEN_ELLIPSIS
1493 import_level += 3;
1494 }
1495 }
1496 } while (0);
1497
Damiend99b0522013-12-21 18:17:45 +00001498 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001499 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001500
1501 // build the "fromlist" tuple
1502#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001503 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001504#else
Damien George708c0732014-04-27 19:23:46 +01001505 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001506 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001507#endif
1508
1509 // do the import
Damien George635543c2014-04-10 12:56:52 +01001510 qstr dummy_q;
1511 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001512 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001513
Damien429d7192013-10-04 19:53:11 +01001514 } else {
Damien George635543c2014-04-10 12:56:52 +01001515 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001516
1517 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001518 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001519 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001520#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001521 {
1522 vstr_t *vstr = vstr_new();
1523 vstr_printf(vstr, "(");
1524 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001525 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1526 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1527 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001528 if (i > 0) {
1529 vstr_printf(vstr, ", ");
1530 }
1531 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001532 uint len;
1533 const byte *str = qstr_data(id2, &len);
1534 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001535 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001536 }
Damien02f89412013-12-12 15:13:36 +00001537 if (n == 1) {
1538 vstr_printf(vstr, ",");
1539 }
1540 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001541 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001542 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001543 }
Damiendb4c3612013-12-10 17:27:24 +00001544#else
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
Damien Georgeb9791222014-01-23 00:34:21 +00001549 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001550 }
Damien Georgeb9791222014-01-23 00:34:21 +00001551 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001552#endif
1553
1554 // do the import
Damien George635543c2014-04-10 12:56:52 +01001555 qstr dummy_q;
1556 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001557 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001558 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1559 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1560 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001562 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001563 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001564 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001565 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001566 }
1567 }
1568 EMIT(pop_top);
1569 }
1570}
1571
Damiend99b0522013-12-21 18:17:45 +00001572void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001573 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001574 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1575 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001576 } else {
Damiend99b0522013-12-21 18:17:45 +00001577 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1578 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001579 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001580 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001581 }
Damien429d7192013-10-04 19:53:11 +01001582 }
1583 }
1584}
1585
Damiend99b0522013-12-21 18:17:45 +00001586void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001587 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001588 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1589 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001590 } else {
Damiend99b0522013-12-21 18:17:45 +00001591 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1592 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001593 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001594 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001595 }
Damien429d7192013-10-04 19:53:11 +01001596 }
1597 }
1598}
1599
Damiend99b0522013-12-21 18:17:45 +00001600void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001601 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001602 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001603 EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001604 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001605 // assertion message
1606 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001607 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001608 }
Damien Georgeb9791222014-01-23 00:34:21 +00001609 EMIT_ARG(raise_varargs, 1);
1610 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001611}
1612
Damiend99b0522013-12-21 18:17:45 +00001613void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001614 // TODO proper and/or short circuiting
1615
Damien George6f355fd2014-04-10 14:11:31 +01001616 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001617
Damien George6f355fd2014-04-10 14:11:31 +01001618 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001619 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1620
1621 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001622
1623 if (
1624#if !MICROPY_EMIT_CPYTHON
1625 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1626 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1627#endif
1628 // optimisation to not jump if last instruction was return
1629 !EMIT(last_emit_was_return_value)
1630 ) {
1631 // jump over elif/else blocks
1632 EMIT_ARG(jump, l_end);
1633 }
1634
Damien Georgeb9791222014-01-23 00:34:21 +00001635 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001636
Damiend99b0522013-12-21 18:17:45 +00001637 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001638 // compile elif blocks
1639
Damiend99b0522013-12-21 18:17:45 +00001640 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001641
Damiend99b0522013-12-21 18:17:45 +00001642 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001643 // multiple elif blocks
1644
Damiend99b0522013-12-21 18:17:45 +00001645 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001646 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001647 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001648 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001649 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1650
1651 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001652 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001653 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001654 }
Damien Georgeb9791222014-01-23 00:34:21 +00001655 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001656 }
1657
1658 } else {
1659 // a single elif block
1660
Damienb05d7072013-10-05 13:37:10 +01001661 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001662 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1663
1664 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001665 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001666 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001667 }
Damien Georgeb9791222014-01-23 00:34:21 +00001668 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001669 }
1670 }
1671
1672 // compile else block
1673 compile_node(comp, pns->nodes[3]); // can be null
1674
Damien Georgeb9791222014-01-23 00:34:21 +00001675 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001676}
1677
Damien Georgecbddb272014-02-01 20:08:18 +00001678#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001679 uint old_break_label = comp->break_label; \
1680 uint old_continue_label = comp->continue_label; \
1681 uint break_label = comp_next_label(comp); \
1682 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001683 comp->break_label = break_label; \
1684 comp->continue_label = continue_label; \
1685 comp->break_continue_except_level = comp->cur_except_level;
1686
1687#define END_BREAK_CONTINUE_BLOCK \
1688 comp->break_label = old_break_label; \
1689 comp->continue_label = old_continue_label; \
1690 comp->break_continue_except_level = comp->cur_except_level;
1691
Damiend99b0522013-12-21 18:17:45 +00001692void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001693 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001694
Damience89a212013-10-15 22:25:17 +01001695 // compared to CPython, we have an optimised version of while loops
1696#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001697 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001698 EMIT_ARG(setup_loop, break_label);
1699 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001700 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1701 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001702 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001703 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001704 }
Damien Georgeb9791222014-01-23 00:34:21 +00001705 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001706 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1707 // this is a small hack to agree with CPython
1708 if (!node_is_const_true(pns->nodes[0])) {
1709 EMIT(pop_block);
1710 }
Damience89a212013-10-15 22:25:17 +01001711#else
Damien George6f355fd2014-04-10 14:11:31 +01001712 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001713 EMIT_ARG(jump, continue_label);
1714 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001715 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001716 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001717 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1718#endif
1719
1720 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001721 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001722
1723 compile_node(comp, pns->nodes[2]); // else
1724
Damien Georgeb9791222014-01-23 00:34:21 +00001725 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001726}
1727
Damienf72fd0e2013-11-06 20:20:49 +00001728// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001729// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1730// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001731void 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 +00001732 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001733
Damien George6f355fd2014-04-10 14:11:31 +01001734 uint top_label = comp_next_label(comp);
1735 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001736
Damien George3ff2d032014-03-31 18:02:22 +01001737 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001738 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001739 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001740
Damien Georgeb9791222014-01-23 00:34:21 +00001741 EMIT_ARG(jump, entry_label);
1742 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001743
Damien George3ff2d032014-03-31 18:02:22 +01001744 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001745 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001746
1747 // store next value to var
1748 c_assign(comp, pn_var, ASSIGN_STORE);
1749
Damienf3822fc2013-11-09 20:12:03 +00001750 // compile body
1751 compile_node(comp, pn_body);
1752
Damien Georgeb9791222014-01-23 00:34:21 +00001753 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001754
Damien George3ff2d032014-03-31 18:02:22 +01001755 // compile: var + step, duplicated on stack
1756 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001757 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001758 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001759 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001760
Damien Georgeb9791222014-01-23 00:34:21 +00001761 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001762
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001763 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001764 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001765 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1766 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001767 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001768 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001769 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001770 }
Damien Georgeb9791222014-01-23 00:34:21 +00001771 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001772
Damien George3ff2d032014-03-31 18:02:22 +01001773 // discard final value of var that failed the loop condition
1774 EMIT(pop_top);
1775
Damienf72fd0e2013-11-06 20:20:49 +00001776 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001777 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001778
1779 compile_node(comp, pn_else);
1780
Damien Georgeb9791222014-01-23 00:34:21 +00001781 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001782}
1783
Damiend99b0522013-12-21 18:17:45 +00001784void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001785#if !MICROPY_EMIT_CPYTHON
1786 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1787 // this is actually slower, but uses no heap memory
1788 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001789 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 +00001790 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001791 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1792 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1793 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1794 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001795 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1796 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001797 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001798 mp_parse_node_t pn_range_start;
1799 mp_parse_node_t pn_range_end;
1800 mp_parse_node_t pn_range_step;
1801 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001802 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001803 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001804 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001805 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001806 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001807 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001808 } else if (n_args == 2) {
1809 pn_range_start = args[0];
1810 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001811 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001812 } else {
1813 pn_range_start = args[0];
1814 pn_range_end = args[1];
1815 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001816 // We need to know sign of step. This is possible only if it's constant
1817 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1818 optimize = false;
1819 }
Damienf72fd0e2013-11-06 20:20:49 +00001820 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001821 }
1822 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001823 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1824 return;
1825 }
1826 }
1827 }
1828#endif
1829
Damien Georgecbddb272014-02-01 20:08:18 +00001830 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001831
Damien George6f355fd2014-04-10 14:11:31 +01001832 uint pop_label = comp_next_label(comp);
1833 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001834
Damience89a212013-10-15 22:25:17 +01001835 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1836#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001837 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001838#endif
1839
Damien429d7192013-10-04 19:53:11 +01001840 compile_node(comp, pns->nodes[1]); // iterator
1841 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001842 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001843 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001844 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1845 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001846 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001847 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001848 }
Damien Georgeb9791222014-01-23 00:34:21 +00001849 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001850 EMIT(for_iter_end);
1851
1852 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001853 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001854
Damience89a212013-10-15 22:25:17 +01001855#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001856 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001857#endif
Damien429d7192013-10-04 19:53:11 +01001858
1859 compile_node(comp, pns->nodes[3]); // else (not tested)
1860
Damien Georgeb9791222014-01-23 00:34:21 +00001861 EMIT_ARG(label_assign, break_label);
1862 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001863}
1864
Damiend99b0522013-12-21 18:17:45 +00001865void 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 +01001866 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001867 uint l1 = comp_next_label(comp);
1868 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001869
Damien Georgeb9791222014-01-23 00:34:21 +00001870 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001871 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001872
Damien429d7192013-10-04 19:53:11 +01001873 compile_node(comp, pn_body); // body
1874 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001875 EMIT_ARG(jump, success_label); // jump over exception handler
1876
1877 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001878 EMIT_ARG(adjust_stack_size, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
Damien George069a35e2014-04-10 17:22:19 +00001879
Damien George6f355fd2014-04-10 14:11:31 +01001880 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001881
1882 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001883 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1884 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001885
1886 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001887 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001888
Damiend99b0522013-12-21 18:17:45 +00001889 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001890 // this is a catch all exception handler
1891 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001892 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001893 return;
1894 }
1895 } else {
1896 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001897 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1898 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1899 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1900 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001901 // handler binds the exception to a local
1902 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001903 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001904 }
1905 }
1906 EMIT(dup_top);
1907 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001908 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001909 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001910 }
1911
1912 EMIT(pop_top);
1913
1914 if (qstr_exception_local == 0) {
1915 EMIT(pop_top);
1916 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001917 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001918 }
1919
1920 EMIT(pop_top);
1921
Damien George6f355fd2014-04-10 14:11:31 +01001922 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001923 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001924 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001925 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001926 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001927 }
1928 compile_node(comp, pns_except->nodes[1]);
1929 if (qstr_exception_local != 0) {
1930 EMIT(pop_block);
1931 }
1932 EMIT(pop_except);
1933 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001934 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1935 EMIT_ARG(label_assign, l3);
1936 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1937 EMIT_ARG(store_id, qstr_exception_local);
1938 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001939
Damien George8dcc0c72014-03-27 10:55:21 +00001940 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001941 EMIT(end_finally);
1942 }
Damien Georgeb9791222014-01-23 00:34:21 +00001943 EMIT_ARG(jump, l2);
1944 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001945 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001946 }
1947
Damien George8dcc0c72014-03-27 10:55:21 +00001948 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001949 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001950 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001951
Damien Georgeb9791222014-01-23 00:34:21 +00001952 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001953 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001954 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001955}
1956
Damiend99b0522013-12-21 18:17:45 +00001957void 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 +01001958 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001959
Damien Georgeb9791222014-01-23 00:34:21 +00001960 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001961 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001962
Damien429d7192013-10-04 19:53:11 +01001963 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001964 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001965 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001966 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001967 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001968 } else {
1969 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1970 }
1971 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001972 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1973 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001974 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001975
Damien George8dcc0c72014-03-27 10:55:21 +00001976 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001977 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001978}
1979
Damiend99b0522013-12-21 18:17:45 +00001980void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1981 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1982 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1983 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001984 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001985 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1986 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001987 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001988 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001989 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001990 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001991 // no finally
1992 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1993 } else {
1994 // have finally
Damiend99b0522013-12-21 18:17:45 +00001995 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 +01001996 }
1997 } else {
1998 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001999 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002000 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002001 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002002 }
2003 } else {
2004 // shouldn't happen
2005 assert(0);
2006 }
2007}
2008
Damiend99b0522013-12-21 18:17:45 +00002009void 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 +01002010 if (n == 0) {
2011 // no more pre-bits, compile the body of the with
2012 compile_node(comp, body);
2013 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002014 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002015 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002016 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002017 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002018 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002019 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002020 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2021 } else {
2022 // this pre-bit is just an expression
2023 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002024 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002025 EMIT(pop_top);
2026 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002027 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002028 // compile additional pre-bits and the body
2029 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2030 // finish this with block
2031 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002032 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2033 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002034 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002035 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002036 EMIT(end_finally);
2037 }
2038}
2039
Damiend99b0522013-12-21 18:17:45 +00002040void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002041 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002042 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002043 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2044 assert(n > 0);
2045
2046 // compile in a nested fashion
2047 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2048}
2049
Damiend99b0522013-12-21 18:17:45 +00002050void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2051 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002052 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2053 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002054 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002055 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002056 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002057 EMIT(pop_top);
2058
Damien429d7192013-10-04 19:53:11 +01002059 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002060 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00002061 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002062 // do nothing with a lonely constant
2063 } else {
2064 compile_node(comp, pns->nodes[0]); // just an expression
2065 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2066 }
Damien429d7192013-10-04 19:53:11 +01002067 }
2068 } else {
Damiend99b0522013-12-21 18:17:45 +00002069 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2070 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002071 if (kind == PN_expr_stmt_augassign) {
2072 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2073 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002074 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002075 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002076 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002077 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2078 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2079 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2080 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2081 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2082 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2083 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2084 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2085 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2086 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2087 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2088 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2089 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002090 }
Damien George7e5fb242014-02-01 22:18:47 +00002091 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002092 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2093 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002094 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2095 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002096 // following CPython, we store left-most first
2097 if (rhs > 0) {
2098 EMIT(dup_top);
2099 }
2100 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2101 for (int i = 0; i < rhs; i++) {
2102 if (i + 1 < rhs) {
2103 EMIT(dup_top);
2104 }
Damiend99b0522013-12-21 18:17:45 +00002105 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002106 }
2107 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002108 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002109 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2110 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2111 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002112 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002113 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2114 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002115 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2116 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2117 // can't optimise when it's a star expression on the lhs
2118 goto no_optimisation;
2119 }
Damien429d7192013-10-04 19:53:11 +01002120 compile_node(comp, pns10->nodes[0]); // rhs
2121 compile_node(comp, pns10->nodes[1]); // rhs
2122 EMIT(rot_two);
2123 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2124 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002125 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2126 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2127 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2128 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002129 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002130 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2131 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002132 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2133 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2134 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2135 // can't optimise when it's a star expression on the lhs
2136 goto no_optimisation;
2137 }
Damien429d7192013-10-04 19:53:11 +01002138 compile_node(comp, pns10->nodes[0]); // rhs
2139 compile_node(comp, pns10->nodes[1]); // rhs
2140 compile_node(comp, pns10->nodes[2]); // rhs
2141 EMIT(rot_three);
2142 EMIT(rot_two);
2143 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2144 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2145 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2146 } else {
Damien George495d7812014-04-08 17:51:47 +01002147 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002148 compile_node(comp, pns1->nodes[0]); // rhs
2149 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2150 }
2151 } else {
2152 // shouldn't happen
2153 assert(0);
2154 }
2155 }
2156}
2157
Damien Georged17926d2014-03-30 13:35:08 +01002158void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002159 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002160 compile_node(comp, pns->nodes[0]);
2161 for (int i = 1; i < num_nodes; i += 1) {
2162 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002163 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002164 }
2165}
2166
Damiend99b0522013-12-21 18:17:45 +00002167void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2168 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2169 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002170
Damien George6f355fd2014-04-10 14:11:31 +01002171 uint l_fail = comp_next_label(comp);
2172 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002173 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2174 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002175 EMIT_ARG(jump, l_end);
2176 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002177 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002178 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002179 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002180}
2181
Damiend99b0522013-12-21 18:17:45 +00002182void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002183 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002184 //mp_parse_node_t pn_params = pns->nodes[0];
2185 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002186
Damien George36db6bc2014-05-07 17:24:22 +01002187 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002188 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002189 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 +01002190 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002191 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002192 }
2193
2194 // get the scope for this lambda
2195 scope_t *this_scope = (scope_t*)pns->nodes[2];
2196
2197 // make the lambda
2198 close_over_variables_etc(comp, this_scope, 0, 0);
2199}
2200
Damiend99b0522013-12-21 18:17:45 +00002201void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002202 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002203 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002204 for (int i = 0; i < n; i += 1) {
2205 compile_node(comp, pns->nodes[i]);
2206 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002207 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002208 }
2209 }
Damien Georgeb9791222014-01-23 00:34:21 +00002210 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002211}
2212
Damiend99b0522013-12-21 18:17:45 +00002213void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002214 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002215 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002216 for (int i = 0; i < n; i += 1) {
2217 compile_node(comp, pns->nodes[i]);
2218 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002219 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002220 }
2221 }
Damien Georgeb9791222014-01-23 00:34:21 +00002222 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002223}
2224
Damiend99b0522013-12-21 18:17:45 +00002225void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002226 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002227 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002228}
2229
Damiend99b0522013-12-21 18:17:45 +00002230void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002231 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002232 compile_node(comp, pns->nodes[0]);
2233 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002234 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002235 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002236 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002237 }
2238 for (int i = 1; i + 1 < num_nodes; i += 2) {
2239 compile_node(comp, pns->nodes[i + 1]);
2240 if (i + 2 < num_nodes) {
2241 EMIT(dup_top);
2242 EMIT(rot_three);
2243 }
Damien George7e5fb242014-02-01 22:18:47 +00002244 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002245 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002246 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002247 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2248 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2249 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2250 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2251 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2252 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2253 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2254 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002255 }
2256 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002257 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2258 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2259 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002260 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002261 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002262 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002263 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002264 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002265 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002266 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002267 }
2268 } else {
2269 // shouldn't happen
2270 assert(0);
2271 }
2272 } else {
2273 // shouldn't happen
2274 assert(0);
2275 }
2276 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002277 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002278 }
2279 }
2280 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002281 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002282 EMIT_ARG(jump, l_end);
2283 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002284 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002285 EMIT(rot_two);
2286 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002287 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002288 }
2289}
2290
Damiend99b0522013-12-21 18:17:45 +00002291void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002292 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002293}
2294
Damiend99b0522013-12-21 18:17:45 +00002295void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002296 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002297}
2298
Damiend99b0522013-12-21 18:17:45 +00002299void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002300 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002301}
2302
Damiend99b0522013-12-21 18:17:45 +00002303void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002304 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002305}
2306
Damiend99b0522013-12-21 18:17:45 +00002307void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2308 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002309 compile_node(comp, pns->nodes[0]);
2310 for (int i = 1; i + 1 < num_nodes; i += 2) {
2311 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002312 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002313 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002314 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002315 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002316 } else {
2317 // shouldn't happen
2318 assert(0);
2319 }
2320 }
2321}
2322
Damiend99b0522013-12-21 18:17:45 +00002323void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2324 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002325 compile_node(comp, pns->nodes[0]);
2326 for (int i = 1; i + 1 < num_nodes; i += 2) {
2327 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002328 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002329 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002330 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002331 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002332 } else {
2333 // shouldn't happen
2334 assert(0);
2335 }
2336 }
2337}
2338
Damiend99b0522013-12-21 18:17:45 +00002339void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2340 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002341 compile_node(comp, pns->nodes[0]);
2342 for (int i = 1; i + 1 < num_nodes; i += 2) {
2343 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002344 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002345 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002346 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002347 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002348 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002349 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002350 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002351 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002352 } else {
2353 // shouldn't happen
2354 assert(0);
2355 }
2356 }
2357}
2358
Damiend99b0522013-12-21 18:17:45 +00002359void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002360 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002361 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002362 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002363 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002364 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002365 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002366 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002367 } else {
2368 // shouldn't happen
2369 assert(0);
2370 }
2371}
2372
Damien George35e2a4e2014-02-05 00:51:47 +00002373void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2374 // this is to handle special super() call
2375 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2376
2377 compile_generic_all_nodes(comp, pns);
2378}
2379
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002380STATIC 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 +01002381 // function to call is on top of stack
2382
Damien George35e2a4e2014-02-05 00:51:47 +00002383#if !MICROPY_EMIT_CPYTHON
2384 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002385 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George35e2a4e2014-02-05 00:51:47 +00002386 EMIT_ARG(load_id, MP_QSTR___class__);
2387 // get first argument to function
2388 bool found = false;
2389 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002390 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2391 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George35e2a4e2014-02-05 00:51:47 +00002392 found = true;
2393 break;
2394 }
2395 }
2396 if (!found) {
2397 printf("TypeError: super() call cannot find self\n");
2398 return;
2399 }
Damien George922ddd62014-04-09 12:43:17 +01002400 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002401 return;
2402 }
2403#endif
2404
Damien George2c0842b2014-04-27 16:46:51 +01002405 // get the list of arguments
2406 mp_parse_node_t *args;
2407 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002408
Damien George2c0842b2014-04-27 16:46:51 +01002409 // compile the arguments
2410 // Rather than calling compile_node on the list, we go through the list of args
2411 // explicitly here so that we can count the number of arguments and give sensible
2412 // error messages.
2413 int n_positional = n_positional_extra;
2414 uint n_keyword = 0;
2415 uint star_flags = 0;
2416 for (int i = 0; i < n_args; i++) {
2417 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2418 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2419 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2420 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2421 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2422 return;
2423 }
2424 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2425 compile_node(comp, pns_arg->nodes[0]);
2426 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2427 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2428 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2429 return;
2430 }
2431 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2432 compile_node(comp, pns_arg->nodes[0]);
2433 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2434 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2435 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2436 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2437 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2438 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2439 return;
2440 }
Damien George968bf342014-04-27 19:12:05 +01002441 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002442 compile_node(comp, pns2->nodes[0]);
2443 n_keyword += 1;
2444 } else {
2445 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2446 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2447 n_positional++;
2448 }
2449 } else {
2450 goto normal_argument;
2451 }
2452 } else {
2453 normal_argument:
2454 if (n_keyword > 0) {
2455 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2456 return;
2457 }
2458 compile_node(comp, args[i]);
2459 n_positional++;
2460 }
Damien429d7192013-10-04 19:53:11 +01002461 }
2462
Damien George2c0842b2014-04-27 16:46:51 +01002463 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002464 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002465 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002466 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002467 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002468 }
Damien429d7192013-10-04 19:53:11 +01002469}
2470
Damiend99b0522013-12-21 18:17:45 +00002471void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2472 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002473 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002474 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 +01002475 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002476 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2477 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002478 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002479 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002480 i += 1;
2481 } else {
2482 compile_node(comp, pns->nodes[i]);
2483 }
Damien George35e2a4e2014-02-05 00:51:47 +00002484 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002485 }
2486}
2487
Damiend99b0522013-12-21 18:17:45 +00002488void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002489 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002490 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002491}
2492
Damiend99b0522013-12-21 18:17:45 +00002493void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002494 // a list of strings
Damien63321742013-12-10 17:41:49 +00002495
2496 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002497 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002498 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002499 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002500 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002501 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2502 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2503 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002504 if (i == 0) {
2505 string_kind = pn_kind;
2506 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002507 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002508 return;
2509 }
Damien George55baff42014-01-21 21:40:13 +00002510 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002511 }
Damien63321742013-12-10 17:41:49 +00002512
Damien63321742013-12-10 17:41:49 +00002513 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002514 byte *q_ptr;
2515 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002516 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002517 uint s_len;
2518 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002519 memcpy(s_dest, s, s_len);
2520 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002521 }
Damien George55baff42014-01-21 21:40:13 +00002522 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002523
Damien Georgeb9791222014-01-23 00:34:21 +00002524 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002525}
2526
2527// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002528void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2529 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2530 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2531 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002532
Damien George36db6bc2014-05-07 17:24:22 +01002533 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002534 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002535 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 +01002536 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002537 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002538 }
2539
2540 // get the scope for this comprehension
2541 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2542
2543 // compile the comprehension
2544 close_over_variables_etc(comp, this_scope, 0, 0);
2545
2546 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2547 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002548 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002549}
2550
Damiend99b0522013-12-21 18:17:45 +00002551void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2552 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002553 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002554 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2555 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2556 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2557 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2558 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2559 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2560 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002561 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002562 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002563 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002564 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002565 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002566 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002567 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002568 // generator expression
2569 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2570 } else {
2571 // tuple with 2 items
2572 goto tuple_with_2_items;
2573 }
2574 } else {
2575 // tuple with 2 items
2576 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002577 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002578 }
2579 } else {
2580 // parenthesis around a single item, is just that item
2581 compile_node(comp, pns->nodes[0]);
2582 }
2583}
2584
Damiend99b0522013-12-21 18:17:45 +00002585void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2586 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002587 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002588 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002589 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2590 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2591 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2592 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2593 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002594 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002595 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002596 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002597 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002598 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002599 // list of many items
2600 compile_node(comp, pns2->nodes[0]);
2601 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002602 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002603 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002604 // list comprehension
2605 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2606 } else {
2607 // list with 2 items
2608 goto list_with_2_items;
2609 }
2610 } else {
2611 // list with 2 items
2612 list_with_2_items:
2613 compile_node(comp, pns2->nodes[0]);
2614 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002615 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002616 }
2617 } else {
2618 // list with 1 item
2619 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002620 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002621 }
2622}
2623
Damiend99b0522013-12-21 18:17:45 +00002624void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2625 mp_parse_node_t pn = pns->nodes[0];
2626 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002627 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002628 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002629 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2630 pns = (mp_parse_node_struct_t*)pn;
2631 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002632 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002633 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002634 compile_node(comp, pn);
2635 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002636 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2637 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2638 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2639 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002640 // dict/set with multiple elements
2641
2642 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002643 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002644 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2645
2646 // first element sets whether it's a dict or set
2647 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002648 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002649 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002650 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002651 compile_node(comp, pns->nodes[0]);
2652 EMIT(store_map);
2653 is_dict = true;
2654 } else {
2655 // a set
2656 compile_node(comp, pns->nodes[0]); // 1st value of set
2657 is_dict = false;
2658 }
2659
2660 // process rest of elements
2661 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002662 mp_parse_node_t pn = nodes[i];
2663 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002664 compile_node(comp, pn);
2665 if (is_dict) {
2666 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002667 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002668 return;
2669 }
2670 EMIT(store_map);
2671 } else {
2672 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002673 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002674 return;
2675 }
2676 }
2677 }
2678
2679 // if it's a set, build it
2680 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002681 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002682 }
Damiend99b0522013-12-21 18:17:45 +00002683 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002684 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002685 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002686 // a dictionary comprehension
2687 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2688 } else {
2689 // a set comprehension
2690 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2691 }
2692 } else {
2693 // shouldn't happen
2694 assert(0);
2695 }
2696 } else {
2697 // set with one element
2698 goto set_with_one_element;
2699 }
2700 } else {
2701 // set with one element
2702 set_with_one_element:
2703 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002704 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002705 }
2706}
2707
Damiend99b0522013-12-21 18:17:45 +00002708void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002709 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002710}
2711
Damiend99b0522013-12-21 18:17:45 +00002712void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002713 // object who's index we want is on top of stack
2714 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002715 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002716}
2717
Damiend99b0522013-12-21 18:17:45 +00002718void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002719 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002720 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002721}
2722
Damiend99b0522013-12-21 18:17:45 +00002723void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2724 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2725 mp_parse_node_t pn = pns->nodes[0];
2726 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002727 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002728 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2729 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002730 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2731 pns = (mp_parse_node_struct_t*)pn;
2732 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002733 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002734 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002735 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002736 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002737 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002738 } else {
2739 // [?::x]
2740 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002741 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002742 }
Damiend99b0522013-12-21 18:17:45 +00002743 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002744 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002745 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2746 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2747 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2748 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002749 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002750 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002751 } else {
2752 // [?:x:x]
2753 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002754 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002755 }
2756 } else {
2757 // [?:x]
2758 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002759 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002760 }
2761 } else {
2762 // [?:x]
2763 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002764 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002765 }
2766}
2767
Damiend99b0522013-12-21 18:17:45 +00002768void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002769 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002770 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2771 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002772}
2773
Damiend99b0522013-12-21 18:17:45 +00002774void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002775 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002776 compile_subscript_3_helper(comp, pns);
2777}
2778
Damiend99b0522013-12-21 18:17:45 +00002779void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002780 // if this is called then we are compiling a dict key:value pair
2781 compile_node(comp, pns->nodes[1]); // value
2782 compile_node(comp, pns->nodes[0]); // key
2783}
2784
Damiend99b0522013-12-21 18:17:45 +00002785void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002786 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002787 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002788 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002789}
2790
Damiend99b0522013-12-21 18:17:45 +00002791void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002792 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002793 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002794 return;
2795 }
Damiend99b0522013-12-21 18:17:45 +00002796 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002797 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002798 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002799 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2800 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002801 compile_node(comp, pns->nodes[0]);
2802 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002803 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002804 EMIT(yield_from);
2805 } else {
2806 compile_node(comp, pns->nodes[0]);
2807 EMIT(yield_value);
2808 }
2809}
2810
Damiend99b0522013-12-21 18:17:45 +00002811typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002812STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002813 NULL,
2814#define nc NULL
2815#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002816#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002817#include "grammar.h"
2818#undef nc
2819#undef c
2820#undef DEF_RULE
2821};
2822
Damiend99b0522013-12-21 18:17:45 +00002823void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2824 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002825 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002826 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2827 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2828 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002829 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002830 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002831 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002832 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002833 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2834 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2835 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2836 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002837 case MP_PARSE_NODE_TOKEN:
2838 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002839 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002840 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002841 // do nothing
2842 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002843 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002844 }
2845 break;
Damien429d7192013-10-04 19:53:11 +01002846 default: assert(0);
2847 }
2848 } else {
Damiend99b0522013-12-21 18:17:45 +00002849 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002850 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002851 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002852 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002853 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002854#if MICROPY_DEBUG_PRINTERS
2855 mp_parse_node_print(pn, 0);
2856#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002857 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002858 } else {
2859 f(comp, pns);
2860 }
2861 }
2862}
2863
Damiend99b0522013-12-21 18:17:45 +00002864void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
Damien429d7192013-10-04 19:53:11 +01002865 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002866 qstr param_name = MP_QSTR_NULL;
2867 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002868 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2869 if (MP_PARSE_NODE_IS_ID(pn)) {
2870 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002871 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002872 // comes after a star, so counts as a keyword-only parameter
2873 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002874 } else {
Damien George2827d622014-04-27 15:50:52 +01002875 // comes before a star, so counts as a positional parameter
2876 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002877 }
Damienb14de212013-10-06 00:28:28 +01002878 } else {
Damiend99b0522013-12-21 18:17:45 +00002879 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2880 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2881 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2882 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002883 //int node_index = 1; unused
2884 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002885 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002886 // this parameter has an annotation
2887 pn_annotation = pns->nodes[1];
2888 }
2889 //node_index = 2; unused
2890 }
2891 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002892 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002893 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002894 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002895 comp->scope_cur->num_dict_params += 1;
2896 } else {
2897 comp->scope_cur->num_default_params += 1;
2898 }
2899 }
2900 */
Damien George8b19db02014-04-11 23:25:34 +01002901 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002902 // comes after a star, so counts as a keyword-only parameter
2903 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002904 } else {
Damien George2827d622014-04-27 15:50:52 +01002905 // comes before a star, so counts as a positional parameter
2906 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002907 }
Damiend99b0522013-12-21 18:17:45 +00002908 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002909 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002910 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002911 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002912 // bare star
2913 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002914 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002915 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002916 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002917 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002918 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2919 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002920 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002921 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002922 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2923 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002924 pn_annotation = pns->nodes[1];
2925 } else {
2926 // shouldn't happen
2927 assert(0);
2928 }
Damiend99b0522013-12-21 18:17:45 +00002929 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2930 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002931 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002932 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002933 // this parameter has an annotation
2934 pn_annotation = pns->nodes[1];
2935 }
Damien George8725f8f2014-02-15 19:33:11 +00002936 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002937 } else {
Damienb14de212013-10-06 00:28:28 +01002938 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002939 assert(0);
2940 }
Damien429d7192013-10-04 19:53:11 +01002941 }
2942
Damien George2827d622014-04-27 15:50:52 +01002943 if (param_name != MP_QSTR_NULL) {
Damiend99b0522013-12-21 18:17:45 +00002944 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002945 // TODO this parameter has an annotation
2946 }
2947 bool added;
2948 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2949 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002950 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002951 return;
2952 }
Damien429d7192013-10-04 19:53:11 +01002953 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002954 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002955 }
2956}
2957
Damien George2827d622014-04-27 15:50:52 +01002958STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002959 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2960}
2961
Damien George2827d622014-04-27 15:50:52 +01002962STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002963 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2964}
2965
Damiend99b0522013-12-21 18:17:45 +00002966void 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 +01002967 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002968 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002969 // no more nested if/for; compile inner expression
2970 compile_node(comp, pn_inner_expr);
2971 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002972 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002973 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002974 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002975 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002976 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002977 } else {
2978 EMIT(yield_value);
2979 EMIT(pop_top);
2980 }
Damiend99b0522013-12-21 18:17:45 +00002981 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002982 // if condition
Damiend99b0522013-12-21 18:17:45 +00002983 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002984 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2985 pn_iter = pns_comp_if->nodes[1];
2986 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002987 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002988 // for loop
Damiend99b0522013-12-21 18:17:45 +00002989 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002990 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002991 uint l_end2 = comp_next_label(comp);
2992 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002993 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002994 EMIT_ARG(label_assign, l_top2);
2995 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002996 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2997 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 +00002998 EMIT_ARG(jump, l_top2);
2999 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003000 EMIT(for_iter_end);
3001 } else {
3002 // shouldn't happen
3003 assert(0);
3004 }
3005}
3006
Damien George1463c1f2014-04-25 23:52:57 +01003007STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3008#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003009 // see http://www.python.org/dev/peps/pep-0257/
3010
3011 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003012 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003013 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003014 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003015 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003016 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3017 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003018 for (int i = 0; i < num_nodes; i++) {
3019 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003020 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 +00003021 // not a newline, so this is the first statement; finish search
3022 break;
3023 }
3024 }
3025 // 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 +00003026 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003027 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003028 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003029 } else {
3030 return;
3031 }
3032
3033 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003034 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3035 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
3036 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
3037 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
3038 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01003039 compile_node(comp, pns->nodes[0]); // a doc string
3040 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003041 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003042 }
3043 }
3044 }
Damien George1463c1f2014-04-25 23:52:57 +01003045#endif
Damien429d7192013-10-04 19:53:11 +01003046}
3047
Damien George1463c1f2014-04-25 23:52:57 +01003048STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003049 comp->pass = pass;
3050 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003051 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003052 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003053
Damien George36db6bc2014-05-07 17:24:22 +01003054 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003055 // reset maximum stack sizes in scope
3056 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003057 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003058 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003059 }
3060
Damien5ac1b2e2013-10-18 19:58:12 +01003061#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003062 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003063 scope_print_info(scope);
3064 }
Damien5ac1b2e2013-10-18 19:58:12 +01003065#endif
Damien429d7192013-10-04 19:53:11 +01003066
3067 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003068 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3069 assert(scope->kind == SCOPE_MODULE);
3070 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3071 compile_node(comp, pns->nodes[0]); // compile the expression
3072 EMIT(return_value);
3073 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003074 if (!comp->is_repl) {
3075 check_for_doc_string(comp, scope->pn);
3076 }
Damien429d7192013-10-04 19:53:11 +01003077 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003078 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003079 EMIT(return_value);
3080 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003081 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3082 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3083 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003084
3085 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003086 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003087 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003088 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003089 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3090 }
3091
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003092 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003093
3094 compile_node(comp, pns->nodes[3]); // 3 is function body
3095 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003096 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003097 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003098 EMIT(return_value);
3099 }
3100 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003101 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3102 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3103 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003104
3105 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003106 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003107 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003108 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003109 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3110 }
3111
3112 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003113
3114 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3115 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3116 EMIT(pop_top);
3117 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3118 }
Damien429d7192013-10-04 19:53:11 +01003119 EMIT(return_value);
3120 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3121 // a bit of a hack at the moment
3122
Damiend99b0522013-12-21 18:17:45 +00003123 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3124 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3125 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3126 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3127 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003128
Damien George708c0732014-04-27 19:23:46 +01003129 // We need a unique name for the comprehension argument (the iterator).
3130 // CPython uses .0, but we should be able to use anything that won't
3131 // clash with a user defined variable. Best to use an existing qstr,
3132 // so we use the blank qstr.
3133#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003134 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003135#else
3136 qstr qstr_arg = MP_QSTR_;
3137#endif
Damien George36db6bc2014-05-07 17:24:22 +01003138 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003139 bool added;
3140 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3141 assert(added);
3142 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003143 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003144 }
3145
3146 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003147 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003148 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003149 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003150 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003151 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003152 }
3153
Damien George6f355fd2014-04-10 14:11:31 +01003154 uint l_end = comp_next_label(comp);
3155 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003156 EMIT_ARG(load_id, qstr_arg);
3157 EMIT_ARG(label_assign, l_top);
3158 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003159 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3160 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003161 EMIT_ARG(jump, l_top);
3162 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003163 EMIT(for_iter_end);
3164
3165 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003166 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003167 }
3168 EMIT(return_value);
3169 } else {
3170 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003171 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3172 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3173 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003174
Damien George36db6bc2014-05-07 17:24:22 +01003175 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003176 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003177 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003178 assert(added);
3179 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003180 }
3181
Damien Georgeb9791222014-01-23 00:34:21 +00003182 EMIT_ARG(load_id, MP_QSTR___name__);
3183 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003184 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), false); // 0 is class name
Damien Georgeb9791222014-01-23 00:34:21 +00003185 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003186
3187 check_for_doc_string(comp, pns->nodes[2]);
3188 compile_node(comp, pns->nodes[2]); // 2 is class body
3189
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003190 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003191 assert(id != NULL);
3192 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003193 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003194 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003195#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003196 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003197#else
Damien George2bf7c092014-04-09 15:26:46 +01003198 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003199#endif
Damien429d7192013-10-04 19:53:11 +01003200 }
3201 EMIT(return_value);
3202 }
3203
Damien415eb6f2013-10-05 12:19:06 +01003204 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003205
3206 // make sure we match all the exception levels
3207 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003208}
3209
Damien George094d4502014-04-02 17:31:27 +01003210#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003211// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003212STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003213 comp->pass = pass;
3214 comp->scope_cur = scope;
3215 comp->next_label = 1;
3216
3217 if (scope->kind != SCOPE_FUNCTION) {
3218 printf("Error: inline assembler must be a function\n");
3219 return;
3220 }
3221
Damien George36db6bc2014-05-07 17:24:22 +01003222 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003223 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003224 }
3225
Damien826005c2013-10-05 23:17:28 +01003226 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003227 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3228 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3229 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003230
Damiend99b0522013-12-21 18:17:45 +00003231 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003232
Damiena2f2f7d2013-10-06 00:14:13 +01003233 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003234 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003235 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003236 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003237 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003238 }
3239
Damiend99b0522013-12-21 18:17:45 +00003240 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003241
Damiend99b0522013-12-21 18:17:45 +00003242 mp_parse_node_t pn_body = pns->nodes[3]; // body
3243 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003244 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3245
Damien Georgecbd2f742014-01-19 11:48:48 +00003246 /*
Damien George36db6bc2014-05-07 17:24:22 +01003247 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003248 //printf("----\n");
3249 scope_print_info(scope);
3250 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003251 */
Damien826005c2013-10-05 23:17:28 +01003252
3253 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003254 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3255 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003256 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3257 // no instructions
3258 continue;
3259 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3260 // an instruction; fall through
3261 } else {
3262 // not an instruction; error
3263 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3264 return;
3265 }
Damiend99b0522013-12-21 18:17:45 +00003266 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3267 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3268 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3269 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3270 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3271 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3272 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3273 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3274 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3275 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003276 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3277
3278 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003279 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003280 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003281 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003282 return;
3283 }
Damien George6f355fd2014-04-10 14:11:31 +01003284 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003285 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003286 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003287 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003288 } else if (op == MP_QSTR_align) {
3289 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3290 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3291 return;
3292 }
Damien George36db6bc2014-05-07 17:24:22 +01003293 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003294 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3295 }
3296 } else if (op == MP_QSTR_data) {
3297 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3298 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3299 return;
3300 }
Damien George36db6bc2014-05-07 17:24:22 +01003301 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003302 machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
3303 for (uint i = 1; i < n_args; i++) {
3304 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3305 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3306 return;
3307 }
3308 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3309 }
3310 }
Damien826005c2013-10-05 23:17:28 +01003311 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003312 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003313 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003314 }
3315 }
3316 }
3317
Damien George36db6bc2014-05-07 17:24:22 +01003318 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003319 bool success = EMIT_INLINE_ASM(end_pass);
3320 if (!success) {
3321 comp->had_error = true;
3322 }
Damienb05d7072013-10-05 13:37:10 +01003323 }
Damien429d7192013-10-04 19:53:11 +01003324}
Damien George094d4502014-04-02 17:31:27 +01003325#endif
Damien429d7192013-10-04 19:53:11 +01003326
Damien George1463c1f2014-04-25 23:52:57 +01003327STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003328#if !MICROPY_EMIT_CPYTHON
3329 // in Micro Python we put the *x parameter after all other parameters (except **y)
3330 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3331 id_info_t *id_param = NULL;
3332 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3333 id_info_t *id = &scope->id_info[i];
3334 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3335 if (id_param != NULL) {
3336 // swap star param with last param
3337 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3338 }
3339 break;
3340 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3341 id_param = id;
3342 }
3343 }
3344 }
3345#endif
3346
Damien429d7192013-10-04 19:53:11 +01003347 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003348 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003349 scope->num_locals = 0;
3350 for (int i = 0; i < scope->id_info_len; i++) {
3351 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003352 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003353 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3354 continue;
3355 }
3356 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3357 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3358 }
Damien George2827d622014-04-27 15:50:52 +01003359 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003360 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003361 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003362 }
3363 }
3364
3365 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003366#if MICROPY_EMIT_CPYTHON
3367 int num_cell = 0;
3368#endif
Damien9ecbcff2013-12-11 00:41:43 +00003369 for (int i = 0; i < scope->id_info_len; i++) {
3370 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003371#if MICROPY_EMIT_CPYTHON
3372 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003373 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003374 id->local_num = num_cell;
3375 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003376 }
Damien George6baf76e2013-12-30 22:32:17 +00003377#else
3378 // in Micro Python the cells come right after the fast locals
3379 // parameters are not counted here, since they remain at the start
3380 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003381 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003382 id->local_num = scope->num_locals;
3383 scope->num_locals += 1;
3384 }
3385#endif
Damien9ecbcff2013-12-11 00:41:43 +00003386 }
Damien9ecbcff2013-12-11 00:41:43 +00003387
3388 // compute the index of free vars (freevars[idx] in CPython)
3389 // make sure they are in the order of the parent scope
3390 if (scope->parent != NULL) {
3391 int num_free = 0;
3392 for (int i = 0; i < scope->parent->id_info_len; i++) {
3393 id_info_t *id = &scope->parent->id_info[i];
3394 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3395 for (int j = 0; j < scope->id_info_len; j++) {
3396 id_info_t *id2 = &scope->id_info[j];
3397 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003398 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003399#if MICROPY_EMIT_CPYTHON
3400 // in CPython the frees are numbered after the cells
3401 id2->local_num = num_cell + num_free;
3402#else
3403 // in Micro Python the frees come first, before the params
3404 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003405#endif
3406 num_free += 1;
3407 }
3408 }
3409 }
Damien429d7192013-10-04 19:53:11 +01003410 }
Damien George6baf76e2013-12-30 22:32:17 +00003411#if !MICROPY_EMIT_CPYTHON
3412 // in Micro Python shift all other locals after the free locals
3413 if (num_free > 0) {
3414 for (int i = 0; i < scope->id_info_len; i++) {
3415 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003416 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003417 id->local_num += num_free;
3418 }
3419 }
Damien George2827d622014-04-27 15:50:52 +01003420 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 +00003421 scope->num_locals += num_free;
3422 }
3423#endif
Damien429d7192013-10-04 19:53:11 +01003424 }
3425
Damien George8725f8f2014-02-15 19:33:11 +00003426 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003427
3428#if MICROPY_EMIT_CPYTHON
3429 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003430 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) {
3431 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003432 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003433 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003434 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 +00003435 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003436 }
3437 }
Damien George882b3632014-04-02 15:56:31 +01003438#endif
3439
Damien429d7192013-10-04 19:53:11 +01003440 int num_free = 0;
3441 for (int i = 0; i < scope->id_info_len; i++) {
3442 id_info_t *id = &scope->id_info[i];
3443 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3444 num_free += 1;
3445 }
3446 }
3447 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003448 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003449 }
3450}
3451
Damien George65cad122014-04-06 11:48:15 +01003452mp_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 +01003453 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003454 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003455 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003456
Damien826005c2013-10-05 23:17:28 +01003457 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003458 mp_map_t consts;
3459 mp_map_init(&consts, 0);
3460 pn = fold_constants(comp, pn, &consts);
3461 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003462
3463 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003464 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003465
Damien826005c2013-10-05 23:17:28 +01003466 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003467 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003468 comp->emit_method_table = &emit_pass1_method_table;
3469 comp->emit_inline_asm = NULL;
3470 comp->emit_inline_asm_method_table = NULL;
3471 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003472 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003473 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003474#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003475 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003476 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003477#endif
Damien826005c2013-10-05 23:17:28 +01003478 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003479 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003480 }
3481
3482 // update maximim number of labels needed
3483 if (comp->next_label > max_num_labels) {
3484 max_num_labels = comp->next_label;
3485 }
Damien429d7192013-10-04 19:53:11 +01003486 }
3487
Damien826005c2013-10-05 23:17:28 +01003488 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003489 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003490 compile_scope_compute_things(comp, s);
3491 }
3492
Damien826005c2013-10-05 23:17:28 +01003493 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003494 emit_pass1_free(comp->emit);
3495
Damien826005c2013-10-05 23:17:28 +01003496 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003497#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003498 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003499#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003500 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003501#endif
Damien3ef4abb2013-10-12 16:53:13 +01003502#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003503 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003504#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003505#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003506 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003507 if (false) {
3508 // dummy
3509
Damien3ef4abb2013-10-12 16:53:13 +01003510#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003511 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003512 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003513 if (emit_inline_thumb == NULL) {
3514 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3515 }
3516 comp->emit = NULL;
3517 comp->emit_method_table = NULL;
3518 comp->emit_inline_asm = emit_inline_thumb;
3519 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003520 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea26dc502014-04-12 17:54:52 +01003521 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003522 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003523 }
Damienc025ebb2013-10-12 14:30:21 +01003524#endif
3525
Damien826005c2013-10-05 23:17:28 +01003526 } else {
Damienc025ebb2013-10-12 14:30:21 +01003527
3528 // choose the emit type
3529
Damien3ef4abb2013-10-12 16:53:13 +01003530#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003531 comp->emit = emit_cpython_new(max_num_labels);
3532 comp->emit_method_table = &emit_cpython_method_table;
3533#else
Damien826005c2013-10-05 23:17:28 +01003534 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003535
3536#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003537 case MP_EMIT_OPT_NATIVE_PYTHON:
3538 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003539#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003540 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003541 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003542 }
Damien13ed3a62013-10-08 09:05:10 +01003543 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003544#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003545 if (emit_native == NULL) {
3546 emit_native = emit_native_thumb_new(max_num_labels);
3547 }
3548 comp->emit_method_table = &emit_native_thumb_method_table;
3549#endif
3550 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003551 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien George36db6bc2014-05-07 17:24:22 +01003552
3553 // native emitters need an extra pass to compute stack size
3554 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3555
Damien7af3d192013-10-07 00:02:49 +01003556 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003557#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003558
Damien826005c2013-10-05 23:17:28 +01003559 default:
3560 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003561 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003562 }
3563 comp->emit = emit_bc;
3564 comp->emit_method_table = &emit_bc_method_table;
3565 break;
3566 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003567#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003568
Damien George36db6bc2014-05-07 17:24:22 +01003569 // second last pass: compute code size
Damien Georgea26dc502014-04-12 17:54:52 +01003570 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003571 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3572 }
3573
3574 // final pass: emit code
3575 if (!comp->had_error) {
3576 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003577 }
Damien6cdd3af2013-10-05 18:08:26 +01003578 }
Damien429d7192013-10-04 19:53:11 +01003579 }
3580
Damien George41d02b62014-01-24 22:42:28 +00003581 // free the emitters
3582#if !MICROPY_EMIT_CPYTHON
3583 if (emit_bc != NULL) {
3584 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003585 }
Damien George41d02b62014-01-24 22:42:28 +00003586#if MICROPY_EMIT_NATIVE
3587 if (emit_native != NULL) {
3588#if MICROPY_EMIT_X64
3589 emit_native_x64_free(emit_native);
3590#elif MICROPY_EMIT_THUMB
3591 emit_native_thumb_free(emit_native);
3592#endif
3593 }
3594#endif
3595#if MICROPY_EMIT_INLINE_THUMB
3596 if (emit_inline_thumb != NULL) {
3597 emit_inline_thumb_free(emit_inline_thumb);
3598 }
3599#endif
3600#endif // !MICROPY_EMIT_CPYTHON
3601
3602 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003603 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003604 for (scope_t *s = module_scope; s;) {
3605 scope_t *next = s->next;
3606 scope_free(s);
3607 s = next;
3608 }
Damien5ac1b2e2013-10-18 19:58:12 +01003609
Damien George41d02b62014-01-24 22:42:28 +00003610 // free the compiler
3611 bool had_error = comp->had_error;
3612 m_del_obj(compiler_t, comp);
3613
Damien George1fb03172014-01-03 14:22:03 +00003614 if (had_error) {
3615 // TODO return a proper error message
3616 return mp_const_none;
3617 } else {
3618#if MICROPY_EMIT_CPYTHON
3619 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003620 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003621 return mp_const_true;
3622#else
3623 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003624 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003625#endif
3626 }
Damien429d7192013-10-04 19:53:11 +01003627}