blob: bc9d7aeb4cde6485b8e6252b06907044423c5553 [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) {
Damien George36db6bc2014-05-07 17:24:22 +0100997 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100998 // 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) {
Damien George36db6bc2014-05-07 17:24:22 +01001046 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001047 // 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) {
Damien George36db6bc2014-05-07 17:24:22 +01001513 if (comp->pass == MP_PASS_SCOPE) {
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) {
Damien George36db6bc2014-05-07 17:24:22 +01001527 if (comp->pass == MP_PASS_SCOPE) {
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) {
Damien Georgeca25c152014-05-07 15:42:03 +01002048 if (0) {
2049 // dummy
2050#if 0
2051 // code to compile constants: id = const(...)
2052 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
2053 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
2054 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
2055 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
2056 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
2057 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
2058 ) {
Damien George36db6bc2014-05-07 17:24:22 +01002059 if (comp->pass == MP_PASS_SCOPE) {
Damien Georgeca25c152014-05-07 15:42:03 +01002060 qstr const_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2061
2062 if (!MP_PARSE_NODE_IS_SMALL_INT(((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0])) {
2063 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
2064 }
2065 machine_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0]);
2066
2067 printf("assign const: %s = %ld\n", qstr_str(const_id), value);
2068 mp_map_elem_t *elem = mp_map_lookup(&comp->module_consts, MP_OBJ_NEW_QSTR(const_id), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
2069 if (elem->value != MP_OBJ_NULL) {
2070 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
2071 }
2072 elem->value = MP_OBJ_NEW_SMALL_INT(value);
2073 }
2074 goto no_optimisation;
2075
2076#endif
2077 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002078 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2079 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2080 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002081 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002082 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2083 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002084 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2085 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2086 // can't optimise when it's a star expression on the lhs
2087 goto no_optimisation;
2088 }
Damien429d7192013-10-04 19:53:11 +01002089 compile_node(comp, pns10->nodes[0]); // rhs
2090 compile_node(comp, pns10->nodes[1]); // rhs
2091 EMIT(rot_two);
2092 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2093 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002094 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2095 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2096 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2097 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002098 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002099 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2100 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002101 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2102 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2103 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2104 // can't optimise when it's a star expression on the lhs
2105 goto no_optimisation;
2106 }
Damien429d7192013-10-04 19:53:11 +01002107 compile_node(comp, pns10->nodes[0]); // rhs
2108 compile_node(comp, pns10->nodes[1]); // rhs
2109 compile_node(comp, pns10->nodes[2]); // rhs
2110 EMIT(rot_three);
2111 EMIT(rot_two);
2112 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2113 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2114 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2115 } else {
Damien George495d7812014-04-08 17:51:47 +01002116 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002117 compile_node(comp, pns1->nodes[0]); // rhs
2118 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2119 }
2120 } else {
2121 // shouldn't happen
2122 assert(0);
2123 }
2124 }
2125}
2126
Damien Georged17926d2014-03-30 13:35:08 +01002127void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002128 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002129 compile_node(comp, pns->nodes[0]);
2130 for (int i = 1; i < num_nodes; i += 1) {
2131 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002132 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002133 }
2134}
2135
Damiend99b0522013-12-21 18:17:45 +00002136void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2137 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2138 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002139
Damien George6f355fd2014-04-10 14:11:31 +01002140 uint l_fail = comp_next_label(comp);
2141 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002142 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2143 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002144 EMIT_ARG(jump, l_end);
2145 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002146 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002147 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002148 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002149}
2150
Damiend99b0522013-12-21 18:17:45 +00002151void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002152 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002153 //mp_parse_node_t pn_params = pns->nodes[0];
2154 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002155
Damien George36db6bc2014-05-07 17:24:22 +01002156 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002157 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002158 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 +01002159 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002160 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002161 }
2162
2163 // get the scope for this lambda
2164 scope_t *this_scope = (scope_t*)pns->nodes[2];
2165
2166 // make the lambda
2167 close_over_variables_etc(comp, this_scope, 0, 0);
2168}
2169
Damiend99b0522013-12-21 18:17:45 +00002170void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002171 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002172 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002173 for (int i = 0; i < n; i += 1) {
2174 compile_node(comp, pns->nodes[i]);
2175 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002176 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002177 }
2178 }
Damien Georgeb9791222014-01-23 00:34:21 +00002179 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002180}
2181
Damiend99b0522013-12-21 18:17:45 +00002182void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002183 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002184 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002185 for (int i = 0; i < n; i += 1) {
2186 compile_node(comp, pns->nodes[i]);
2187 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002188 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002189 }
2190 }
Damien Georgeb9791222014-01-23 00:34:21 +00002191 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002192}
2193
Damiend99b0522013-12-21 18:17:45 +00002194void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002195 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002196 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002197}
2198
Damiend99b0522013-12-21 18:17:45 +00002199void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002200 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002201 compile_node(comp, pns->nodes[0]);
2202 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002203 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002204 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002205 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002206 }
2207 for (int i = 1; i + 1 < num_nodes; i += 2) {
2208 compile_node(comp, pns->nodes[i + 1]);
2209 if (i + 2 < num_nodes) {
2210 EMIT(dup_top);
2211 EMIT(rot_three);
2212 }
Damien George7e5fb242014-02-01 22:18:47 +00002213 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002214 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002215 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002216 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2217 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2218 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2219 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2220 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2221 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2222 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2223 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002224 }
2225 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002226 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2227 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2228 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002229 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002230 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002231 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002232 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002233 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002234 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002235 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002236 }
2237 } else {
2238 // shouldn't happen
2239 assert(0);
2240 }
2241 } else {
2242 // shouldn't happen
2243 assert(0);
2244 }
2245 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002246 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002247 }
2248 }
2249 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002250 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002251 EMIT_ARG(jump, l_end);
2252 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002253 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002254 EMIT(rot_two);
2255 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002256 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002257 }
2258}
2259
Damiend99b0522013-12-21 18:17:45 +00002260void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002261 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002262}
2263
Damiend99b0522013-12-21 18:17:45 +00002264void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002265 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002266}
2267
Damiend99b0522013-12-21 18:17:45 +00002268void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002269 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002270}
2271
Damiend99b0522013-12-21 18:17:45 +00002272void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002273 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002274}
2275
Damiend99b0522013-12-21 18:17:45 +00002276void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2277 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002278 compile_node(comp, pns->nodes[0]);
2279 for (int i = 1; i + 1 < num_nodes; i += 2) {
2280 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002281 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002282 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002283 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002284 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002285 } else {
2286 // shouldn't happen
2287 assert(0);
2288 }
2289 }
2290}
2291
Damiend99b0522013-12-21 18:17:45 +00002292void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2293 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002294 compile_node(comp, pns->nodes[0]);
2295 for (int i = 1; i + 1 < num_nodes; i += 2) {
2296 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002297 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002298 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002299 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002300 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002301 } else {
2302 // shouldn't happen
2303 assert(0);
2304 }
2305 }
2306}
2307
Damiend99b0522013-12-21 18:17:45 +00002308void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2309 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002310 compile_node(comp, pns->nodes[0]);
2311 for (int i = 1; i + 1 < num_nodes; i += 2) {
2312 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002313 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002314 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002315 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002316 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002317 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002318 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002319 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002320 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002321 } else {
2322 // shouldn't happen
2323 assert(0);
2324 }
2325 }
2326}
2327
Damiend99b0522013-12-21 18:17:45 +00002328void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002329 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002330 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002331 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002332 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002333 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002334 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002335 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002336 } else {
2337 // shouldn't happen
2338 assert(0);
2339 }
2340}
2341
Damien George35e2a4e2014-02-05 00:51:47 +00002342void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2343 // this is to handle special super() call
2344 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2345
2346 compile_generic_all_nodes(comp, pns);
2347}
2348
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002349STATIC 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 +01002350 // function to call is on top of stack
2351
Damien George35e2a4e2014-02-05 00:51:47 +00002352#if !MICROPY_EMIT_CPYTHON
2353 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002354 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 +00002355 EMIT_ARG(load_id, MP_QSTR___class__);
2356 // get first argument to function
2357 bool found = false;
2358 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002359 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2360 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 +00002361 found = true;
2362 break;
2363 }
2364 }
2365 if (!found) {
2366 printf("TypeError: super() call cannot find self\n");
2367 return;
2368 }
Damien George922ddd62014-04-09 12:43:17 +01002369 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002370 return;
2371 }
2372#endif
2373
Damien George2c0842b2014-04-27 16:46:51 +01002374 // get the list of arguments
2375 mp_parse_node_t *args;
2376 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002377
Damien George2c0842b2014-04-27 16:46:51 +01002378 // compile the arguments
2379 // Rather than calling compile_node on the list, we go through the list of args
2380 // explicitly here so that we can count the number of arguments and give sensible
2381 // error messages.
2382 int n_positional = n_positional_extra;
2383 uint n_keyword = 0;
2384 uint star_flags = 0;
2385 for (int i = 0; i < n_args; i++) {
2386 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2387 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2388 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2389 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2390 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2391 return;
2392 }
2393 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2394 compile_node(comp, pns_arg->nodes[0]);
2395 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2396 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2397 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2398 return;
2399 }
2400 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2401 compile_node(comp, pns_arg->nodes[0]);
2402 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2403 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2404 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2405 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2406 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2407 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2408 return;
2409 }
Damien George968bf342014-04-27 19:12:05 +01002410 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002411 compile_node(comp, pns2->nodes[0]);
2412 n_keyword += 1;
2413 } else {
2414 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2415 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2416 n_positional++;
2417 }
2418 } else {
2419 goto normal_argument;
2420 }
2421 } else {
2422 normal_argument:
2423 if (n_keyword > 0) {
2424 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2425 return;
2426 }
2427 compile_node(comp, args[i]);
2428 n_positional++;
2429 }
Damien429d7192013-10-04 19:53:11 +01002430 }
2431
Damien George2c0842b2014-04-27 16:46:51 +01002432 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002433 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002434 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002435 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002436 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002437 }
Damien429d7192013-10-04 19:53:11 +01002438}
2439
Damiend99b0522013-12-21 18:17:45 +00002440void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2441 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002442 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002443 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 +01002444 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002445 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2446 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002447 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002448 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002449 i += 1;
2450 } else {
2451 compile_node(comp, pns->nodes[i]);
2452 }
Damien George35e2a4e2014-02-05 00:51:47 +00002453 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002454 }
2455}
2456
Damiend99b0522013-12-21 18:17:45 +00002457void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002458 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002459 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002460}
2461
Damiend99b0522013-12-21 18:17:45 +00002462void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002463 // a list of strings
Damien63321742013-12-10 17:41:49 +00002464
2465 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002466 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002467 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002468 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002469 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002470 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2471 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2472 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002473 if (i == 0) {
2474 string_kind = pn_kind;
2475 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002476 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002477 return;
2478 }
Damien George55baff42014-01-21 21:40:13 +00002479 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002480 }
Damien63321742013-12-10 17:41:49 +00002481
Damien63321742013-12-10 17:41:49 +00002482 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002483 byte *q_ptr;
2484 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002485 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002486 uint s_len;
2487 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002488 memcpy(s_dest, s, s_len);
2489 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002490 }
Damien George55baff42014-01-21 21:40:13 +00002491 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002492
Damien Georgeb9791222014-01-23 00:34:21 +00002493 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002494}
2495
2496// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002497void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2498 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2499 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2500 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002501
Damien George36db6bc2014-05-07 17:24:22 +01002502 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002503 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002504 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 +01002505 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002506 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002507 }
2508
2509 // get the scope for this comprehension
2510 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2511
2512 // compile the comprehension
2513 close_over_variables_etc(comp, this_scope, 0, 0);
2514
2515 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2516 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002517 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002518}
2519
Damiend99b0522013-12-21 18:17:45 +00002520void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2521 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002522 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002523 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2524 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2525 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2526 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2527 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2528 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2529 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002530 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002531 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002532 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002533 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002534 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002535 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002536 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002537 // generator expression
2538 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2539 } else {
2540 // tuple with 2 items
2541 goto tuple_with_2_items;
2542 }
2543 } else {
2544 // tuple with 2 items
2545 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002546 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002547 }
2548 } else {
2549 // parenthesis around a single item, is just that item
2550 compile_node(comp, pns->nodes[0]);
2551 }
2552}
2553
Damiend99b0522013-12-21 18:17:45 +00002554void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2555 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002556 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002557 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002558 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2559 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2560 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2561 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2562 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002563 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002564 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002565 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002566 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002567 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002568 // list of many items
2569 compile_node(comp, pns2->nodes[0]);
2570 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002571 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002572 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002573 // list comprehension
2574 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2575 } else {
2576 // list with 2 items
2577 goto list_with_2_items;
2578 }
2579 } else {
2580 // list with 2 items
2581 list_with_2_items:
2582 compile_node(comp, pns2->nodes[0]);
2583 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002584 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002585 }
2586 } else {
2587 // list with 1 item
2588 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002589 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002590 }
2591}
2592
Damiend99b0522013-12-21 18:17:45 +00002593void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2594 mp_parse_node_t pn = pns->nodes[0];
2595 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002596 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002597 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002598 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2599 pns = (mp_parse_node_struct_t*)pn;
2600 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002601 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002602 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002603 compile_node(comp, pn);
2604 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002605 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2606 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2607 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2608 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002609 // dict/set with multiple elements
2610
2611 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002612 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002613 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2614
2615 // first element sets whether it's a dict or set
2616 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002617 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002618 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002619 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002620 compile_node(comp, pns->nodes[0]);
2621 EMIT(store_map);
2622 is_dict = true;
2623 } else {
2624 // a set
2625 compile_node(comp, pns->nodes[0]); // 1st value of set
2626 is_dict = false;
2627 }
2628
2629 // process rest of elements
2630 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002631 mp_parse_node_t pn = nodes[i];
2632 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002633 compile_node(comp, pn);
2634 if (is_dict) {
2635 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002636 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002637 return;
2638 }
2639 EMIT(store_map);
2640 } else {
2641 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002642 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002643 return;
2644 }
2645 }
2646 }
2647
2648 // if it's a set, build it
2649 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002650 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002651 }
Damiend99b0522013-12-21 18:17:45 +00002652 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002653 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002654 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002655 // a dictionary comprehension
2656 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2657 } else {
2658 // a set comprehension
2659 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2660 }
2661 } else {
2662 // shouldn't happen
2663 assert(0);
2664 }
2665 } else {
2666 // set with one element
2667 goto set_with_one_element;
2668 }
2669 } else {
2670 // set with one element
2671 set_with_one_element:
2672 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002673 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002674 }
2675}
2676
Damiend99b0522013-12-21 18:17:45 +00002677void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002678 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002679}
2680
Damiend99b0522013-12-21 18:17:45 +00002681void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002682 // object who's index we want is on top of stack
2683 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002684 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002685}
2686
Damiend99b0522013-12-21 18:17:45 +00002687void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002688 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002689 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002690}
2691
Damiend99b0522013-12-21 18:17:45 +00002692void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2693 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2694 mp_parse_node_t pn = pns->nodes[0];
2695 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002696 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002697 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2698 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002699 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2700 pns = (mp_parse_node_struct_t*)pn;
2701 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002702 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002703 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002704 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002705 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002706 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002707 } else {
2708 // [?::x]
2709 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002710 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002711 }
Damiend99b0522013-12-21 18:17:45 +00002712 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002713 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002714 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2715 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2716 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2717 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002718 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002719 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002720 } else {
2721 // [?:x:x]
2722 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002723 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002724 }
2725 } else {
2726 // [?:x]
2727 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002728 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002729 }
2730 } else {
2731 // [?:x]
2732 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002733 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002734 }
2735}
2736
Damiend99b0522013-12-21 18:17:45 +00002737void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002738 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002739 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2740 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002741}
2742
Damiend99b0522013-12-21 18:17:45 +00002743void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002744 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002745 compile_subscript_3_helper(comp, pns);
2746}
2747
Damiend99b0522013-12-21 18:17:45 +00002748void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002749 // if this is called then we are compiling a dict key:value pair
2750 compile_node(comp, pns->nodes[1]); // value
2751 compile_node(comp, pns->nodes[0]); // key
2752}
2753
Damiend99b0522013-12-21 18:17:45 +00002754void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002755 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002756 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002757 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002758}
2759
Damiend99b0522013-12-21 18:17:45 +00002760void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002761 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002762 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002763 return;
2764 }
Damiend99b0522013-12-21 18:17:45 +00002765 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002766 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002767 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002768 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2769 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002770 compile_node(comp, pns->nodes[0]);
2771 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002772 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002773 EMIT(yield_from);
2774 } else {
2775 compile_node(comp, pns->nodes[0]);
2776 EMIT(yield_value);
2777 }
2778}
2779
Damiend99b0522013-12-21 18:17:45 +00002780typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002781STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002782 NULL,
2783#define nc NULL
2784#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002785#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002786#include "grammar.h"
2787#undef nc
2788#undef c
2789#undef DEF_RULE
2790};
2791
Damiend99b0522013-12-21 18:17:45 +00002792void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2793 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002794 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002795 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2796 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2797 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002798 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002799 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002800 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002801 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002802 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2803 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2804 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2805 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002806 case MP_PARSE_NODE_TOKEN:
2807 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002808 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002809 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002810 // do nothing
2811 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002812 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002813 }
2814 break;
Damien429d7192013-10-04 19:53:11 +01002815 default: assert(0);
2816 }
2817 } else {
Damiend99b0522013-12-21 18:17:45 +00002818 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002819 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002820 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002821 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002822 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002823#if MICROPY_DEBUG_PRINTERS
2824 mp_parse_node_print(pn, 0);
2825#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002826 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002827 } else {
2828 f(comp, pns);
2829 }
2830 }
2831}
2832
Damiend99b0522013-12-21 18:17:45 +00002833void 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 +01002834 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002835 qstr param_name = MP_QSTR_NULL;
2836 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002837 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2838 if (MP_PARSE_NODE_IS_ID(pn)) {
2839 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002840 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002841 // comes after a star, so counts as a keyword-only parameter
2842 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002843 } else {
Damien George2827d622014-04-27 15:50:52 +01002844 // comes before a star, so counts as a positional parameter
2845 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002846 }
Damienb14de212013-10-06 00:28:28 +01002847 } else {
Damiend99b0522013-12-21 18:17:45 +00002848 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2849 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2850 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2851 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002852 //int node_index = 1; unused
2853 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002854 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002855 // this parameter has an annotation
2856 pn_annotation = pns->nodes[1];
2857 }
2858 //node_index = 2; unused
2859 }
2860 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002861 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002862 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002863 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002864 comp->scope_cur->num_dict_params += 1;
2865 } else {
2866 comp->scope_cur->num_default_params += 1;
2867 }
2868 }
2869 */
Damien George8b19db02014-04-11 23:25:34 +01002870 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002871 // comes after a star, so counts as a keyword-only parameter
2872 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002873 } else {
Damien George2827d622014-04-27 15:50:52 +01002874 // comes before a star, so counts as a positional parameter
2875 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002876 }
Damiend99b0522013-12-21 18:17:45 +00002877 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002878 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002879 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002880 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002881 // bare star
2882 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002883 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002884 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002885 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002886 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002887 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2888 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002889 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002890 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002891 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2892 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002893 pn_annotation = pns->nodes[1];
2894 } else {
2895 // shouldn't happen
2896 assert(0);
2897 }
Damiend99b0522013-12-21 18:17:45 +00002898 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2899 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002900 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002901 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002902 // this parameter has an annotation
2903 pn_annotation = pns->nodes[1];
2904 }
Damien George8725f8f2014-02-15 19:33:11 +00002905 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002906 } else {
Damienb14de212013-10-06 00:28:28 +01002907 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002908 assert(0);
2909 }
Damien429d7192013-10-04 19:53:11 +01002910 }
2911
Damien George2827d622014-04-27 15:50:52 +01002912 if (param_name != MP_QSTR_NULL) {
Damiend99b0522013-12-21 18:17:45 +00002913 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002914 // TODO this parameter has an annotation
2915 }
2916 bool added;
2917 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2918 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002919 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002920 return;
2921 }
Damien429d7192013-10-04 19:53:11 +01002922 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002923 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002924 }
2925}
2926
Damien George2827d622014-04-27 15:50:52 +01002927STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002928 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2929}
2930
Damien George2827d622014-04-27 15:50:52 +01002931STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002932 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2933}
2934
Damiend99b0522013-12-21 18:17:45 +00002935void 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 +01002936 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002937 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002938 // no more nested if/for; compile inner expression
2939 compile_node(comp, pn_inner_expr);
2940 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002941 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002942 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002943 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002944 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002945 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002946 } else {
2947 EMIT(yield_value);
2948 EMIT(pop_top);
2949 }
Damiend99b0522013-12-21 18:17:45 +00002950 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002951 // if condition
Damiend99b0522013-12-21 18:17:45 +00002952 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002953 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2954 pn_iter = pns_comp_if->nodes[1];
2955 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002956 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002957 // for loop
Damiend99b0522013-12-21 18:17:45 +00002958 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002959 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002960 uint l_end2 = comp_next_label(comp);
2961 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002962 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002963 EMIT_ARG(label_assign, l_top2);
2964 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002965 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2966 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 +00002967 EMIT_ARG(jump, l_top2);
2968 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002969 EMIT(for_iter_end);
2970 } else {
2971 // shouldn't happen
2972 assert(0);
2973 }
2974}
2975
Damien George1463c1f2014-04-25 23:52:57 +01002976STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
2977#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002978 // see http://www.python.org/dev/peps/pep-0257/
2979
2980 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002981 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002982 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002983 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002984 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002985 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2986 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002987 for (int i = 0; i < num_nodes; i++) {
2988 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002989 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 +00002990 // not a newline, so this is the first statement; finish search
2991 break;
2992 }
2993 }
2994 // 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 +00002995 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002996 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002997 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002998 } else {
2999 return;
3000 }
3001
3002 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003003 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3004 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
3005 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
3006 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
3007 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01003008 compile_node(comp, pns->nodes[0]); // a doc string
3009 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003010 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003011 }
3012 }
3013 }
Damien George1463c1f2014-04-25 23:52:57 +01003014#endif
Damien429d7192013-10-04 19:53:11 +01003015}
3016
Damien George1463c1f2014-04-25 23:52:57 +01003017STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003018 comp->pass = pass;
3019 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003020 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003021 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003022
Damien George36db6bc2014-05-07 17:24:22 +01003023 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003024 // reset maximum stack sizes in scope
3025 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003026 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003027 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003028 }
3029
Damien5ac1b2e2013-10-18 19:58:12 +01003030#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003031 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003032 scope_print_info(scope);
3033 }
Damien5ac1b2e2013-10-18 19:58:12 +01003034#endif
Damien429d7192013-10-04 19:53:11 +01003035
3036 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003037 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3038 assert(scope->kind == SCOPE_MODULE);
3039 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3040 compile_node(comp, pns->nodes[0]); // compile the expression
3041 EMIT(return_value);
3042 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003043 if (!comp->is_repl) {
3044 check_for_doc_string(comp, scope->pn);
3045 }
Damien429d7192013-10-04 19:53:11 +01003046 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003047 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003048 EMIT(return_value);
3049 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003050 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3051 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3052 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003053
3054 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003055 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003056 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003057 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003058 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3059 }
3060
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003061 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003062
3063 compile_node(comp, pns->nodes[3]); // 3 is function body
3064 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003065 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003066 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003067 EMIT(return_value);
3068 }
3069 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003070 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3071 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3072 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003073
3074 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003075 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003076 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003077 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003078 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3079 }
3080
3081 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003082
3083 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3084 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3085 EMIT(pop_top);
3086 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3087 }
Damien429d7192013-10-04 19:53:11 +01003088 EMIT(return_value);
3089 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3090 // a bit of a hack at the moment
3091
Damiend99b0522013-12-21 18:17:45 +00003092 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3093 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3094 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3095 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3096 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003097
Damien George708c0732014-04-27 19:23:46 +01003098 // We need a unique name for the comprehension argument (the iterator).
3099 // CPython uses .0, but we should be able to use anything that won't
3100 // clash with a user defined variable. Best to use an existing qstr,
3101 // so we use the blank qstr.
3102#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003103 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003104#else
3105 qstr qstr_arg = MP_QSTR_;
3106#endif
Damien George36db6bc2014-05-07 17:24:22 +01003107 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003108 bool added;
3109 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3110 assert(added);
3111 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003112 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003113 }
3114
3115 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003116 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003117 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003118 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003119 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003120 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003121 }
3122
Damien George6f355fd2014-04-10 14:11:31 +01003123 uint l_end = comp_next_label(comp);
3124 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003125 EMIT_ARG(load_id, qstr_arg);
3126 EMIT_ARG(label_assign, l_top);
3127 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003128 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3129 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003130 EMIT_ARG(jump, l_top);
3131 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003132 EMIT(for_iter_end);
3133
3134 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003135 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003136 }
3137 EMIT(return_value);
3138 } else {
3139 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003140 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3141 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3142 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003143
Damien George36db6bc2014-05-07 17:24:22 +01003144 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003145 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003146 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003147 assert(added);
3148 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003149 }
3150
Damien Georgeb9791222014-01-23 00:34:21 +00003151 EMIT_ARG(load_id, MP_QSTR___name__);
3152 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003153 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 +00003154 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003155
3156 check_for_doc_string(comp, pns->nodes[2]);
3157 compile_node(comp, pns->nodes[2]); // 2 is class body
3158
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003159 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003160 assert(id != NULL);
3161 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003162 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003163 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003164#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003165 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003166#else
Damien George2bf7c092014-04-09 15:26:46 +01003167 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003168#endif
Damien429d7192013-10-04 19:53:11 +01003169 }
3170 EMIT(return_value);
3171 }
3172
Damien415eb6f2013-10-05 12:19:06 +01003173 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003174
3175 // make sure we match all the exception levels
3176 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003177}
3178
Damien George094d4502014-04-02 17:31:27 +01003179#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003180// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003181STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003182 comp->pass = pass;
3183 comp->scope_cur = scope;
3184 comp->next_label = 1;
3185
3186 if (scope->kind != SCOPE_FUNCTION) {
3187 printf("Error: inline assembler must be a function\n");
3188 return;
3189 }
3190
Damien George36db6bc2014-05-07 17:24:22 +01003191 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003192 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003193 }
3194
Damien826005c2013-10-05 23:17:28 +01003195 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003196 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3197 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3198 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003199
Damiend99b0522013-12-21 18:17:45 +00003200 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003201
Damiena2f2f7d2013-10-06 00:14:13 +01003202 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003203 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003204 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003205 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003206 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003207 }
3208
Damiend99b0522013-12-21 18:17:45 +00003209 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003210
Damiend99b0522013-12-21 18:17:45 +00003211 mp_parse_node_t pn_body = pns->nodes[3]; // body
3212 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003213 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3214
Damien Georgecbd2f742014-01-19 11:48:48 +00003215 /*
Damien George36db6bc2014-05-07 17:24:22 +01003216 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003217 //printf("----\n");
3218 scope_print_info(scope);
3219 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003220 */
Damien826005c2013-10-05 23:17:28 +01003221
3222 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003223 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3224 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003225 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3226 // no instructions
3227 continue;
3228 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3229 // an instruction; fall through
3230 } else {
3231 // not an instruction; error
3232 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3233 return;
3234 }
Damiend99b0522013-12-21 18:17:45 +00003235 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3236 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3237 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3238 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3239 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3240 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3241 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3242 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3243 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3244 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003245 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3246
3247 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003248 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003249 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003250 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003251 return;
3252 }
Damien George6f355fd2014-04-10 14:11:31 +01003253 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003254 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003255 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003256 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003257 } else if (op == MP_QSTR_align) {
3258 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3259 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3260 return;
3261 }
Damien George36db6bc2014-05-07 17:24:22 +01003262 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003263 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3264 }
3265 } else if (op == MP_QSTR_data) {
3266 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3267 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3268 return;
3269 }
Damien George36db6bc2014-05-07 17:24:22 +01003270 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003271 machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
3272 for (uint i = 1; i < n_args; i++) {
3273 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3274 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3275 return;
3276 }
3277 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3278 }
3279 }
Damien826005c2013-10-05 23:17:28 +01003280 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003281 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003282 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003283 }
3284 }
3285 }
3286
Damien George36db6bc2014-05-07 17:24:22 +01003287 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003288 bool success = EMIT_INLINE_ASM(end_pass);
3289 if (!success) {
3290 comp->had_error = true;
3291 }
Damienb05d7072013-10-05 13:37:10 +01003292 }
Damien429d7192013-10-04 19:53:11 +01003293}
Damien George094d4502014-04-02 17:31:27 +01003294#endif
Damien429d7192013-10-04 19:53:11 +01003295
Damien George1463c1f2014-04-25 23:52:57 +01003296STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003297#if !MICROPY_EMIT_CPYTHON
3298 // in Micro Python we put the *x parameter after all other parameters (except **y)
3299 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3300 id_info_t *id_param = NULL;
3301 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3302 id_info_t *id = &scope->id_info[i];
3303 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3304 if (id_param != NULL) {
3305 // swap star param with last param
3306 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3307 }
3308 break;
3309 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3310 id_param = id;
3311 }
3312 }
3313 }
3314#endif
3315
Damien429d7192013-10-04 19:53:11 +01003316 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003317 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003318 scope->num_locals = 0;
3319 for (int i = 0; i < scope->id_info_len; i++) {
3320 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003321 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003322 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3323 continue;
3324 }
3325 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3326 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3327 }
Damien George2827d622014-04-27 15:50:52 +01003328 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003329 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003330 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003331 }
3332 }
3333
3334 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003335#if MICROPY_EMIT_CPYTHON
3336 int num_cell = 0;
3337#endif
Damien9ecbcff2013-12-11 00:41:43 +00003338 for (int i = 0; i < scope->id_info_len; i++) {
3339 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003340#if MICROPY_EMIT_CPYTHON
3341 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003342 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003343 id->local_num = num_cell;
3344 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003345 }
Damien George6baf76e2013-12-30 22:32:17 +00003346#else
3347 // in Micro Python the cells come right after the fast locals
3348 // parameters are not counted here, since they remain at the start
3349 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003350 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003351 id->local_num = scope->num_locals;
3352 scope->num_locals += 1;
3353 }
3354#endif
Damien9ecbcff2013-12-11 00:41:43 +00003355 }
Damien9ecbcff2013-12-11 00:41:43 +00003356
3357 // compute the index of free vars (freevars[idx] in CPython)
3358 // make sure they are in the order of the parent scope
3359 if (scope->parent != NULL) {
3360 int num_free = 0;
3361 for (int i = 0; i < scope->parent->id_info_len; i++) {
3362 id_info_t *id = &scope->parent->id_info[i];
3363 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3364 for (int j = 0; j < scope->id_info_len; j++) {
3365 id_info_t *id2 = &scope->id_info[j];
3366 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003367 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003368#if MICROPY_EMIT_CPYTHON
3369 // in CPython the frees are numbered after the cells
3370 id2->local_num = num_cell + num_free;
3371#else
3372 // in Micro Python the frees come first, before the params
3373 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003374#endif
3375 num_free += 1;
3376 }
3377 }
3378 }
Damien429d7192013-10-04 19:53:11 +01003379 }
Damien George6baf76e2013-12-30 22:32:17 +00003380#if !MICROPY_EMIT_CPYTHON
3381 // in Micro Python shift all other locals after the free locals
3382 if (num_free > 0) {
3383 for (int i = 0; i < scope->id_info_len; i++) {
3384 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003385 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003386 id->local_num += num_free;
3387 }
3388 }
Damien George2827d622014-04-27 15:50:52 +01003389 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 +00003390 scope->num_locals += num_free;
3391 }
3392#endif
Damien429d7192013-10-04 19:53:11 +01003393 }
3394
Damien George8725f8f2014-02-15 19:33:11 +00003395 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003396
3397#if MICROPY_EMIT_CPYTHON
3398 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003399 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) {
3400 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003401 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003402 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003403 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 +00003404 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003405 }
3406 }
Damien George882b3632014-04-02 15:56:31 +01003407#endif
3408
Damien429d7192013-10-04 19:53:11 +01003409 int num_free = 0;
3410 for (int i = 0; i < scope->id_info_len; i++) {
3411 id_info_t *id = &scope->id_info[i];
3412 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3413 num_free += 1;
3414 }
3415 }
3416 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003417 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003418 }
3419}
3420
Damien George65cad122014-04-06 11:48:15 +01003421mp_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 +01003422 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003423 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003424 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003425
Damien826005c2013-10-05 23:17:28 +01003426 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003427 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003428
3429 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003430 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003431
Damien826005c2013-10-05 23:17:28 +01003432 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003433 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003434 comp->emit_method_table = &emit_pass1_method_table;
3435 comp->emit_inline_asm = NULL;
3436 comp->emit_inline_asm_method_table = NULL;
3437 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003438 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003439 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003440#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003441 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003442 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003443#endif
Damien826005c2013-10-05 23:17:28 +01003444 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003445 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003446 }
3447
3448 // update maximim number of labels needed
3449 if (comp->next_label > max_num_labels) {
3450 max_num_labels = comp->next_label;
3451 }
Damien429d7192013-10-04 19:53:11 +01003452 }
3453
Damien826005c2013-10-05 23:17:28 +01003454 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003455 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003456 compile_scope_compute_things(comp, s);
3457 }
3458
Damien826005c2013-10-05 23:17:28 +01003459 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003460 emit_pass1_free(comp->emit);
3461
Damien826005c2013-10-05 23:17:28 +01003462 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003463#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003464 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003465#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003466 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003467#endif
Damien3ef4abb2013-10-12 16:53:13 +01003468#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003469 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003470#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003471#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003472 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003473 if (false) {
3474 // dummy
3475
Damien3ef4abb2013-10-12 16:53:13 +01003476#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003477 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003478 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003479 if (emit_inline_thumb == NULL) {
3480 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3481 }
3482 comp->emit = NULL;
3483 comp->emit_method_table = NULL;
3484 comp->emit_inline_asm = emit_inline_thumb;
3485 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003486 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea26dc502014-04-12 17:54:52 +01003487 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003488 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003489 }
Damienc025ebb2013-10-12 14:30:21 +01003490#endif
3491
Damien826005c2013-10-05 23:17:28 +01003492 } else {
Damienc025ebb2013-10-12 14:30:21 +01003493
3494 // choose the emit type
3495
Damien3ef4abb2013-10-12 16:53:13 +01003496#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003497 comp->emit = emit_cpython_new(max_num_labels);
3498 comp->emit_method_table = &emit_cpython_method_table;
3499#else
Damien826005c2013-10-05 23:17:28 +01003500 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003501
3502#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003503 case MP_EMIT_OPT_NATIVE_PYTHON:
3504 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003505#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003506 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003507 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003508 }
Damien13ed3a62013-10-08 09:05:10 +01003509 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003510#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003511 if (emit_native == NULL) {
3512 emit_native = emit_native_thumb_new(max_num_labels);
3513 }
3514 comp->emit_method_table = &emit_native_thumb_method_table;
3515#endif
3516 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003517 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien George36db6bc2014-05-07 17:24:22 +01003518
3519 // native emitters need an extra pass to compute stack size
3520 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3521
Damien7af3d192013-10-07 00:02:49 +01003522 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003523#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003524
Damien826005c2013-10-05 23:17:28 +01003525 default:
3526 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003527 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003528 }
3529 comp->emit = emit_bc;
3530 comp->emit_method_table = &emit_bc_method_table;
3531 break;
3532 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003533#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003534
Damien George36db6bc2014-05-07 17:24:22 +01003535 // second last pass: compute code size
Damien Georgea26dc502014-04-12 17:54:52 +01003536 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003537 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3538 }
3539
3540 // final pass: emit code
3541 if (!comp->had_error) {
3542 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003543 }
Damien6cdd3af2013-10-05 18:08:26 +01003544 }
Damien429d7192013-10-04 19:53:11 +01003545 }
3546
Damien George41d02b62014-01-24 22:42:28 +00003547 // free the emitters
3548#if !MICROPY_EMIT_CPYTHON
3549 if (emit_bc != NULL) {
3550 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003551 }
Damien George41d02b62014-01-24 22:42:28 +00003552#if MICROPY_EMIT_NATIVE
3553 if (emit_native != NULL) {
3554#if MICROPY_EMIT_X64
3555 emit_native_x64_free(emit_native);
3556#elif MICROPY_EMIT_THUMB
3557 emit_native_thumb_free(emit_native);
3558#endif
3559 }
3560#endif
3561#if MICROPY_EMIT_INLINE_THUMB
3562 if (emit_inline_thumb != NULL) {
3563 emit_inline_thumb_free(emit_inline_thumb);
3564 }
3565#endif
3566#endif // !MICROPY_EMIT_CPYTHON
3567
3568 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003569 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003570 for (scope_t *s = module_scope; s;) {
3571 scope_t *next = s->next;
3572 scope_free(s);
3573 s = next;
3574 }
Damien5ac1b2e2013-10-18 19:58:12 +01003575
Damien George41d02b62014-01-24 22:42:28 +00003576 // free the compiler
3577 bool had_error = comp->had_error;
3578 m_del_obj(compiler_t, comp);
3579
Damien George1fb03172014-01-03 14:22:03 +00003580 if (had_error) {
3581 // TODO return a proper error message
3582 return mp_const_none;
3583 } else {
3584#if MICROPY_EMIT_CPYTHON
3585 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003586 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003587 return mp_const_true;
3588#else
3589 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003590 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003591#endif
3592 }
Damien429d7192013-10-04 19:53:11 +01003593}