blob: fdae6fbcf4030017b0779085a89b9fa5d4e4b181 [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 George1463c1f2014-04-25 23:52:57 +0100118STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000119 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
120 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
121 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100122
123 // fold arguments first
124 for (int i = 0; i < n; i++) {
125 pns->nodes[i] = fold_constants(pns->nodes[i]);
126 }
127
Damien Georgea26dc502014-04-12 17:54:52 +0100128 // now try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000129 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100130 case PN_atom_paren:
131 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
132 // (int)
133 pn = pns->nodes[0];
134 }
135 break;
136
137 case PN_expr:
138 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
139 // int | int
140 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
141 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
142 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
143 }
144 break;
145
146 case PN_and_expr:
147 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
148 // int & int
149 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
150 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
151 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
152 }
153 break;
154
Damien429d7192013-10-04 19:53:11 +0100155 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000156 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 +0100157 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
158 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000159 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100160 // int << int
161 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
162 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
163 }
Damiend99b0522013-12-21 18:17:45 +0000164 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100165 // int >> int
Damiend99b0522013-12-21 18:17:45 +0000166 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100167 } else {
168 // shouldn't happen
169 assert(0);
170 }
171 }
172 break;
173
174 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100175 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000176 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 +0200177 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
178 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000179 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100180 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000181 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000182 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100183 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000184 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100185 } else {
186 // shouldn't happen
187 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100188 }
Damien Georgeaf272592014-04-04 11:21:58 +0000189 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100190 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000191 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100192 }
193 }
194 break;
195
196 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000197 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 +0000198 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
199 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000200 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100201 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000202 if (!mp_small_int_mul_overflow(arg0, arg1)) {
203 arg0 *= arg1;
204 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
205 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
206 }
207 }
Damiend99b0522013-12-21 18:17:45 +0000208 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100209 // int / int
210 // pass
Damiend99b0522013-12-21 18:17:45 +0000211 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100212 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000213 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000214 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300215 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100216 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000217 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 +0300218 }
Damien429d7192013-10-04 19:53:11 +0100219 } else {
220 // shouldn't happen
221 assert(0);
222 }
223 }
224 break;
225
226 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000227 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200228 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000229 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100230 // +int
Damiend99b0522013-12-21 18:17:45 +0000231 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
232 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100233 // -int
Damiend99b0522013-12-21 18:17:45 +0000234 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
235 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100236 // ~int
Damiend99b0522013-12-21 18:17:45 +0000237 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100238 } else {
239 // shouldn't happen
240 assert(0);
241 }
242 }
243 break;
244
245 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100246 if (0) {
247#if MICROPY_EMIT_CPYTHON
248 } 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 +0100249 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100250 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000251 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
252 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200253 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100254 if (power >= 0) {
255 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200256 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100257 for (; power > 0; power--) {
258 ans *= base;
259 }
Damiend99b0522013-12-21 18:17:45 +0000260 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100261 }
262 }
Damien George57e99eb2014-04-10 22:42:11 +0100263#endif
264 } 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])) {
265 // id.id
266 // look it up in constant table, see if it can be replaced with an integer
267 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
268 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
269 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
270 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
271 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
272 if (elem != NULL) {
273 mp_obj_t dest[2];
274 mp_load_method_maybe(elem->value, q_attr, dest);
275 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
276 machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
277 if (MP_PARSE_FITS_SMALL_INT(val)) {
278 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
279 }
280 }
281 }
Damien429d7192013-10-04 19:53:11 +0100282 }
283 break;
284 }
285 }
286
287 return pn;
288}
289
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200290STATIC 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 +0100291void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000292STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100293
Damien George6f355fd2014-04-10 14:11:31 +0100294STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100295 return comp->next_label++;
296}
297
Damien George8dcc0c72014-03-27 10:55:21 +0000298STATIC void compile_increase_except_level(compiler_t *comp) {
299 comp->cur_except_level += 1;
300 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
301 comp->scope_cur->exc_stack_size = comp->cur_except_level;
302 }
303}
304
305STATIC void compile_decrease_except_level(compiler_t *comp) {
306 assert(comp->cur_except_level > 0);
307 comp->cur_except_level -= 1;
308}
309
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200310STATIC 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 +0100311 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100312 scope->parent = comp->scope_cur;
313 scope->next = NULL;
314 if (comp->scope_head == NULL) {
315 comp->scope_head = scope;
316 } else {
317 scope_t *s = comp->scope_head;
318 while (s->next != NULL) {
319 s = s->next;
320 }
321 s->next = scope;
322 }
323 return scope;
324}
325
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200326STATIC 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 +0000327 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
328 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
329 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100330 for (int i = 0; i < num_nodes; i++) {
331 f(comp, pns->nodes[i]);
332 }
Damiend99b0522013-12-21 18:17:45 +0000333 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100334 f(comp, pn);
335 }
336}
337
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200338STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000339 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100340 *nodes = NULL;
341 return 0;
Damiend99b0522013-12-21 18:17:45 +0000342 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100343 *nodes = pn;
344 return 1;
345 } else {
Damiend99b0522013-12-21 18:17:45 +0000346 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
347 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100348 *nodes = pn;
349 return 1;
350 } else {
351 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000352 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100353 }
354 }
355}
356
Damiend99b0522013-12-21 18:17:45 +0000357void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100358}
359
Damiend99b0522013-12-21 18:17:45 +0000360void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
361 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100362 for (int i = 0; i < num_nodes; i++) {
363 compile_node(comp, pns->nodes[i]);
364 }
365}
366
Damien3ef4abb2013-10-12 16:53:13 +0100367#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200368STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000369 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100370 return false;
371 }
Damiend99b0522013-12-21 18:17:45 +0000372 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100373 return false;
374 }
375 return true;
376}
377
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200378STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000379 uint len;
380 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000381 bool has_single_quote = false;
382 bool has_double_quote = false;
383 for (int i = 0; i < len; i++) {
384 if (str[i] == '\'') {
385 has_single_quote = true;
386 } else if (str[i] == '"') {
387 has_double_quote = true;
388 }
389 }
390 if (bytes) {
391 vstr_printf(vstr, "b");
392 }
393 bool quote_single = false;
394 if (has_single_quote && !has_double_quote) {
395 vstr_printf(vstr, "\"");
396 } else {
397 quote_single = true;
398 vstr_printf(vstr, "'");
399 }
400 for (int i = 0; i < len; i++) {
401 if (str[i] == '\n') {
402 vstr_printf(vstr, "\\n");
403 } else if (str[i] == '\\') {
404 vstr_printf(vstr, "\\\\");
405 } else if (str[i] == '\'' && quote_single) {
406 vstr_printf(vstr, "\\'");
407 } else {
408 vstr_printf(vstr, "%c", str[i]);
409 }
410 }
411 if (has_single_quote && !has_double_quote) {
412 vstr_printf(vstr, "\"");
413 } else {
414 vstr_printf(vstr, "'");
415 }
416}
417
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200418STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000419 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200420 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
421 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
422 return;
423 }
424
Damiend99b0522013-12-21 18:17:45 +0000425 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
426 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
427 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000428 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
429 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
430 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
431 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
432 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100433 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000434 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
435 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
436 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100437 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100438 }
439 break;
440 default: assert(0);
441 }
442}
443
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200444STATIC 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 +0100445 int n = 0;
446 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000447 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100448 }
449 int total = n;
450 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000451 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100452 total += 1;
Damien3a205172013-10-12 15:01:56 +0100453 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100454 is_const = false;
455 }
456 }
457 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100458 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100459 is_const = false;
460 break;
461 }
462 }
463 if (total > 0 && is_const) {
464 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000465 vstr_t *vstr = vstr_new();
466 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000467 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000468 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100469 need_comma = true;
470 }
471 for (int i = 0; i < n; i++) {
472 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000473 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100474 }
Damien02f89412013-12-12 15:13:36 +0000475 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100476 need_comma = true;
477 }
478 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000479 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100480 } else {
Damien02f89412013-12-12 15:13:36 +0000481 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100482 }
Damien Georgeb9791222014-01-23 00:34:21 +0000483 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000484 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100485 } else {
Damiend99b0522013-12-21 18:17:45 +0000486 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100487 compile_node(comp, pn);
488 }
489 for (int i = 0; i < n; i++) {
490 compile_node(comp, pns_list->nodes[i]);
491 }
Damien Georgeb9791222014-01-23 00:34:21 +0000492 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100493 }
494}
Damien3a205172013-10-12 15:01:56 +0100495#endif
496
497// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100498STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100499#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100500 cpython_c_tuple(comp, pn, pns_list);
501#else
502 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000503 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100504 compile_node(comp, pn);
505 total += 1;
506 }
507 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000508 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100509 for (int i = 0; i < n; i++) {
510 compile_node(comp, pns_list->nodes[i]);
511 }
512 total += n;
513 }
Damien Georgeb9791222014-01-23 00:34:21 +0000514 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100515#endif
516}
Damien429d7192013-10-04 19:53:11 +0100517
Damiend99b0522013-12-21 18:17:45 +0000518void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100519 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000520 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100521}
522
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200523STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000524 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200525 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100526}
527
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200528STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200529 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 +0100530}
531
Damien3ef4abb2013-10-12 16:53:13 +0100532#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100533// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200534STATIC 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 +0100535 if (node_is_const_false(pn)) {
536 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000537 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100538 }
539 return;
540 } else if (node_is_const_true(pn)) {
541 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000542 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100543 }
544 return;
Damiend99b0522013-12-21 18:17:45 +0000545 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
546 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
547 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
548 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100549 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100550 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100551 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100552 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100553 }
Damien3a205172013-10-12 15:01:56 +0100554 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000555 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100556 } else {
557 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100558 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100559 }
560 }
561 return;
Damiend99b0522013-12-21 18:17:45 +0000562 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100563 if (jump_if == false) {
564 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100565 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100566 }
567 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100568 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100569 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100570 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100571 }
Damien3a205172013-10-12 15:01:56 +0100572 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000573 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100574 }
575 return;
Damiend99b0522013-12-21 18:17:45 +0000576 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100577 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100578 return;
579 }
580 }
581
582 // nothing special, fall back to default compiling for node and jump
583 compile_node(comp, pn);
584 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000585 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100586 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000587 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100588 }
589}
Damien3a205172013-10-12 15:01:56 +0100590#endif
Damien429d7192013-10-04 19:53:11 +0100591
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200592STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100593#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100594 cpython_c_if_cond(comp, pn, jump_if, label, false);
595#else
596 if (node_is_const_false(pn)) {
597 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000598 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100599 }
600 return;
601 } else if (node_is_const_true(pn)) {
602 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000603 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100604 }
605 return;
Damiend99b0522013-12-21 18:17:45 +0000606 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
607 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
608 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
609 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100610 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100611 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100612 for (int i = 0; i < n - 1; i++) {
613 c_if_cond(comp, pns->nodes[i], true, label2);
614 }
615 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000616 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100617 } else {
618 for (int i = 0; i < n; i++) {
619 c_if_cond(comp, pns->nodes[i], true, label);
620 }
621 }
622 return;
Damiend99b0522013-12-21 18:17:45 +0000623 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100624 if (jump_if == false) {
625 for (int i = 0; i < n; i++) {
626 c_if_cond(comp, pns->nodes[i], false, label);
627 }
628 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100629 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100630 for (int i = 0; i < n - 1; i++) {
631 c_if_cond(comp, pns->nodes[i], false, label2);
632 }
633 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000634 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100635 }
636 return;
Damiend99b0522013-12-21 18:17:45 +0000637 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100638 c_if_cond(comp, pns->nodes[0], !jump_if, label);
639 return;
640 }
641 }
642
643 // nothing special, fall back to default compiling for node and jump
644 compile_node(comp, pn);
645 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000646 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100647 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000648 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100649 }
650#endif
Damien429d7192013-10-04 19:53:11 +0100651}
652
653typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000654void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100655
Damiend99b0522013-12-21 18:17:45 +0000656void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100657 if (assign_kind != ASSIGN_AUG_STORE) {
658 compile_node(comp, pns->nodes[0]);
659 }
660
Damiend99b0522013-12-21 18:17:45 +0000661 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
662 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
663 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
664 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100665 if (assign_kind != ASSIGN_AUG_STORE) {
666 for (int i = 0; i < n - 1; i++) {
667 compile_node(comp, pns1->nodes[i]);
668 }
669 }
Damiend99b0522013-12-21 18:17:45 +0000670 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
671 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100672 }
Damiend99b0522013-12-21 18:17:45 +0000673 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100674 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000675 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100676 if (assign_kind == ASSIGN_AUG_STORE) {
677 EMIT(rot_three);
678 EMIT(store_subscr);
679 } else {
680 compile_node(comp, pns1->nodes[0]);
681 if (assign_kind == ASSIGN_AUG_LOAD) {
682 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100683 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100684 } else {
685 EMIT(store_subscr);
686 }
687 }
Damiend99b0522013-12-21 18:17:45 +0000688 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
689 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100690 if (assign_kind == ASSIGN_AUG_LOAD) {
691 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000692 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100693 } else {
694 if (assign_kind == ASSIGN_AUG_STORE) {
695 EMIT(rot_two);
696 }
Damien Georgeb9791222014-01-23 00:34:21 +0000697 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100698 }
699 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100700 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100701 }
702 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100703 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100704 }
705
Damiend99b0522013-12-21 18:17:45 +0000706 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100707 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100708 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100709
710 return;
711
712cannot_assign:
713 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100714}
715
Damien George0288cf02014-04-11 11:53:00 +0000716// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
717void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
718 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
719
720 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100721 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000722 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
723 EMIT_ARG(unpack_ex, 0, num_tail);
724 have_star_index = 0;
725 }
726 for (int i = 0; i < num_tail; i++) {
727 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100728 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000729 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
730 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100731 } else {
Damien George9d181f62014-04-27 16:55:27 +0100732 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100733 return;
734 }
735 }
736 }
737 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000738 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100739 }
Damien George0288cf02014-04-11 11:53:00 +0000740 if (num_head != 0) {
741 if (0 == have_star_index) {
742 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100743 } else {
Damien George0288cf02014-04-11 11:53:00 +0000744 c_assign(comp, node_head, ASSIGN_STORE);
745 }
746 }
747 for (int i = 0; i < num_tail; i++) {
748 if (num_head + i == have_star_index) {
749 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
750 } else {
751 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100752 }
753 }
754}
755
756// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000757void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100758 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000759 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100760 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000761 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
762 if (MP_PARSE_NODE_IS_ID(pn)) {
763 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100764 switch (assign_kind) {
765 case ASSIGN_STORE:
766 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000767 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100768 break;
769 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000770 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100771 break;
772 }
773 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100774 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100775 return;
776 }
777 } else {
Damiend99b0522013-12-21 18:17:45 +0000778 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
779 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100780 case PN_power:
781 // lhs is an index or attribute
782 c_assign_power(comp, pns, assign_kind);
783 break;
784
785 case PN_testlist_star_expr:
786 case PN_exprlist:
787 // lhs is a tuple
788 if (assign_kind != ASSIGN_STORE) {
789 goto bad_aug;
790 }
Damien George0288cf02014-04-11 11:53:00 +0000791 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100792 break;
793
794 case PN_atom_paren:
795 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000796 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100797 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100798 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000799 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
800 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100801 goto testlist_comp;
802 } else {
803 // parenthesis around 1 item, is just that item
804 pn = pns->nodes[0];
805 goto tail_recursion;
806 }
807 break;
808
809 case PN_atom_bracket:
810 // lhs is something in brackets
811 if (assign_kind != ASSIGN_STORE) {
812 goto bad_aug;
813 }
Damiend99b0522013-12-21 18:17:45 +0000814 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100815 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000816 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000817 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
818 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100819 goto testlist_comp;
820 } else {
821 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000822 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100823 }
824 break;
825
826 default:
Damien George9d181f62014-04-27 16:55:27 +0100827 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100828 }
829 return;
830
831 testlist_comp:
832 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000833 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
834 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
835 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100836 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000837 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000838 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000839 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100840 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000841 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
842 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000843 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100844 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100845 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100846 } else {
847 // sequence with 2 items
848 goto sequence_with_2_items;
849 }
850 } else {
851 // sequence with 2 items
852 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000853 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100854 }
855 return;
856 }
857 return;
858
Damien George9d181f62014-04-27 16:55:27 +0100859 cannot_assign:
860 compile_syntax_error(comp, pn, "can't assign to expression");
861 return;
862
Damien429d7192013-10-04 19:53:11 +0100863 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100864 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100865}
866
867// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100868// if we are not in CPython compatibility mode then:
869// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
870// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
871// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100872void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
873 assert(n_pos_defaults >= 0);
874 assert(n_kw_defaults >= 0);
875
Damien429d7192013-10-04 19:53:11 +0100876 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000877 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100878 int nfree = 0;
879 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000880 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
881 id_info_t *id = &comp->scope_cur->id_info[i];
882 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
883 for (int j = 0; j < this_scope->id_info_len; j++) {
884 id_info_t *id2 = &this_scope->id_info[j];
885 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000886#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000887 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000888#else
889 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100890 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000891#endif
Damien318aec62013-12-10 18:28:17 +0000892 nfree += 1;
893 }
894 }
Damien429d7192013-10-04 19:53:11 +0100895 }
896 }
897 }
Damien429d7192013-10-04 19:53:11 +0100898
899 // make the function/closure
900 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100901 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100902 } else {
Damien George3558f622014-04-20 17:50:40 +0100903 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100904 }
905}
906
Damiend99b0522013-12-21 18:17:45 +0000907void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000908 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100909 comp->have_star = true;
910 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000911 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
912 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100913 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100914 } else {
915 // named star
Damien429d7192013-10-04 19:53:11 +0100916 }
Damien George8b19db02014-04-11 23:25:34 +0100917 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000918
919 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100920 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000921 // TODO do we need to do anything with this?
922
923 } else {
924 mp_parse_node_t pn_id;
925 mp_parse_node_t pn_colon;
926 mp_parse_node_t pn_equal;
927 if (MP_PARSE_NODE_IS_ID(pn)) {
928 // this parameter is just an id
929
930 pn_id = pn;
931 pn_colon = MP_PARSE_NODE_NULL;
932 pn_equal = MP_PARSE_NODE_NULL;
933
934 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
935 // this parameter has a colon and/or equal specifier
936
937 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
938 pn_id = pns->nodes[0];
939 pn_colon = pns->nodes[1];
940 pn_equal = pns->nodes[2];
941
942 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100943 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000944 assert(0);
945 return;
946 }
947
948 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
949 // this parameter does not have a default value
950
951 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100952 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100953 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000954 return;
955 }
956
957 } else {
958 // this parameter has a default value
959 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
960
Damien George8b19db02014-04-11 23:25:34 +0100961 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000962 comp->num_dict_params += 1;
Damien Georgee337f1e2014-03-31 15:18:37 +0100963#if !MICROPY_EMIT_CPYTHON
Damien George69b89d22014-04-11 13:38:30 +0000964 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
965 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100966 // in Micro Python we put the default positional parameters into a tuple using the bytecode
967 // we need to do this here before we start building the map for the default keywords
968 if (comp->num_default_params > 0) {
969 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100970 } else {
971 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100972 }
Damien George69b89d22014-04-11 13:38:30 +0000973 // first default dict param, so make the map
974 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000975 }
Damien George69b89d22014-04-11 13:38:30 +0000976#endif
Damien George968bf342014-04-27 19:12:05 +0100977 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +0000978 compile_node(comp, pn_equal);
979#if !MICROPY_EMIT_CPYTHON
980 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
981 EMIT(store_map);
982#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000983 } else {
Damien George69b89d22014-04-11 13:38:30 +0000984 comp->num_default_params += 1;
985 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000986 }
987 }
988
989 // TODO pn_colon not implemented
990 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100991 }
992}
993
994// leaves function object on stack
995// returns function name
Damiend99b0522013-12-21 18:17:45 +0000996qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100997 if (comp->pass == PASS_1) {
998 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000999 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001000 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001001 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001002 }
1003
1004 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001005 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001006 uint old_num_dict_params = comp->num_dict_params;
1007 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001008
1009 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001010 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001011 comp->num_dict_params = 0;
1012 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001013 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001014
1015 if (comp->had_error) {
1016 return MP_QSTR_NULL;
1017 }
1018
Damien Georgee337f1e2014-03-31 15:18:37 +01001019#if !MICROPY_EMIT_CPYTHON
1020 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001021 // the default keywords args may have already made the tuple; if not, do it now
1022 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001023 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001024 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001025 }
1026#endif
1027
Damien429d7192013-10-04 19:53:11 +01001028 // get the scope for this function
1029 scope_t *fscope = (scope_t*)pns->nodes[4];
1030
1031 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001032 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001033
1034 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001035 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001036 comp->num_dict_params = old_num_dict_params;
1037 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001038
1039 // return its name (the 'f' in "def f(...):")
1040 return fscope->simple_name;
1041}
1042
1043// leaves class object on stack
1044// returns class name
Damiend99b0522013-12-21 18:17:45 +00001045qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +01001046 if (comp->pass == PASS_1) {
1047 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001048 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001049 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001050 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001051 }
1052
1053 EMIT(load_build_class);
1054
1055 // scope for this class
1056 scope_t *cscope = (scope_t*)pns->nodes[3];
1057
1058 // compile the class
1059 close_over_variables_etc(comp, cscope, 0, 0);
1060
1061 // get its name
Damien George968bf342014-04-27 19:12:05 +01001062 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001063
1064 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001065 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1066 mp_parse_node_t parents = pns->nodes[1];
1067 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1068 parents = MP_PARSE_NODE_NULL;
1069 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001070 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001071 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001072
1073 // return its name (the 'C' in class C(...):")
1074 return cscope->simple_name;
1075}
1076
Damien6cdd3af2013-10-05 18:08:26 +01001077// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001078STATIC 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 +00001079 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001080 return false;
1081 }
1082
1083 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001084 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001085 return true;
1086 }
1087
Damiend99b0522013-12-21 18:17:45 +00001088 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001089 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001090 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001091#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001092 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001093 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001094 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001095 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001096#endif
Damien3ef4abb2013-10-12 16:53:13 +01001097#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001098 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001099 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001100#endif
Damien6cdd3af2013-10-05 18:08:26 +01001101 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001102 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001103 }
1104
1105 return true;
1106}
1107
Damiend99b0522013-12-21 18:17:45 +00001108void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001109 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001110 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001111 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1112
Damien6cdd3af2013-10-05 18:08:26 +01001113 // inherit emit options for this function/class definition
1114 uint emit_options = comp->scope_cur->emit_options;
1115
1116 // compile each decorator
1117 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001118 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001119 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1120 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001121
1122 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001123 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001124 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1125
1126 // check for built-in decorators
1127 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1128 // this was a built-in
1129 num_built_in_decorators += 1;
1130
1131 } else {
1132 // not a built-in, compile normally
1133
1134 // compile the decorator function
1135 compile_node(comp, name_nodes[0]);
1136 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001137 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001138 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001139 }
1140
1141 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001142 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001143 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001144 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001145 compile_node(comp, pns_decorator->nodes[1]);
1146 }
Damien429d7192013-10-04 19:53:11 +01001147 }
1148 }
1149
1150 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001151 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001152 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001153 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001154 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001155 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001156 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001157 } else {
1158 // shouldn't happen
1159 assert(0);
1160 }
1161
1162 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001163 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001164 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001165 }
1166
1167 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001168 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001169}
1170
Damiend99b0522013-12-21 18:17:45 +00001171void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001172 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001173 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001174 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001175}
1176
Damiend99b0522013-12-21 18:17:45 +00001177void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1178 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001179 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001180 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1181 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001182
1183 compile_node(comp, pns->nodes[0]); // base of the power node
1184
Damiend99b0522013-12-21 18:17:45 +00001185 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1186 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1187 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1188 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001189 for (int i = 0; i < n - 1; i++) {
1190 compile_node(comp, pns1->nodes[i]);
1191 }
Damiend99b0522013-12-21 18:17:45 +00001192 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1193 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001194 }
Damiend99b0522013-12-21 18:17:45 +00001195 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001196 // can't delete function calls
1197 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001198 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001199 compile_node(comp, pns1->nodes[0]);
1200 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001201 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1202 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001203 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001204 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001205 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001206 }
1207 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001208 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001209 }
1210
Damiend99b0522013-12-21 18:17:45 +00001211 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001212 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001213 }
Damiend99b0522013-12-21 18:17:45 +00001214 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1215 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1216 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1217 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001218 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1219
Damiend99b0522013-12-21 18:17:45 +00001220 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1221 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1222 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001223 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001224 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001225 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001226 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001227 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001228 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001229 c_del_stmt(comp, pns->nodes[0]);
1230 for (int i = 0; i < n; i++) {
1231 c_del_stmt(comp, pns1->nodes[i]);
1232 }
Damiend99b0522013-12-21 18:17:45 +00001233 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001234 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001235 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001236 } else {
1237 // sequence with 2 items
1238 goto sequence_with_2_items;
1239 }
1240 } else {
1241 // sequence with 2 items
1242 sequence_with_2_items:
1243 c_del_stmt(comp, pns->nodes[0]);
1244 c_del_stmt(comp, pns->nodes[1]);
1245 }
1246 } else {
1247 // tuple with 1 element
1248 c_del_stmt(comp, pn);
1249 }
1250 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001251 // TODO is there anything else to implement?
1252 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001253 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001254
1255 return;
1256
1257cannot_delete:
1258 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001259}
1260
Damiend99b0522013-12-21 18:17:45 +00001261void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001262 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1263}
1264
Damiend99b0522013-12-21 18:17:45 +00001265void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001266 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001267 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001268 }
Damien Georgecbddb272014-02-01 20:08:18 +00001269 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001270}
1271
Damiend99b0522013-12-21 18:17:45 +00001272void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001273 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001274 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001275 }
Damien Georgecbddb272014-02-01 20:08:18 +00001276 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001277}
1278
Damiend99b0522013-12-21 18:17:45 +00001279void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001280 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001281 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001282 return;
1283 }
Damiend99b0522013-12-21 18:17:45 +00001284 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001285 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001286 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001287 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001288 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001289 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1290 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 +01001291
Damien George6f355fd2014-04-10 14:11:31 +01001292 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001293 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1294 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1295 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001296 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001297 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1298 } else {
1299 compile_node(comp, pns->nodes[0]);
1300 }
1301 EMIT(return_value);
1302}
1303
Damiend99b0522013-12-21 18:17:45 +00001304void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001305 compile_node(comp, pns->nodes[0]);
1306 EMIT(pop_top);
1307}
1308
Damiend99b0522013-12-21 18:17:45 +00001309void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1310 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001311 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001312 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001313 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001314 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001315 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001316 compile_node(comp, pns->nodes[0]);
1317 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001318 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001319 } else {
1320 // raise x
1321 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001322 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001323 }
1324}
1325
Damien George635543c2014-04-10 12:56:52 +01001326// q_base holds the base of the name
1327// eg a -> q_base=a
1328// a.b.c -> q_base=a
1329void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001330 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001331 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1332 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001333 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001334 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001335 pn = pns->nodes[0];
1336 is_as = true;
1337 }
Damien George635543c2014-04-10 12:56:52 +01001338 if (MP_PARSE_NODE_IS_NULL(pn)) {
1339 // empty name (eg, from . import x)
1340 *q_base = MP_QSTR_;
1341 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1342 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001343 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001344 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001345 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001346 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001347 }
Damien George635543c2014-04-10 12:56:52 +01001348 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001349 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1350 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1351 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001352 // a name of the form a.b.c
1353 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001354 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001355 }
Damiend99b0522013-12-21 18:17:45 +00001356 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001357 int len = n - 1;
1358 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001359 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001360 }
Damien George55baff42014-01-21 21:40:13 +00001361 byte *q_ptr;
1362 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001363 for (int i = 0; i < n; i++) {
1364 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001365 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001366 }
Damien George55baff42014-01-21 21:40:13 +00001367 uint str_src_len;
1368 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001369 memcpy(str_dest, str_src, str_src_len);
1370 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001371 }
Damien George635543c2014-04-10 12:56:52 +01001372 qstr q_full = qstr_build_end(q_ptr);
1373 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001374 if (is_as) {
1375 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001376 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001377 }
1378 }
1379 } else {
Damien George635543c2014-04-10 12:56:52 +01001380 // shouldn't happen
1381 assert(0);
Damien429d7192013-10-04 19:53:11 +01001382 }
1383 } else {
Damien George635543c2014-04-10 12:56:52 +01001384 // shouldn't happen
1385 assert(0);
Damien429d7192013-10-04 19:53:11 +01001386 }
1387}
1388
Damiend99b0522013-12-21 18:17:45 +00001389void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001390 EMIT_ARG(load_const_small_int, 0); // level 0 import
1391 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1392 qstr q_base;
1393 do_import_name(comp, pn, &q_base);
1394 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001395}
1396
Damiend99b0522013-12-21 18:17:45 +00001397void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001398 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1399}
1400
Damiend99b0522013-12-21 18:17:45 +00001401void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001402 mp_parse_node_t pn_import_source = pns->nodes[0];
1403
1404 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1405 uint import_level = 0;
1406 do {
1407 mp_parse_node_t pn_rel;
1408 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)) {
1409 // This covers relative imports with dots only like "from .. import"
1410 pn_rel = pn_import_source;
1411 pn_import_source = MP_PARSE_NODE_NULL;
1412 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1413 // This covers relative imports starting with dot(s) like "from .foo import"
1414 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1415 pn_rel = pns_2b->nodes[0];
1416 pn_import_source = pns_2b->nodes[1];
1417 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1418 } else {
1419 // Not a relative import
1420 break;
1421 }
1422
1423 // get the list of . and/or ...'s
1424 mp_parse_node_t *nodes;
1425 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1426
1427 // count the total number of .'s
1428 for (int i = 0; i < n; i++) {
1429 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1430 import_level++;
1431 } else {
1432 // should be an MP_TOKEN_ELLIPSIS
1433 import_level += 3;
1434 }
1435 }
1436 } while (0);
1437
Damiend99b0522013-12-21 18:17:45 +00001438 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001439 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001440
1441 // build the "fromlist" tuple
1442#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001443 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001444#else
Damien George708c0732014-04-27 19:23:46 +01001445 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001446 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001447#endif
1448
1449 // do the import
Damien George635543c2014-04-10 12:56:52 +01001450 qstr dummy_q;
1451 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001452 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001453
Damien429d7192013-10-04 19:53:11 +01001454 } else {
Damien George635543c2014-04-10 12:56:52 +01001455 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001456
1457 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001458 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001459 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001460#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001461 {
1462 vstr_t *vstr = vstr_new();
1463 vstr_printf(vstr, "(");
1464 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001465 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1466 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1467 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001468 if (i > 0) {
1469 vstr_printf(vstr, ", ");
1470 }
1471 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001472 uint len;
1473 const byte *str = qstr_data(id2, &len);
1474 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001475 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001476 }
Damien02f89412013-12-12 15:13:36 +00001477 if (n == 1) {
1478 vstr_printf(vstr, ",");
1479 }
1480 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001481 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001482 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001483 }
Damiendb4c3612013-12-10 17:27:24 +00001484#else
1485 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001486 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1487 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1488 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001489 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001490 }
Damien Georgeb9791222014-01-23 00:34:21 +00001491 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001492#endif
1493
1494 // do the import
Damien George635543c2014-04-10 12:56:52 +01001495 qstr dummy_q;
1496 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001497 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001498 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1499 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1500 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001501 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001502 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001503 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001504 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001505 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001506 }
1507 }
1508 EMIT(pop_top);
1509 }
1510}
1511
Damiend99b0522013-12-21 18:17:45 +00001512void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001513 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001514 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1515 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001516 } else {
Damiend99b0522013-12-21 18:17:45 +00001517 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1518 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001519 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001520 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001521 }
Damien429d7192013-10-04 19:53:11 +01001522 }
1523 }
1524}
1525
Damiend99b0522013-12-21 18:17:45 +00001526void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001527 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001528 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1529 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001530 } else {
Damiend99b0522013-12-21 18:17:45 +00001531 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1532 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001533 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001534 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001535 }
Damien429d7192013-10-04 19:53:11 +01001536 }
1537 }
1538}
1539
Damiend99b0522013-12-21 18:17:45 +00001540void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001541 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001542 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001543 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 +00001544 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001545 // assertion message
1546 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001547 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001548 }
Damien Georgeb9791222014-01-23 00:34:21 +00001549 EMIT_ARG(raise_varargs, 1);
1550 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001551}
1552
Damiend99b0522013-12-21 18:17:45 +00001553void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001554 // TODO proper and/or short circuiting
1555
Damien George6f355fd2014-04-10 14:11:31 +01001556 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001557
Damien George6f355fd2014-04-10 14:11:31 +01001558 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001559 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1560
1561 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001562
1563 if (
1564#if !MICROPY_EMIT_CPYTHON
1565 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1566 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1567#endif
1568 // optimisation to not jump if last instruction was return
1569 !EMIT(last_emit_was_return_value)
1570 ) {
1571 // jump over elif/else blocks
1572 EMIT_ARG(jump, l_end);
1573 }
1574
Damien Georgeb9791222014-01-23 00:34:21 +00001575 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001576
Damiend99b0522013-12-21 18:17:45 +00001577 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001578 // compile elif blocks
1579
Damiend99b0522013-12-21 18:17:45 +00001580 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001581
Damiend99b0522013-12-21 18:17:45 +00001582 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001583 // multiple elif blocks
1584
Damiend99b0522013-12-21 18:17:45 +00001585 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001586 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001587 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001588 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001589 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1590
1591 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001592 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001593 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001594 }
Damien Georgeb9791222014-01-23 00:34:21 +00001595 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001596 }
1597
1598 } else {
1599 // a single elif block
1600
Damienb05d7072013-10-05 13:37:10 +01001601 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001602 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1603
1604 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001605 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001606 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001607 }
Damien Georgeb9791222014-01-23 00:34:21 +00001608 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001609 }
1610 }
1611
1612 // compile else block
1613 compile_node(comp, pns->nodes[3]); // can be null
1614
Damien Georgeb9791222014-01-23 00:34:21 +00001615 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001616}
1617
Damien Georgecbddb272014-02-01 20:08:18 +00001618#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001619 uint old_break_label = comp->break_label; \
1620 uint old_continue_label = comp->continue_label; \
1621 uint break_label = comp_next_label(comp); \
1622 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001623 comp->break_label = break_label; \
1624 comp->continue_label = continue_label; \
1625 comp->break_continue_except_level = comp->cur_except_level;
1626
1627#define END_BREAK_CONTINUE_BLOCK \
1628 comp->break_label = old_break_label; \
1629 comp->continue_label = old_continue_label; \
1630 comp->break_continue_except_level = comp->cur_except_level;
1631
Damiend99b0522013-12-21 18:17:45 +00001632void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001633 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001634
Damience89a212013-10-15 22:25:17 +01001635 // compared to CPython, we have an optimised version of while loops
1636#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001637 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001638 EMIT_ARG(setup_loop, break_label);
1639 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001640 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1641 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001642 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001643 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001644 }
Damien Georgeb9791222014-01-23 00:34:21 +00001645 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001646 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1647 // this is a small hack to agree with CPython
1648 if (!node_is_const_true(pns->nodes[0])) {
1649 EMIT(pop_block);
1650 }
Damience89a212013-10-15 22:25:17 +01001651#else
Damien George6f355fd2014-04-10 14:11:31 +01001652 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001653 EMIT_ARG(jump, continue_label);
1654 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001655 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001656 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001657 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1658#endif
1659
1660 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001661 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001662
1663 compile_node(comp, pns->nodes[2]); // else
1664
Damien Georgeb9791222014-01-23 00:34:21 +00001665 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001666}
1667
Damienf72fd0e2013-11-06 20:20:49 +00001668// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001669// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1670// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001671void 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 +00001672 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001673
Damien George6f355fd2014-04-10 14:11:31 +01001674 uint top_label = comp_next_label(comp);
1675 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001676
Damien George3ff2d032014-03-31 18:02:22 +01001677 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001678 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001679 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001680
Damien Georgeb9791222014-01-23 00:34:21 +00001681 EMIT_ARG(jump, entry_label);
1682 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001683
Damien George3ff2d032014-03-31 18:02:22 +01001684 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001685 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001686
1687 // store next value to var
1688 c_assign(comp, pn_var, ASSIGN_STORE);
1689
Damienf3822fc2013-11-09 20:12:03 +00001690 // compile body
1691 compile_node(comp, pn_body);
1692
Damien Georgeb9791222014-01-23 00:34:21 +00001693 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001694
Damien George3ff2d032014-03-31 18:02:22 +01001695 // compile: var + step, duplicated on stack
1696 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001697 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001698 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001699 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001700
Damien Georgeb9791222014-01-23 00:34:21 +00001701 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001702
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001703 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001704 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001705 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1706 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001707 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001708 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001709 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001710 }
Damien Georgeb9791222014-01-23 00:34:21 +00001711 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001712
Damien George3ff2d032014-03-31 18:02:22 +01001713 // discard final value of var that failed the loop condition
1714 EMIT(pop_top);
1715
Damienf72fd0e2013-11-06 20:20:49 +00001716 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001717 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001718
1719 compile_node(comp, pn_else);
1720
Damien Georgeb9791222014-01-23 00:34:21 +00001721 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001722}
1723
Damiend99b0522013-12-21 18:17:45 +00001724void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001725#if !MICROPY_EMIT_CPYTHON
1726 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1727 // this is actually slower, but uses no heap memory
1728 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001729 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 +00001730 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001731 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1732 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1733 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1734 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001735 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1736 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001737 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001738 mp_parse_node_t pn_range_start;
1739 mp_parse_node_t pn_range_end;
1740 mp_parse_node_t pn_range_step;
1741 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001742 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001743 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001744 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001745 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001746 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001747 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001748 } else if (n_args == 2) {
1749 pn_range_start = args[0];
1750 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001751 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001752 } else {
1753 pn_range_start = args[0];
1754 pn_range_end = args[1];
1755 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001756 // We need to know sign of step. This is possible only if it's constant
1757 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1758 optimize = false;
1759 }
Damienf72fd0e2013-11-06 20:20:49 +00001760 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001761 }
1762 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001763 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1764 return;
1765 }
1766 }
1767 }
1768#endif
1769
Damien Georgecbddb272014-02-01 20:08:18 +00001770 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001771
Damien George6f355fd2014-04-10 14:11:31 +01001772 uint pop_label = comp_next_label(comp);
1773 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001774
Damience89a212013-10-15 22:25:17 +01001775 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1776#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001777 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001778#endif
1779
Damien429d7192013-10-04 19:53:11 +01001780 compile_node(comp, pns->nodes[1]); // iterator
1781 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001782 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001783 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001784 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1785 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001786 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001787 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001788 }
Damien Georgeb9791222014-01-23 00:34:21 +00001789 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001790 EMIT(for_iter_end);
1791
1792 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001793 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001794
Damience89a212013-10-15 22:25:17 +01001795#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001796 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001797#endif
Damien429d7192013-10-04 19:53:11 +01001798
1799 compile_node(comp, pns->nodes[3]); // else (not tested)
1800
Damien Georgeb9791222014-01-23 00:34:21 +00001801 EMIT_ARG(label_assign, break_label);
1802 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001803}
1804
Damiend99b0522013-12-21 18:17:45 +00001805void 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 +01001806 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001807 uint l1 = comp_next_label(comp);
1808 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001809
Damien Georgeb9791222014-01-23 00:34:21 +00001810 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001811 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001812
Damien429d7192013-10-04 19:53:11 +01001813 compile_node(comp, pn_body); // body
1814 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001815 EMIT_ARG(jump, success_label); // jump over exception handler
1816
1817 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001818 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 +00001819
Damien George6f355fd2014-04-10 14:11:31 +01001820 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001821
1822 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001823 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1824 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001825
1826 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001827 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001828
Damiend99b0522013-12-21 18:17:45 +00001829 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001830 // this is a catch all exception handler
1831 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001832 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001833 return;
1834 }
1835 } else {
1836 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001837 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1838 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1839 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1840 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001841 // handler binds the exception to a local
1842 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001843 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001844 }
1845 }
1846 EMIT(dup_top);
1847 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001848 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001849 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001850 }
1851
1852 EMIT(pop_top);
1853
1854 if (qstr_exception_local == 0) {
1855 EMIT(pop_top);
1856 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001857 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001858 }
1859
1860 EMIT(pop_top);
1861
Damien George6f355fd2014-04-10 14:11:31 +01001862 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001863 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001864 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001865 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001866 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001867 }
1868 compile_node(comp, pns_except->nodes[1]);
1869 if (qstr_exception_local != 0) {
1870 EMIT(pop_block);
1871 }
1872 EMIT(pop_except);
1873 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001874 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1875 EMIT_ARG(label_assign, l3);
1876 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1877 EMIT_ARG(store_id, qstr_exception_local);
1878 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001879
Damien George8dcc0c72014-03-27 10:55:21 +00001880 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001881 EMIT(end_finally);
1882 }
Damien Georgeb9791222014-01-23 00:34:21 +00001883 EMIT_ARG(jump, l2);
1884 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001885 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001886 }
1887
Damien George8dcc0c72014-03-27 10:55:21 +00001888 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001889 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001890 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001891
Damien Georgeb9791222014-01-23 00:34:21 +00001892 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001893 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001894 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001895}
1896
Damiend99b0522013-12-21 18:17:45 +00001897void 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 +01001898 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001899
Damien Georgeb9791222014-01-23 00:34:21 +00001900 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001901 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001902
Damien429d7192013-10-04 19:53:11 +01001903 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001904 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001905 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001906 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001907 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001908 } else {
1909 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1910 }
1911 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001912 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1913 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001914 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001915
Damien George8dcc0c72014-03-27 10:55:21 +00001916 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001917 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001918}
1919
Damiend99b0522013-12-21 18:17:45 +00001920void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1921 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1922 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1923 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001924 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001925 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1926 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001927 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001928 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001929 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001930 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001931 // no finally
1932 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1933 } else {
1934 // have finally
Damiend99b0522013-12-21 18:17:45 +00001935 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 +01001936 }
1937 } else {
1938 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001939 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001940 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001941 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001942 }
1943 } else {
1944 // shouldn't happen
1945 assert(0);
1946 }
1947}
1948
Damiend99b0522013-12-21 18:17:45 +00001949void 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 +01001950 if (n == 0) {
1951 // no more pre-bits, compile the body of the with
1952 compile_node(comp, body);
1953 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001954 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001955 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001956 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001957 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001958 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001959 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001960 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1961 } else {
1962 // this pre-bit is just an expression
1963 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001964 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001965 EMIT(pop_top);
1966 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001967 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001968 // compile additional pre-bits and the body
1969 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1970 // finish this with block
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_end);
Damien429d7192013-10-04 19:53:11 +01001974 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001975 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001976 EMIT(end_finally);
1977 }
1978}
1979
Damiend99b0522013-12-21 18:17:45 +00001980void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001981 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001982 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001983 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1984 assert(n > 0);
1985
1986 // compile in a nested fashion
1987 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1988}
1989
Damiend99b0522013-12-21 18:17:45 +00001990void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1991 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001992 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1993 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001994 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001995 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001996 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001997 EMIT(pop_top);
1998
Damien429d7192013-10-04 19:53:11 +01001999 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002000 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00002001 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002002 // do nothing with a lonely constant
2003 } else {
2004 compile_node(comp, pns->nodes[0]); // just an expression
2005 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2006 }
Damien429d7192013-10-04 19:53:11 +01002007 }
2008 } else {
Damiend99b0522013-12-21 18:17:45 +00002009 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2010 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002011 if (kind == PN_expr_stmt_augassign) {
2012 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2013 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002014 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002015 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002016 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002017 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2018 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2019 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2020 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2021 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2022 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2023 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2024 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2025 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2026 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2027 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2028 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2029 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002030 }
Damien George7e5fb242014-02-01 22:18:47 +00002031 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002032 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2033 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002034 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2035 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002036 // following CPython, we store left-most first
2037 if (rhs > 0) {
2038 EMIT(dup_top);
2039 }
2040 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2041 for (int i = 0; i < rhs; i++) {
2042 if (i + 1 < rhs) {
2043 EMIT(dup_top);
2044 }
Damiend99b0522013-12-21 18:17:45 +00002045 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002046 }
2047 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00002048 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2049 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2050 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2051 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002052 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002053 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2054 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002055 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2056 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2057 // can't optimise when it's a star expression on the lhs
2058 goto no_optimisation;
2059 }
Damien429d7192013-10-04 19:53:11 +01002060 compile_node(comp, pns10->nodes[0]); // rhs
2061 compile_node(comp, pns10->nodes[1]); // rhs
2062 EMIT(rot_two);
2063 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2064 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002065 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2066 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2067 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2068 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002069 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002070 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2071 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002072 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2073 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2074 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2075 // can't optimise when it's a star expression on the lhs
2076 goto no_optimisation;
2077 }
Damien429d7192013-10-04 19:53:11 +01002078 compile_node(comp, pns10->nodes[0]); // rhs
2079 compile_node(comp, pns10->nodes[1]); // rhs
2080 compile_node(comp, pns10->nodes[2]); // rhs
2081 EMIT(rot_three);
2082 EMIT(rot_two);
2083 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2084 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2085 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2086 } else {
Damien George495d7812014-04-08 17:51:47 +01002087 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002088 compile_node(comp, pns1->nodes[0]); // rhs
2089 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2090 }
2091 } else {
2092 // shouldn't happen
2093 assert(0);
2094 }
2095 }
2096}
2097
Damien Georged17926d2014-03-30 13:35:08 +01002098void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002099 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002100 compile_node(comp, pns->nodes[0]);
2101 for (int i = 1; i < num_nodes; i += 1) {
2102 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002103 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002104 }
2105}
2106
Damiend99b0522013-12-21 18:17:45 +00002107void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2108 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2109 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002110
Damien George6f355fd2014-04-10 14:11:31 +01002111 uint l_fail = comp_next_label(comp);
2112 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002113 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2114 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002115 EMIT_ARG(jump, l_end);
2116 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002117 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002118 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002119 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002120}
2121
Damiend99b0522013-12-21 18:17:45 +00002122void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002123 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002124 //mp_parse_node_t pn_params = pns->nodes[0];
2125 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002126
2127 if (comp->pass == PASS_1) {
2128 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002129 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 +01002130 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002131 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002132 }
2133
2134 // get the scope for this lambda
2135 scope_t *this_scope = (scope_t*)pns->nodes[2];
2136
2137 // make the lambda
2138 close_over_variables_etc(comp, this_scope, 0, 0);
2139}
2140
Damiend99b0522013-12-21 18:17:45 +00002141void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002142 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002143 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002144 for (int i = 0; i < n; i += 1) {
2145 compile_node(comp, pns->nodes[i]);
2146 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002147 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002148 }
2149 }
Damien Georgeb9791222014-01-23 00:34:21 +00002150 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002151}
2152
Damiend99b0522013-12-21 18:17:45 +00002153void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002154 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002155 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002156 for (int i = 0; i < n; i += 1) {
2157 compile_node(comp, pns->nodes[i]);
2158 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002159 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002160 }
2161 }
Damien Georgeb9791222014-01-23 00:34:21 +00002162 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002163}
2164
Damiend99b0522013-12-21 18:17:45 +00002165void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002166 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002167 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002168}
2169
Damiend99b0522013-12-21 18:17:45 +00002170void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002171 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002172 compile_node(comp, pns->nodes[0]);
2173 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002174 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002175 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002176 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002177 }
2178 for (int i = 1; i + 1 < num_nodes; i += 2) {
2179 compile_node(comp, pns->nodes[i + 1]);
2180 if (i + 2 < num_nodes) {
2181 EMIT(dup_top);
2182 EMIT(rot_three);
2183 }
Damien George7e5fb242014-02-01 22:18:47 +00002184 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002185 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002186 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002187 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2188 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2189 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2190 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2191 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2192 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2193 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2194 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002195 }
2196 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002197 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2198 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2199 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002200 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002201 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002202 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002203 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002204 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002205 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002206 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002207 }
2208 } else {
2209 // shouldn't happen
2210 assert(0);
2211 }
2212 } else {
2213 // shouldn't happen
2214 assert(0);
2215 }
2216 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002217 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002218 }
2219 }
2220 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002221 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002222 EMIT_ARG(jump, l_end);
2223 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002224 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002225 EMIT(rot_two);
2226 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002227 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002228 }
2229}
2230
Damiend99b0522013-12-21 18:17:45 +00002231void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002232 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002233}
2234
Damiend99b0522013-12-21 18:17:45 +00002235void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002236 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002237}
2238
Damiend99b0522013-12-21 18:17:45 +00002239void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002240 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002241}
2242
Damiend99b0522013-12-21 18:17:45 +00002243void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002244 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002245}
2246
Damiend99b0522013-12-21 18:17:45 +00002247void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2248 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002249 compile_node(comp, pns->nodes[0]);
2250 for (int i = 1; i + 1 < num_nodes; i += 2) {
2251 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002252 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002253 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002254 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002255 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002256 } else {
2257 // shouldn't happen
2258 assert(0);
2259 }
2260 }
2261}
2262
Damiend99b0522013-12-21 18:17:45 +00002263void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2264 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002265 compile_node(comp, pns->nodes[0]);
2266 for (int i = 1; i + 1 < num_nodes; i += 2) {
2267 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002268 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002269 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002270 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002271 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002272 } else {
2273 // shouldn't happen
2274 assert(0);
2275 }
2276 }
2277}
2278
Damiend99b0522013-12-21 18:17:45 +00002279void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2280 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002281 compile_node(comp, pns->nodes[0]);
2282 for (int i = 1; i + 1 < num_nodes; i += 2) {
2283 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002284 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002285 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002286 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002287 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002288 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002289 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002290 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002291 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002292 } else {
2293 // shouldn't happen
2294 assert(0);
2295 }
2296 }
2297}
2298
Damiend99b0522013-12-21 18:17:45 +00002299void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002300 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002301 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002302 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002303 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002304 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002305 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002306 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002307 } else {
2308 // shouldn't happen
2309 assert(0);
2310 }
2311}
2312
Damien George35e2a4e2014-02-05 00:51:47 +00002313void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2314 // this is to handle special super() call
2315 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2316
2317 compile_generic_all_nodes(comp, pns);
2318}
2319
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002320STATIC 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 +01002321 // function to call is on top of stack
2322
Damien George35e2a4e2014-02-05 00:51:47 +00002323#if !MICROPY_EMIT_CPYTHON
2324 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002325 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 +00002326 EMIT_ARG(load_id, MP_QSTR___class__);
2327 // get first argument to function
2328 bool found = false;
2329 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002330 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2331 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 +00002332 found = true;
2333 break;
2334 }
2335 }
2336 if (!found) {
2337 printf("TypeError: super() call cannot find self\n");
2338 return;
2339 }
Damien George922ddd62014-04-09 12:43:17 +01002340 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002341 return;
2342 }
2343#endif
2344
Damien George2c0842b2014-04-27 16:46:51 +01002345 // get the list of arguments
2346 mp_parse_node_t *args;
2347 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002348
Damien George2c0842b2014-04-27 16:46:51 +01002349 // compile the arguments
2350 // Rather than calling compile_node on the list, we go through the list of args
2351 // explicitly here so that we can count the number of arguments and give sensible
2352 // error messages.
2353 int n_positional = n_positional_extra;
2354 uint n_keyword = 0;
2355 uint star_flags = 0;
2356 for (int i = 0; i < n_args; i++) {
2357 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2358 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2359 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2360 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2361 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2362 return;
2363 }
2364 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2365 compile_node(comp, pns_arg->nodes[0]);
2366 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2367 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2368 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2369 return;
2370 }
2371 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2372 compile_node(comp, pns_arg->nodes[0]);
2373 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2374 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2375 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2376 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2377 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2378 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2379 return;
2380 }
Damien George968bf342014-04-27 19:12:05 +01002381 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002382 compile_node(comp, pns2->nodes[0]);
2383 n_keyword += 1;
2384 } else {
2385 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2386 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2387 n_positional++;
2388 }
2389 } else {
2390 goto normal_argument;
2391 }
2392 } else {
2393 normal_argument:
2394 if (n_keyword > 0) {
2395 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2396 return;
2397 }
2398 compile_node(comp, args[i]);
2399 n_positional++;
2400 }
Damien429d7192013-10-04 19:53:11 +01002401 }
2402
Damien George2c0842b2014-04-27 16:46:51 +01002403 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002404 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002405 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002406 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002407 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002408 }
Damien429d7192013-10-04 19:53:11 +01002409}
2410
Damiend99b0522013-12-21 18:17:45 +00002411void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2412 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002413 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002414 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 +01002415 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002416 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2417 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002418 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002419 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002420 i += 1;
2421 } else {
2422 compile_node(comp, pns->nodes[i]);
2423 }
Damien George35e2a4e2014-02-05 00:51:47 +00002424 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002425 }
2426}
2427
Damiend99b0522013-12-21 18:17:45 +00002428void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002429 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002430 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002431}
2432
Damiend99b0522013-12-21 18:17:45 +00002433void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002434 // a list of strings
Damien63321742013-12-10 17:41:49 +00002435
2436 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002437 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002438 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002439 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002440 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002441 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2442 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2443 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002444 if (i == 0) {
2445 string_kind = pn_kind;
2446 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002447 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002448 return;
2449 }
Damien George55baff42014-01-21 21:40:13 +00002450 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002451 }
Damien63321742013-12-10 17:41:49 +00002452
Damien63321742013-12-10 17:41:49 +00002453 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002454 byte *q_ptr;
2455 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002456 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002457 uint s_len;
2458 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002459 memcpy(s_dest, s, s_len);
2460 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002461 }
Damien George55baff42014-01-21 21:40:13 +00002462 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002463
Damien Georgeb9791222014-01-23 00:34:21 +00002464 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002465}
2466
2467// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002468void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2469 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2470 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2471 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002472
2473 if (comp->pass == PASS_1) {
2474 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002475 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 +01002476 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002477 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002478 }
2479
2480 // get the scope for this comprehension
2481 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2482
2483 // compile the comprehension
2484 close_over_variables_etc(comp, this_scope, 0, 0);
2485
2486 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2487 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002488 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002489}
2490
Damiend99b0522013-12-21 18:17:45 +00002491void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2492 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002493 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002494 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2495 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2496 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2497 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2498 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2499 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2500 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002501 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002502 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002503 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002504 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002505 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002506 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002507 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002508 // generator expression
2509 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2510 } else {
2511 // tuple with 2 items
2512 goto tuple_with_2_items;
2513 }
2514 } else {
2515 // tuple with 2 items
2516 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002517 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002518 }
2519 } else {
2520 // parenthesis around a single item, is just that item
2521 compile_node(comp, pns->nodes[0]);
2522 }
2523}
2524
Damiend99b0522013-12-21 18:17:45 +00002525void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2526 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002527 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002528 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002529 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2530 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2531 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2532 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2533 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002534 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002535 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002536 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002537 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002538 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002539 // list of many items
2540 compile_node(comp, pns2->nodes[0]);
2541 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002542 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002543 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002544 // list comprehension
2545 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2546 } else {
2547 // list with 2 items
2548 goto list_with_2_items;
2549 }
2550 } else {
2551 // list with 2 items
2552 list_with_2_items:
2553 compile_node(comp, pns2->nodes[0]);
2554 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002555 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002556 }
2557 } else {
2558 // list with 1 item
2559 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002560 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002561 }
2562}
2563
Damiend99b0522013-12-21 18:17:45 +00002564void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2565 mp_parse_node_t pn = pns->nodes[0];
2566 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002567 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002568 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002569 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2570 pns = (mp_parse_node_struct_t*)pn;
2571 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002572 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002573 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002574 compile_node(comp, pn);
2575 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002576 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2577 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2578 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2579 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002580 // dict/set with multiple elements
2581
2582 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002583 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002584 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2585
2586 // first element sets whether it's a dict or set
2587 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002588 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002589 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002590 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002591 compile_node(comp, pns->nodes[0]);
2592 EMIT(store_map);
2593 is_dict = true;
2594 } else {
2595 // a set
2596 compile_node(comp, pns->nodes[0]); // 1st value of set
2597 is_dict = false;
2598 }
2599
2600 // process rest of elements
2601 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002602 mp_parse_node_t pn = nodes[i];
2603 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002604 compile_node(comp, pn);
2605 if (is_dict) {
2606 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002607 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002608 return;
2609 }
2610 EMIT(store_map);
2611 } else {
2612 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002613 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002614 return;
2615 }
2616 }
2617 }
2618
2619 // if it's a set, build it
2620 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002621 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002622 }
Damiend99b0522013-12-21 18:17:45 +00002623 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002624 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002625 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002626 // a dictionary comprehension
2627 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2628 } else {
2629 // a set comprehension
2630 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2631 }
2632 } else {
2633 // shouldn't happen
2634 assert(0);
2635 }
2636 } else {
2637 // set with one element
2638 goto set_with_one_element;
2639 }
2640 } else {
2641 // set with one element
2642 set_with_one_element:
2643 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002644 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002645 }
2646}
2647
Damiend99b0522013-12-21 18:17:45 +00002648void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002649 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002650}
2651
Damiend99b0522013-12-21 18:17:45 +00002652void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002653 // object who's index we want is on top of stack
2654 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002655 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002656}
2657
Damiend99b0522013-12-21 18:17:45 +00002658void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002659 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002660 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002661}
2662
Damiend99b0522013-12-21 18:17:45 +00002663void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2664 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2665 mp_parse_node_t pn = pns->nodes[0];
2666 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002667 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002668 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2669 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002670 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2671 pns = (mp_parse_node_struct_t*)pn;
2672 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002673 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002674 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002675 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002676 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002677 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002678 } else {
2679 // [?::x]
2680 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002681 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002682 }
Damiend99b0522013-12-21 18:17:45 +00002683 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002684 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002685 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2686 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2687 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2688 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002689 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002690 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002691 } else {
2692 // [?:x:x]
2693 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002694 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002695 }
2696 } else {
2697 // [?:x]
2698 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002699 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002700 }
2701 } else {
2702 // [?:x]
2703 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002704 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002705 }
2706}
2707
Damiend99b0522013-12-21 18:17:45 +00002708void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002709 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002710 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2711 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002712}
2713
Damiend99b0522013-12-21 18:17:45 +00002714void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002715 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002716 compile_subscript_3_helper(comp, pns);
2717}
2718
Damiend99b0522013-12-21 18:17:45 +00002719void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002720 // if this is called then we are compiling a dict key:value pair
2721 compile_node(comp, pns->nodes[1]); // value
2722 compile_node(comp, pns->nodes[0]); // key
2723}
2724
Damiend99b0522013-12-21 18:17:45 +00002725void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002726 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002727 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002728 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002729}
2730
Damiend99b0522013-12-21 18:17:45 +00002731void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002732 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002733 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002734 return;
2735 }
Damiend99b0522013-12-21 18:17:45 +00002736 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002737 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002738 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002739 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2740 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002741 compile_node(comp, pns->nodes[0]);
2742 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002743 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002744 EMIT(yield_from);
2745 } else {
2746 compile_node(comp, pns->nodes[0]);
2747 EMIT(yield_value);
2748 }
2749}
2750
Damiend99b0522013-12-21 18:17:45 +00002751typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002752STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002753 NULL,
2754#define nc NULL
2755#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002756#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002757#include "grammar.h"
2758#undef nc
2759#undef c
2760#undef DEF_RULE
2761};
2762
Damiend99b0522013-12-21 18:17:45 +00002763void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2764 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002765 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002766 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2767 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2768 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002769 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002770 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002771 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002772 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002773 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2774 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2775 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2776 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002777 case MP_PARSE_NODE_TOKEN:
2778 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002779 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002780 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002781 // do nothing
2782 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002783 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002784 }
2785 break;
Damien429d7192013-10-04 19:53:11 +01002786 default: assert(0);
2787 }
2788 } else {
Damiend99b0522013-12-21 18:17:45 +00002789 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002790 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002791 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002792 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002793 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002794#if MICROPY_DEBUG_PRINTERS
2795 mp_parse_node_print(pn, 0);
2796#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002797 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002798 } else {
2799 f(comp, pns);
2800 }
2801 }
2802}
2803
Damiend99b0522013-12-21 18:17:45 +00002804void 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 +01002805 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002806 qstr param_name = MP_QSTR_NULL;
2807 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002808 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2809 if (MP_PARSE_NODE_IS_ID(pn)) {
2810 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002811 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002812 // comes after a star, so counts as a keyword-only parameter
2813 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002814 } else {
Damien George2827d622014-04-27 15:50:52 +01002815 // comes before a star, so counts as a positional parameter
2816 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002817 }
Damienb14de212013-10-06 00:28:28 +01002818 } else {
Damiend99b0522013-12-21 18:17:45 +00002819 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2820 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2821 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2822 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002823 //int node_index = 1; unused
2824 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002825 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002826 // this parameter has an annotation
2827 pn_annotation = pns->nodes[1];
2828 }
2829 //node_index = 2; unused
2830 }
2831 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002832 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002833 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002834 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002835 comp->scope_cur->num_dict_params += 1;
2836 } else {
2837 comp->scope_cur->num_default_params += 1;
2838 }
2839 }
2840 */
Damien George8b19db02014-04-11 23:25:34 +01002841 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002842 // comes after a star, so counts as a keyword-only parameter
2843 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002844 } else {
Damien George2827d622014-04-27 15:50:52 +01002845 // comes before a star, so counts as a positional parameter
2846 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002847 }
Damiend99b0522013-12-21 18:17:45 +00002848 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002849 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002850 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002851 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002852 // bare star
2853 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002854 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002855 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002856 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002857 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002858 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2859 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002860 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002861 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002862 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2863 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002864 pn_annotation = pns->nodes[1];
2865 } else {
2866 // shouldn't happen
2867 assert(0);
2868 }
Damiend99b0522013-12-21 18:17:45 +00002869 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2870 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002871 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002872 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002873 // this parameter has an annotation
2874 pn_annotation = pns->nodes[1];
2875 }
Damien George8725f8f2014-02-15 19:33:11 +00002876 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002877 } else {
Damienb14de212013-10-06 00:28:28 +01002878 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002879 assert(0);
2880 }
Damien429d7192013-10-04 19:53:11 +01002881 }
2882
Damien George2827d622014-04-27 15:50:52 +01002883 if (param_name != MP_QSTR_NULL) {
Damiend99b0522013-12-21 18:17:45 +00002884 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002885 // TODO this parameter has an annotation
2886 }
2887 bool added;
2888 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2889 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002890 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002891 return;
2892 }
Damien429d7192013-10-04 19:53:11 +01002893 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002894 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002895 }
2896}
2897
Damien George2827d622014-04-27 15:50:52 +01002898STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002899 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2900}
2901
Damien George2827d622014-04-27 15:50:52 +01002902STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002903 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2904}
2905
Damiend99b0522013-12-21 18:17:45 +00002906void 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 +01002907 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002908 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002909 // no more nested if/for; compile inner expression
2910 compile_node(comp, pn_inner_expr);
2911 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002912 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002913 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002914 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002915 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002916 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002917 } else {
2918 EMIT(yield_value);
2919 EMIT(pop_top);
2920 }
Damiend99b0522013-12-21 18:17:45 +00002921 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002922 // if condition
Damiend99b0522013-12-21 18:17:45 +00002923 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002924 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2925 pn_iter = pns_comp_if->nodes[1];
2926 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002927 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002928 // for loop
Damiend99b0522013-12-21 18:17:45 +00002929 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002930 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002931 uint l_end2 = comp_next_label(comp);
2932 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002933 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002934 EMIT_ARG(label_assign, l_top2);
2935 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002936 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2937 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 +00002938 EMIT_ARG(jump, l_top2);
2939 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002940 EMIT(for_iter_end);
2941 } else {
2942 // shouldn't happen
2943 assert(0);
2944 }
2945}
2946
Damien George1463c1f2014-04-25 23:52:57 +01002947STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
2948#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002949 // see http://www.python.org/dev/peps/pep-0257/
2950
2951 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002952 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002953 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002954 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002955 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002956 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2957 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002958 for (int i = 0; i < num_nodes; i++) {
2959 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002960 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 +00002961 // not a newline, so this is the first statement; finish search
2962 break;
2963 }
2964 }
2965 // 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 +00002966 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002967 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002968 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002969 } else {
2970 return;
2971 }
2972
2973 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002974 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2975 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2976 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2977 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2978 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002979 compile_node(comp, pns->nodes[0]); // a doc string
2980 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002981 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002982 }
2983 }
2984 }
Damien George1463c1f2014-04-25 23:52:57 +01002985#endif
Damien429d7192013-10-04 19:53:11 +01002986}
2987
Damien George1463c1f2014-04-25 23:52:57 +01002988STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002989 comp->pass = pass;
2990 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002991 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002992 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002993
2994 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002995 // reset maximum stack sizes in scope
2996 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002997 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002998 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002999 }
3000
Damien5ac1b2e2013-10-18 19:58:12 +01003001#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01003002 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01003003 scope_print_info(scope);
3004 }
Damien5ac1b2e2013-10-18 19:58:12 +01003005#endif
Damien429d7192013-10-04 19:53:11 +01003006
3007 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003008 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3009 assert(scope->kind == SCOPE_MODULE);
3010 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3011 compile_node(comp, pns->nodes[0]); // compile the expression
3012 EMIT(return_value);
3013 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003014 if (!comp->is_repl) {
3015 check_for_doc_string(comp, scope->pn);
3016 }
Damien429d7192013-10-04 19:53:11 +01003017 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003018 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003019 EMIT(return_value);
3020 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003021 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3022 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3023 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003024
3025 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003026 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003027 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003028 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003029 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3030 }
3031
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003032 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003033
3034 compile_node(comp, pns->nodes[3]); // 3 is function body
3035 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003036 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003037 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003038 EMIT(return_value);
3039 }
3040 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003041 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3042 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3043 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003044
3045 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003046 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003047 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003048 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003049 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3050 }
3051
3052 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003053
3054 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3055 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3056 EMIT(pop_top);
3057 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3058 }
Damien429d7192013-10-04 19:53:11 +01003059 EMIT(return_value);
3060 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3061 // a bit of a hack at the moment
3062
Damiend99b0522013-12-21 18:17:45 +00003063 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3064 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3065 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3066 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3067 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003068
Damien George708c0732014-04-27 19:23:46 +01003069 // We need a unique name for the comprehension argument (the iterator).
3070 // CPython uses .0, but we should be able to use anything that won't
3071 // clash with a user defined variable. Best to use an existing qstr,
3072 // so we use the blank qstr.
3073#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003074 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003075#else
3076 qstr qstr_arg = MP_QSTR_;
3077#endif
Damien429d7192013-10-04 19:53:11 +01003078 if (comp->pass == PASS_1) {
3079 bool added;
3080 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3081 assert(added);
3082 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003083 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003084 }
3085
3086 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003087 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003088 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003089 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003090 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003091 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003092 }
3093
Damien George6f355fd2014-04-10 14:11:31 +01003094 uint l_end = comp_next_label(comp);
3095 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003096 EMIT_ARG(load_id, qstr_arg);
3097 EMIT_ARG(label_assign, l_top);
3098 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003099 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3100 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003101 EMIT_ARG(jump, l_top);
3102 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003103 EMIT(for_iter_end);
3104
3105 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003106 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003107 }
3108 EMIT(return_value);
3109 } else {
3110 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003111 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3112 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3113 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003114
3115 if (comp->pass == PASS_1) {
3116 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003117 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003118 assert(added);
3119 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003120 }
3121
Damien Georgeb9791222014-01-23 00:34:21 +00003122 EMIT_ARG(load_id, MP_QSTR___name__);
3123 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003124 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 +00003125 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003126
3127 check_for_doc_string(comp, pns->nodes[2]);
3128 compile_node(comp, pns->nodes[2]); // 2 is class body
3129
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003130 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003131 assert(id != NULL);
3132 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003133 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003134 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003135#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003136 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003137#else
Damien George2bf7c092014-04-09 15:26:46 +01003138 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003139#endif
Damien429d7192013-10-04 19:53:11 +01003140 }
3141 EMIT(return_value);
3142 }
3143
Damien415eb6f2013-10-05 12:19:06 +01003144 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003145
3146 // make sure we match all the exception levels
3147 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003148}
3149
Damien George094d4502014-04-02 17:31:27 +01003150#if MICROPY_EMIT_INLINE_THUMB
Damien George1463c1f2014-04-25 23:52:57 +01003151STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003152 comp->pass = pass;
3153 comp->scope_cur = scope;
3154 comp->next_label = 1;
3155
3156 if (scope->kind != SCOPE_FUNCTION) {
3157 printf("Error: inline assembler must be a function\n");
3158 return;
3159 }
3160
Damiena2f2f7d2013-10-06 00:14:13 +01003161 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003162 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003163 }
3164
Damien826005c2013-10-05 23:17:28 +01003165 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003166 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3167 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3168 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003169
Damiend99b0522013-12-21 18:17:45 +00003170 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003171
Damiena2f2f7d2013-10-06 00:14:13 +01003172 // parameters are in pns->nodes[1]
3173 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003174 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003175 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003176 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003177 }
3178
Damiend99b0522013-12-21 18:17:45 +00003179 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003180
Damiend99b0522013-12-21 18:17:45 +00003181 mp_parse_node_t pn_body = pns->nodes[3]; // body
3182 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003183 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3184
Damien Georgecbd2f742014-01-19 11:48:48 +00003185 /*
Damien826005c2013-10-05 23:17:28 +01003186 if (comp->pass == PASS_3) {
3187 //printf("----\n");
3188 scope_print_info(scope);
3189 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003190 */
Damien826005c2013-10-05 23:17:28 +01003191
3192 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003193 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3194 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003195 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3196 // no instructions
3197 continue;
3198 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3199 // an instruction; fall through
3200 } else {
3201 // not an instruction; error
3202 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3203 return;
3204 }
Damiend99b0522013-12-21 18:17:45 +00003205 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3206 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3207 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3208 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3209 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3210 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3211 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3212 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3213 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3214 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003215 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3216
3217 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003218 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003219 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003220 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003221 return;
3222 }
Damien George6f355fd2014-04-10 14:11:31 +01003223 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003224 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003225 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003226 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003227 } else if (op == MP_QSTR_align) {
3228 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3229 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3230 return;
3231 }
3232 if (pass > PASS_1) {
3233 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3234 }
3235 } else if (op == MP_QSTR_data) {
3236 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3237 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3238 return;
3239 }
3240 if (pass > PASS_1) {
3241 machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
3242 for (uint i = 1; i < n_args; i++) {
3243 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3244 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3245 return;
3246 }
3247 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3248 }
3249 }
Damien826005c2013-10-05 23:17:28 +01003250 } else {
3251 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003252 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003253 }
3254 }
3255 }
3256
3257 if (comp->pass > PASS_1) {
Damien Georgea26dc502014-04-12 17:54:52 +01003258 bool success = EMIT_INLINE_ASM(end_pass);
3259 if (!success) {
3260 comp->had_error = true;
3261 }
Damienb05d7072013-10-05 13:37:10 +01003262 }
Damien429d7192013-10-04 19:53:11 +01003263}
Damien George094d4502014-04-02 17:31:27 +01003264#endif
Damien429d7192013-10-04 19:53:11 +01003265
Damien George1463c1f2014-04-25 23:52:57 +01003266STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003267#if !MICROPY_EMIT_CPYTHON
3268 // in Micro Python we put the *x parameter after all other parameters (except **y)
3269 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3270 id_info_t *id_param = NULL;
3271 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3272 id_info_t *id = &scope->id_info[i];
3273 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3274 if (id_param != NULL) {
3275 // swap star param with last param
3276 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3277 }
3278 break;
3279 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3280 id_param = id;
3281 }
3282 }
3283 }
3284#endif
3285
Damien429d7192013-10-04 19:53:11 +01003286 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003287 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003288 scope->num_locals = 0;
3289 for (int i = 0; i < scope->id_info_len; i++) {
3290 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003291 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003292 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3293 continue;
3294 }
3295 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3296 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3297 }
Damien George2827d622014-04-27 15:50:52 +01003298 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003299 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003300 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003301 }
3302 }
3303
3304 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003305#if MICROPY_EMIT_CPYTHON
3306 int num_cell = 0;
3307#endif
Damien9ecbcff2013-12-11 00:41:43 +00003308 for (int i = 0; i < scope->id_info_len; i++) {
3309 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003310#if MICROPY_EMIT_CPYTHON
3311 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003312 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003313 id->local_num = num_cell;
3314 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003315 }
Damien George6baf76e2013-12-30 22:32:17 +00003316#else
3317 // in Micro Python the cells come right after the fast locals
3318 // parameters are not counted here, since they remain at the start
3319 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003320 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003321 id->local_num = scope->num_locals;
3322 scope->num_locals += 1;
3323 }
3324#endif
Damien9ecbcff2013-12-11 00:41:43 +00003325 }
Damien9ecbcff2013-12-11 00:41:43 +00003326
3327 // compute the index of free vars (freevars[idx] in CPython)
3328 // make sure they are in the order of the parent scope
3329 if (scope->parent != NULL) {
3330 int num_free = 0;
3331 for (int i = 0; i < scope->parent->id_info_len; i++) {
3332 id_info_t *id = &scope->parent->id_info[i];
3333 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3334 for (int j = 0; j < scope->id_info_len; j++) {
3335 id_info_t *id2 = &scope->id_info[j];
3336 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003337 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003338#if MICROPY_EMIT_CPYTHON
3339 // in CPython the frees are numbered after the cells
3340 id2->local_num = num_cell + num_free;
3341#else
3342 // in Micro Python the frees come first, before the params
3343 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003344#endif
3345 num_free += 1;
3346 }
3347 }
3348 }
Damien429d7192013-10-04 19:53:11 +01003349 }
Damien George6baf76e2013-12-30 22:32:17 +00003350#if !MICROPY_EMIT_CPYTHON
3351 // in Micro Python shift all other locals after the free locals
3352 if (num_free > 0) {
3353 for (int i = 0; i < scope->id_info_len; i++) {
3354 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003355 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003356 id->local_num += num_free;
3357 }
3358 }
Damien George2827d622014-04-27 15:50:52 +01003359 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 +00003360 scope->num_locals += num_free;
3361 }
3362#endif
Damien429d7192013-10-04 19:53:11 +01003363 }
3364
Damien George8725f8f2014-02-15 19:33:11 +00003365 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003366
3367#if MICROPY_EMIT_CPYTHON
3368 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003369 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) {
3370 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003371 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003372 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003373 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 +00003374 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003375 }
3376 }
Damien George882b3632014-04-02 15:56:31 +01003377#endif
3378
Damien429d7192013-10-04 19:53:11 +01003379 int num_free = 0;
3380 for (int i = 0; i < scope->id_info_len; i++) {
3381 id_info_t *id = &scope->id_info[i];
3382 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3383 num_free += 1;
3384 }
3385 }
3386 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003387 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003388 }
3389}
3390
Damien George65cad122014-04-06 11:48:15 +01003391mp_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 +01003392 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003393 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003394 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003395
Damien826005c2013-10-05 23:17:28 +01003396 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003397 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003398
3399 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003400 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003401
Damien826005c2013-10-05 23:17:28 +01003402 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003403 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003404 comp->emit_method_table = &emit_pass1_method_table;
3405 comp->emit_inline_asm = NULL;
3406 comp->emit_inline_asm_method_table = NULL;
3407 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003408 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003409 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003410#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003411 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003412 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003413#endif
Damien826005c2013-10-05 23:17:28 +01003414 } else {
3415 compile_scope(comp, s, PASS_1);
3416 }
3417
3418 // update maximim number of labels needed
3419 if (comp->next_label > max_num_labels) {
3420 max_num_labels = comp->next_label;
3421 }
Damien429d7192013-10-04 19:53:11 +01003422 }
3423
Damien826005c2013-10-05 23:17:28 +01003424 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003425 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003426 compile_scope_compute_things(comp, s);
3427 }
3428
Damien826005c2013-10-05 23:17:28 +01003429 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003430 emit_pass1_free(comp->emit);
3431
Damien826005c2013-10-05 23:17:28 +01003432 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003433#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003434 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003435#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003436 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003437#endif
Damien3ef4abb2013-10-12 16:53:13 +01003438#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003439 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003440#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003441#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003442 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003443 if (false) {
3444 // dummy
3445
Damien3ef4abb2013-10-12 16:53:13 +01003446#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003447 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003448 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003449 if (emit_inline_thumb == NULL) {
3450 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3451 }
3452 comp->emit = NULL;
3453 comp->emit_method_table = NULL;
3454 comp->emit_inline_asm = emit_inline_thumb;
3455 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3456 compile_scope_inline_asm(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003457 if (!comp->had_error) {
3458 compile_scope_inline_asm(comp, s, PASS_3);
3459 }
Damienc025ebb2013-10-12 14:30:21 +01003460#endif
3461
Damien826005c2013-10-05 23:17:28 +01003462 } else {
Damienc025ebb2013-10-12 14:30:21 +01003463
3464 // choose the emit type
3465
Damien3ef4abb2013-10-12 16:53:13 +01003466#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003467 comp->emit = emit_cpython_new(max_num_labels);
3468 comp->emit_method_table = &emit_cpython_method_table;
3469#else
Damien826005c2013-10-05 23:17:28 +01003470 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003471
3472#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003473 case MP_EMIT_OPT_NATIVE_PYTHON:
3474 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003475#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003476 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003477 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003478 }
Damien13ed3a62013-10-08 09:05:10 +01003479 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003480#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003481 if (emit_native == NULL) {
3482 emit_native = emit_native_thumb_new(max_num_labels);
3483 }
3484 comp->emit_method_table = &emit_native_thumb_method_table;
3485#endif
3486 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003487 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003488 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003489#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003490
Damien826005c2013-10-05 23:17:28 +01003491 default:
3492 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003493 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003494 }
3495 comp->emit = emit_bc;
3496 comp->emit_method_table = &emit_bc_method_table;
3497 break;
3498 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003499#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003500
3501 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003502 compile_scope(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003503 if (!comp->had_error) {
3504 compile_scope(comp, s, PASS_3);
3505 }
Damien6cdd3af2013-10-05 18:08:26 +01003506 }
Damien429d7192013-10-04 19:53:11 +01003507 }
3508
Damien George41d02b62014-01-24 22:42:28 +00003509 // free the emitters
3510#if !MICROPY_EMIT_CPYTHON
3511 if (emit_bc != NULL) {
3512 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003513 }
Damien George41d02b62014-01-24 22:42:28 +00003514#if MICROPY_EMIT_NATIVE
3515 if (emit_native != NULL) {
3516#if MICROPY_EMIT_X64
3517 emit_native_x64_free(emit_native);
3518#elif MICROPY_EMIT_THUMB
3519 emit_native_thumb_free(emit_native);
3520#endif
3521 }
3522#endif
3523#if MICROPY_EMIT_INLINE_THUMB
3524 if (emit_inline_thumb != NULL) {
3525 emit_inline_thumb_free(emit_inline_thumb);
3526 }
3527#endif
3528#endif // !MICROPY_EMIT_CPYTHON
3529
3530 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003531 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003532 for (scope_t *s = module_scope; s;) {
3533 scope_t *next = s->next;
3534 scope_free(s);
3535 s = next;
3536 }
Damien5ac1b2e2013-10-18 19:58:12 +01003537
Damien George41d02b62014-01-24 22:42:28 +00003538 // free the compiler
3539 bool had_error = comp->had_error;
3540 m_del_obj(compiler_t, comp);
3541
Damien George1fb03172014-01-03 14:22:03 +00003542 if (had_error) {
3543 // TODO return a proper error message
3544 return mp_const_none;
3545 } else {
3546#if MICROPY_EMIT_CPYTHON
3547 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003548 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003549 return mp_const_true;
3550#else
3551 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003552 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003553#endif
3554 }
Damien429d7192013-10-04 19:53:11 +01003555}