blob: b9979af14854354155f886c7f193dfc583bfcf7b [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,
Damien George5042bce2014-05-25 22:06:06 +010059 PN_string, // special node for non-interned string
Damien429d7192013-10-04 19:53:11 +010060} pn_kind_t;
61
Damien Georgeb9791222014-01-23 00:34:21 +000062#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
63#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
64#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
65#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010066
67typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000068 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010069 uint8_t is_repl;
70 uint8_t pass; // holds enum type pass_kind_t
71 uint8_t had_error; // try to keep compiler clean from nlr
72 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010073
Damien George6f355fd2014-04-10 14:11:31 +010074 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010075
Damien George25c84642014-05-30 15:20:41 +010076 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
77 uint16_t continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000078 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010079 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010080
Damien George8b19db02014-04-11 23:25:34 +010081 uint8_t have_star;
Damien George69b89d22014-04-11 13:38:30 +000082 uint16_t num_dict_params;
83 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010084
85 scope_t *scope_head;
86 scope_t *scope_cur;
87
Damien6cdd3af2013-10-05 18:08:26 +010088 emit_t *emit; // current emitter
89 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010090
91 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
92 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010093} compiler_t;
94
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010095STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000096 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010097 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
98 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
99 } else {
100 printf(" File \"%s\"\n", qstr_str(comp->source_file));
101 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000102 printf("SyntaxError: %s\n", msg);
103 comp->had_error = true;
104}
105
Damien George57e99eb2014-04-10 22:42:11 +0100106STATIC const mp_map_elem_t mp_constants_table[] = {
107 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100108 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100109};
110
111STATIC const mp_map_t mp_constants_map = {
112 .all_keys_are_qstrs = 1,
113 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200114 .used = MP_ARRAY_SIZE(mp_constants_table),
115 .alloc = MP_ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +0100116 .table = (mp_map_elem_t*)mp_constants_table,
117};
118
Damien Georgeffae48d2014-05-08 15:58:39 +0000119// this function is essentially a simple preprocessor
120STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
121 if (0) {
122 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100123#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000124 } else if (MP_PARSE_NODE_IS_ID(pn)) {
125 // lookup identifier in table of dynamic constants
126 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
127 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
128 if (elem != NULL) {
129 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
130 }
131#endif
132 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000133 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100134
Damien Georgeffae48d2014-05-08 15:58:39 +0000135 // fold some parse nodes before folding their arguments
136 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100137#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000138 case PN_expr_stmt:
139 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
140 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
141 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
142 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
143 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
144 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
145 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
146 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
147 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
148 ) {
149 // code to assign dynamic constants: id = const(value)
150
151 // get the id
152 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
153
154 // get the value
155 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
156 pn_value = fold_constants(comp, pn_value, consts);
157 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
158 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
159 break;
160 }
161 machine_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
162
163 // store the value in the table of dynamic constants
164 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
165 if (elem->value != MP_OBJ_NULL) {
166 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
167 break;
168 }
169 elem->value = MP_OBJ_NEW_SMALL_INT(value);
170
171 // replace const(value) with value
172 pns1->nodes[0] = pn_value;
173
174 // finished folding this assignment
175 return pn;
176 }
177 }
178 }
179 break;
180#endif
Damien George5042bce2014-05-25 22:06:06 +0100181 case PN_string:
182 return pn;
Damien429d7192013-10-04 19:53:11 +0100183 }
184
Damien Georgeffae48d2014-05-08 15:58:39 +0000185 // fold arguments
186 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
187 for (int i = 0; i < n; i++) {
188 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
189 }
190
191 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000192 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100193 case PN_atom_paren:
194 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
195 // (int)
196 pn = pns->nodes[0];
197 }
198 break;
199
200 case PN_expr:
201 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
202 // int | int
203 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
204 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
205 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
206 }
207 break;
208
209 case PN_and_expr:
210 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
211 // int & int
212 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
213 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
214 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
215 }
216 break;
217
Damien429d7192013-10-04 19:53:11 +0100218 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000219 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100220 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
221 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000222 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100223 // int << int
224 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
225 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
226 }
Damiend99b0522013-12-21 18:17:45 +0000227 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100228 // int >> int
Damiend99b0522013-12-21 18:17:45 +0000229 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100230 } else {
231 // shouldn't happen
232 assert(0);
233 }
234 }
235 break;
236
237 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100238 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000239 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 +0200240 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
241 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000242 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100243 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000244 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000245 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100246 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000247 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100248 } else {
249 // shouldn't happen
250 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100251 }
Damien Georged1e355e2014-05-28 14:51:12 +0100252 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100253 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000254 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100255 }
256 }
257 break;
258
259 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000260 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 +0000261 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
262 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000263 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100264 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000265 if (!mp_small_int_mul_overflow(arg0, arg1)) {
266 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100267 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000268 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
269 }
270 }
Damiend99b0522013-12-21 18:17:45 +0000271 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100272 // int / int
273 // pass
Damiend99b0522013-12-21 18:17:45 +0000274 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100275 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000276 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000277 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300278 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100279 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000280 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 +0300281 }
Damien429d7192013-10-04 19:53:11 +0100282 } else {
283 // shouldn't happen
284 assert(0);
285 }
286 }
287 break;
288
289 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000290 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200291 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000292 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100293 // +int
Damiend99b0522013-12-21 18:17:45 +0000294 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
295 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100296 // -int
Damiend99b0522013-12-21 18:17:45 +0000297 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
298 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100299 // ~int
Damiend99b0522013-12-21 18:17:45 +0000300 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100301 } else {
302 // shouldn't happen
303 assert(0);
304 }
305 }
306 break;
307
308 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100309 if (0) {
310#if MICROPY_EMIT_CPYTHON
311 } 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 +0100312 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100313 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000314 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
315 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200316 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100317 if (power >= 0) {
318 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200319 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100320 for (; power > 0; power--) {
321 ans *= base;
322 }
Damiend99b0522013-12-21 18:17:45 +0000323 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100324 }
325 }
Damien George57e99eb2014-04-10 22:42:11 +0100326#endif
327 } 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])) {
328 // id.id
329 // look it up in constant table, see if it can be replaced with an integer
330 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
331 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
332 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
333 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
334 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
335 if (elem != NULL) {
336 mp_obj_t dest[2];
337 mp_load_method_maybe(elem->value, q_attr, dest);
338 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
339 machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georged1e355e2014-05-28 14:51:12 +0100340 if (MP_SMALL_INT_FITS(val)) {
Damien George57e99eb2014-04-10 22:42:11 +0100341 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
342 }
343 }
344 }
Damien429d7192013-10-04 19:53:11 +0100345 }
346 break;
347 }
348 }
349
350 return pn;
351}
352
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200353STATIC 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 +0100354void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000355STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100356
Damien George6f355fd2014-04-10 14:11:31 +0100357STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100358 return comp->next_label++;
359}
360
Damien George8dcc0c72014-03-27 10:55:21 +0000361STATIC void compile_increase_except_level(compiler_t *comp) {
362 comp->cur_except_level += 1;
363 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
364 comp->scope_cur->exc_stack_size = comp->cur_except_level;
365 }
366}
367
368STATIC void compile_decrease_except_level(compiler_t *comp) {
369 assert(comp->cur_except_level > 0);
370 comp->cur_except_level -= 1;
371}
372
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200373STATIC 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 +0100374 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100375 scope->parent = comp->scope_cur;
376 scope->next = NULL;
377 if (comp->scope_head == NULL) {
378 comp->scope_head = scope;
379 } else {
380 scope_t *s = comp->scope_head;
381 while (s->next != NULL) {
382 s = s->next;
383 }
384 s->next = scope;
385 }
386 return scope;
387}
388
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200389STATIC 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 +0000390 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
391 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
392 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100393 for (int i = 0; i < num_nodes; i++) {
394 f(comp, pns->nodes[i]);
395 }
Damiend99b0522013-12-21 18:17:45 +0000396 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100397 f(comp, pn);
398 }
399}
400
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200401STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000402 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100403 *nodes = NULL;
404 return 0;
Damiend99b0522013-12-21 18:17:45 +0000405 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100406 *nodes = pn;
407 return 1;
408 } else {
Damiend99b0522013-12-21 18:17:45 +0000409 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
410 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100411 *nodes = pn;
412 return 1;
413 } else {
414 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000415 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100416 }
417 }
418}
419
Damiend99b0522013-12-21 18:17:45 +0000420void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100421}
422
Damiend99b0522013-12-21 18:17:45 +0000423void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
424 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100425 for (int i = 0; i < num_nodes; i++) {
426 compile_node(comp, pns->nodes[i]);
427 }
428}
429
Damien3ef4abb2013-10-12 16:53:13 +0100430#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200431STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100432 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
433 return true;
434 }
Damiend99b0522013-12-21 18:17:45 +0000435 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100436 return false;
437 }
Damiend99b0522013-12-21 18:17:45 +0000438 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100439 return false;
440 }
441 return true;
442}
443
Damien George5042bce2014-05-25 22:06:06 +0100444STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000445 bool has_single_quote = false;
446 bool has_double_quote = false;
447 for (int i = 0; i < len; i++) {
448 if (str[i] == '\'') {
449 has_single_quote = true;
450 } else if (str[i] == '"') {
451 has_double_quote = true;
452 }
453 }
454 if (bytes) {
455 vstr_printf(vstr, "b");
456 }
457 bool quote_single = false;
458 if (has_single_quote && !has_double_quote) {
459 vstr_printf(vstr, "\"");
460 } else {
461 quote_single = true;
462 vstr_printf(vstr, "'");
463 }
464 for (int i = 0; i < len; i++) {
465 if (str[i] == '\n') {
466 vstr_printf(vstr, "\\n");
467 } else if (str[i] == '\\') {
468 vstr_printf(vstr, "\\\\");
469 } else if (str[i] == '\'' && quote_single) {
470 vstr_printf(vstr, "\\'");
471 } else {
472 vstr_printf(vstr, "%c", str[i]);
473 }
474 }
475 if (has_single_quote && !has_double_quote) {
476 vstr_printf(vstr, "\"");
477 } else {
478 vstr_printf(vstr, "'");
479 }
480}
481
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200482STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George5042bce2014-05-25 22:06:06 +0100483 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
484 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
485 cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (machine_uint_t)pns->nodes[1], false);
486 return;
487 }
488
Damiend99b0522013-12-21 18:17:45 +0000489 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200490 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
491 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
492 return;
493 }
494
Damiend99b0522013-12-21 18:17:45 +0000495 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
496 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
497 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000498 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
499 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100500 case MP_PARSE_NODE_STRING:
501 case MP_PARSE_NODE_BYTES: {
502 uint len;
503 const byte *str = qstr_data(arg, &len);
504 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
505 break;
506 }
Damiend99b0522013-12-21 18:17:45 +0000507 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100508 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000509 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
510 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
511 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100512 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100513 }
514 break;
515 default: assert(0);
516 }
517}
518
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200519STATIC 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 +0100520 int n = 0;
521 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000522 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100523 }
524 int total = n;
525 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000526 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100527 total += 1;
Damien3a205172013-10-12 15:01:56 +0100528 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100529 is_const = false;
530 }
531 }
532 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100533 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100534 is_const = false;
535 break;
536 }
537 }
538 if (total > 0 && is_const) {
539 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000540 vstr_t *vstr = vstr_new();
541 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000542 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000543 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100544 need_comma = true;
545 }
546 for (int i = 0; i < n; i++) {
547 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000548 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100549 }
Damien02f89412013-12-12 15:13:36 +0000550 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100551 need_comma = true;
552 }
553 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000554 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100555 } else {
Damien02f89412013-12-12 15:13:36 +0000556 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100557 }
Damien Georgeb9791222014-01-23 00:34:21 +0000558 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000559 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100560 } else {
Damiend99b0522013-12-21 18:17:45 +0000561 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100562 compile_node(comp, pn);
563 }
564 for (int i = 0; i < n; i++) {
565 compile_node(comp, pns_list->nodes[i]);
566 }
Damien Georgeb9791222014-01-23 00:34:21 +0000567 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100568 }
569}
Damien3a205172013-10-12 15:01:56 +0100570#endif
571
572// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100573STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100574#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100575 cpython_c_tuple(comp, pn, pns_list);
576#else
577 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000578 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100579 compile_node(comp, pn);
580 total += 1;
581 }
582 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000583 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100584 for (int i = 0; i < n; i++) {
585 compile_node(comp, pns_list->nodes[i]);
586 }
587 total += n;
588 }
Damien Georgeb9791222014-01-23 00:34:21 +0000589 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100590#endif
591}
Damien429d7192013-10-04 19:53:11 +0100592
Damiend99b0522013-12-21 18:17:45 +0000593void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100594 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000595 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100596}
597
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200598STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000599 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200600 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100601}
602
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200603STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200604 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 +0100605}
606
Damien3ef4abb2013-10-12 16:53:13 +0100607#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100608// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200609STATIC 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 +0100610 if (node_is_const_false(pn)) {
611 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000612 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100613 }
614 return;
615 } else if (node_is_const_true(pn)) {
616 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000617 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100618 }
619 return;
Damiend99b0522013-12-21 18:17:45 +0000620 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
621 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
622 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
623 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100624 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100625 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100626 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100627 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100628 }
Damien3a205172013-10-12 15:01:56 +0100629 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000630 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100631 } else {
632 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100633 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100634 }
635 }
636 return;
Damiend99b0522013-12-21 18:17:45 +0000637 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100638 if (jump_if == false) {
639 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100640 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100641 }
642 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100643 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100644 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100645 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100646 }
Damien3a205172013-10-12 15:01:56 +0100647 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000648 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100649 }
650 return;
Damiend99b0522013-12-21 18:17:45 +0000651 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100652 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100653 return;
654 }
655 }
656
657 // nothing special, fall back to default compiling for node and jump
658 compile_node(comp, pn);
659 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000660 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100661 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000662 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100663 }
664}
Damien3a205172013-10-12 15:01:56 +0100665#endif
Damien429d7192013-10-04 19:53:11 +0100666
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200667STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100668#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100669 cpython_c_if_cond(comp, pn, jump_if, label, false);
670#else
671 if (node_is_const_false(pn)) {
672 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000673 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100674 }
675 return;
676 } else if (node_is_const_true(pn)) {
677 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000678 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100679 }
680 return;
Damiend99b0522013-12-21 18:17:45 +0000681 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
682 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
683 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
684 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100685 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100686 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100687 for (int i = 0; i < n - 1; i++) {
688 c_if_cond(comp, pns->nodes[i], true, label2);
689 }
690 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000691 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100692 } else {
693 for (int i = 0; i < n; i++) {
694 c_if_cond(comp, pns->nodes[i], true, label);
695 }
696 }
697 return;
Damiend99b0522013-12-21 18:17:45 +0000698 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100699 if (jump_if == false) {
700 for (int i = 0; i < n; i++) {
701 c_if_cond(comp, pns->nodes[i], false, label);
702 }
703 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100704 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100705 for (int i = 0; i < n - 1; i++) {
706 c_if_cond(comp, pns->nodes[i], false, label2);
707 }
708 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000709 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100710 }
711 return;
Damiend99b0522013-12-21 18:17:45 +0000712 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100713 c_if_cond(comp, pns->nodes[0], !jump_if, label);
714 return;
715 }
716 }
717
718 // nothing special, fall back to default compiling for node and jump
719 compile_node(comp, pn);
720 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000721 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100722 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000723 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100724 }
725#endif
Damien429d7192013-10-04 19:53:11 +0100726}
727
728typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000729void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100730
Damiend99b0522013-12-21 18:17:45 +0000731void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100732 if (assign_kind != ASSIGN_AUG_STORE) {
733 compile_node(comp, pns->nodes[0]);
734 }
735
Damiend99b0522013-12-21 18:17:45 +0000736 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
737 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
738 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
739 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100740 if (assign_kind != ASSIGN_AUG_STORE) {
741 for (int i = 0; i < n - 1; i++) {
742 compile_node(comp, pns1->nodes[i]);
743 }
744 }
Damiend99b0522013-12-21 18:17:45 +0000745 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
746 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100747 }
Damiend99b0522013-12-21 18:17:45 +0000748 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100749 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000750 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100751 if (assign_kind == ASSIGN_AUG_STORE) {
752 EMIT(rot_three);
753 EMIT(store_subscr);
754 } else {
755 compile_node(comp, pns1->nodes[0]);
756 if (assign_kind == ASSIGN_AUG_LOAD) {
757 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100758 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100759 } else {
760 EMIT(store_subscr);
761 }
762 }
Damiend99b0522013-12-21 18:17:45 +0000763 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
764 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100765 if (assign_kind == ASSIGN_AUG_LOAD) {
766 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000767 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100768 } else {
769 if (assign_kind == ASSIGN_AUG_STORE) {
770 EMIT(rot_two);
771 }
Damien Georgeb9791222014-01-23 00:34:21 +0000772 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100773 }
774 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100775 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100776 }
777 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100778 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100779 }
780
Damiend99b0522013-12-21 18:17:45 +0000781 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100782 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100783 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100784
785 return;
786
787cannot_assign:
788 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100789}
790
Damien George0288cf02014-04-11 11:53:00 +0000791// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
792void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
793 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
794
795 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100796 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000797 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
798 EMIT_ARG(unpack_ex, 0, num_tail);
799 have_star_index = 0;
800 }
801 for (int i = 0; i < num_tail; i++) {
802 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100803 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000804 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
805 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100806 } else {
Damien George9d181f62014-04-27 16:55:27 +0100807 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100808 return;
809 }
810 }
811 }
812 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000813 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100814 }
Damien George0288cf02014-04-11 11:53:00 +0000815 if (num_head != 0) {
816 if (0 == have_star_index) {
817 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100818 } else {
Damien George0288cf02014-04-11 11:53:00 +0000819 c_assign(comp, node_head, ASSIGN_STORE);
820 }
821 }
822 for (int i = 0; i < num_tail; i++) {
823 if (num_head + i == have_star_index) {
824 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
825 } else {
826 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100827 }
828 }
829}
830
831// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000832void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100833 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000834 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100835 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000836 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
837 if (MP_PARSE_NODE_IS_ID(pn)) {
838 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100839 switch (assign_kind) {
840 case ASSIGN_STORE:
841 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000842 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100843 break;
844 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000845 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100846 break;
847 }
848 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100849 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100850 return;
851 }
852 } else {
Damiend99b0522013-12-21 18:17:45 +0000853 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
854 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100855 case PN_power:
856 // lhs is an index or attribute
857 c_assign_power(comp, pns, assign_kind);
858 break;
859
860 case PN_testlist_star_expr:
861 case PN_exprlist:
862 // lhs is a tuple
863 if (assign_kind != ASSIGN_STORE) {
864 goto bad_aug;
865 }
Damien George0288cf02014-04-11 11:53:00 +0000866 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100867 break;
868
869 case PN_atom_paren:
870 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000871 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100872 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100873 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000874 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
875 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100876 goto testlist_comp;
877 } else {
878 // parenthesis around 1 item, is just that item
879 pn = pns->nodes[0];
880 goto tail_recursion;
881 }
882 break;
883
884 case PN_atom_bracket:
885 // lhs is something in brackets
886 if (assign_kind != ASSIGN_STORE) {
887 goto bad_aug;
888 }
Damiend99b0522013-12-21 18:17:45 +0000889 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100890 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000891 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000892 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
893 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100894 goto testlist_comp;
895 } else {
896 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000897 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100898 }
899 break;
900
901 default:
Damien George9d181f62014-04-27 16:55:27 +0100902 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100903 }
904 return;
905
906 testlist_comp:
907 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000908 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
909 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
910 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100911 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000912 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000913 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000914 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100915 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000916 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
917 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000918 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100919 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100920 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100921 } else {
922 // sequence with 2 items
923 goto sequence_with_2_items;
924 }
925 } else {
926 // sequence with 2 items
927 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000928 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100929 }
930 return;
931 }
932 return;
933
Damien George9d181f62014-04-27 16:55:27 +0100934 cannot_assign:
935 compile_syntax_error(comp, pn, "can't assign to expression");
936 return;
937
Damien429d7192013-10-04 19:53:11 +0100938 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100939 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100940}
941
942// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100943// if we are not in CPython compatibility mode then:
944// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
945// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
946// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100947void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
948 assert(n_pos_defaults >= 0);
949 assert(n_kw_defaults >= 0);
950
Damien429d7192013-10-04 19:53:11 +0100951 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000952 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100953 int nfree = 0;
954 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000955 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
956 id_info_t *id = &comp->scope_cur->id_info[i];
957 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
958 for (int j = 0; j < this_scope->id_info_len; j++) {
959 id_info_t *id2 = &this_scope->id_info[j];
960 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000961#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000962 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000963#else
964 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100965 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000966#endif
Damien318aec62013-12-10 18:28:17 +0000967 nfree += 1;
968 }
969 }
Damien429d7192013-10-04 19:53:11 +0100970 }
971 }
972 }
Damien429d7192013-10-04 19:53:11 +0100973
974 // make the function/closure
975 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100976 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100977 } else {
Damien George3558f622014-04-20 17:50:40 +0100978 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100979 }
980}
981
Damiend99b0522013-12-21 18:17:45 +0000982void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000983 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100984 comp->have_star = true;
985 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000986 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
987 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100988 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100989 } else {
990 // named star
Damien429d7192013-10-04 19:53:11 +0100991 }
Damien George8b19db02014-04-11 23:25:34 +0100992 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000993
994 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100995 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000996 // TODO do we need to do anything with this?
997
998 } else {
999 mp_parse_node_t pn_id;
1000 mp_parse_node_t pn_colon;
1001 mp_parse_node_t pn_equal;
1002 if (MP_PARSE_NODE_IS_ID(pn)) {
1003 // this parameter is just an id
1004
1005 pn_id = pn;
1006 pn_colon = MP_PARSE_NODE_NULL;
1007 pn_equal = MP_PARSE_NODE_NULL;
1008
1009 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1010 // this parameter has a colon and/or equal specifier
1011
1012 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1013 pn_id = pns->nodes[0];
1014 pn_colon = pns->nodes[1];
1015 pn_equal = pns->nodes[2];
1016
1017 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001018 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001019 assert(0);
1020 return;
1021 }
1022
1023 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1024 // this parameter does not have a default value
1025
1026 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001027 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001028 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001029 return;
1030 }
1031
1032 } else {
1033 // this parameter has a default value
1034 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1035
Damien George8b19db02014-04-11 23:25:34 +01001036 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001037 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001038#if MICROPY_EMIT_CPYTHON
1039 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1040 compile_node(comp, pn_equal);
1041#else
Damien George69b89d22014-04-11 13:38:30 +00001042 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1043 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001044 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1045 // we need to do this here before we start building the map for the default keywords
1046 if (comp->num_default_params > 0) {
1047 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001048 } else {
1049 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001050 }
Damien George69b89d22014-04-11 13:38:30 +00001051 // first default dict param, so make the map
1052 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001053 }
Damien Georgef0778a72014-06-07 22:01:00 +01001054
1055 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001056 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001057 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001058 EMIT(store_map);
1059#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001060 } else {
Damien George69b89d22014-04-11 13:38:30 +00001061 comp->num_default_params += 1;
1062 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001063 }
1064 }
1065
1066 // TODO pn_colon not implemented
1067 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001068 }
1069}
1070
1071// leaves function object on stack
1072// returns function name
Damiend99b0522013-12-21 18:17:45 +00001073qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001074 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001075 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001076 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001077 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001078 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001079 }
1080
1081 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001082 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001083 uint old_num_dict_params = comp->num_dict_params;
1084 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001085
1086 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001087 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001088 comp->num_dict_params = 0;
1089 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001090 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001091
1092 if (comp->had_error) {
1093 return MP_QSTR_NULL;
1094 }
1095
Damien Georgee337f1e2014-03-31 15:18:37 +01001096#if !MICROPY_EMIT_CPYTHON
1097 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001098 // the default keywords args may have already made the tuple; if not, do it now
1099 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001100 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001101 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001102 }
1103#endif
1104
Damien429d7192013-10-04 19:53:11 +01001105 // get the scope for this function
1106 scope_t *fscope = (scope_t*)pns->nodes[4];
1107
1108 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001109 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001110
1111 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001112 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001113 comp->num_dict_params = old_num_dict_params;
1114 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001115
1116 // return its name (the 'f' in "def f(...):")
1117 return fscope->simple_name;
1118}
1119
1120// leaves class object on stack
1121// returns class name
Damiend99b0522013-12-21 18:17:45 +00001122qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001123 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001124 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001125 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001126 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001127 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001128 }
1129
1130 EMIT(load_build_class);
1131
1132 // scope for this class
1133 scope_t *cscope = (scope_t*)pns->nodes[3];
1134
1135 // compile the class
1136 close_over_variables_etc(comp, cscope, 0, 0);
1137
1138 // get its name
Damien George968bf342014-04-27 19:12:05 +01001139 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001140
1141 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001142 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1143 mp_parse_node_t parents = pns->nodes[1];
1144 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1145 parents = MP_PARSE_NODE_NULL;
1146 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001147 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001148 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001149
1150 // return its name (the 'C' in class C(...):")
1151 return cscope->simple_name;
1152}
1153
Damien6cdd3af2013-10-05 18:08:26 +01001154// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001155STATIC 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 +00001156 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001157 return false;
1158 }
1159
1160 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001161 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001162 return true;
1163 }
1164
Damiend99b0522013-12-21 18:17:45 +00001165 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001166 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001167 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001168#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001169 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001170 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001171 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001172 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001173#endif
Damien3ef4abb2013-10-12 16:53:13 +01001174#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001175 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001176 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001177#endif
Damien6cdd3af2013-10-05 18:08:26 +01001178 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001179 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001180 }
1181
1182 return true;
1183}
1184
Damiend99b0522013-12-21 18:17:45 +00001185void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001186 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001187 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001188 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1189
Damien6cdd3af2013-10-05 18:08:26 +01001190 // inherit emit options for this function/class definition
1191 uint emit_options = comp->scope_cur->emit_options;
1192
1193 // compile each decorator
1194 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001195 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001196 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1197 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001198
1199 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001200 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001201 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1202
1203 // check for built-in decorators
1204 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1205 // this was a built-in
1206 num_built_in_decorators += 1;
1207
1208 } else {
1209 // not a built-in, compile normally
1210
1211 // compile the decorator function
1212 compile_node(comp, name_nodes[0]);
1213 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001214 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001215 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001216 }
1217
1218 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001219 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001220 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001221 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001222 compile_node(comp, pns_decorator->nodes[1]);
1223 }
Damien429d7192013-10-04 19:53:11 +01001224 }
1225 }
1226
1227 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001228 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001229 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001230 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001231 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001232 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001233 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001234 } else {
1235 // shouldn't happen
1236 assert(0);
1237 }
1238
1239 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001240 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001241 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001242 }
1243
1244 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001245 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001246}
1247
Damiend99b0522013-12-21 18:17:45 +00001248void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001249 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001250 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001251 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001252}
1253
Damiend99b0522013-12-21 18:17:45 +00001254void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1255 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001256 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001257 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1258 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001259
1260 compile_node(comp, pns->nodes[0]); // base of the power node
1261
Damiend99b0522013-12-21 18:17:45 +00001262 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1263 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1264 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1265 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001266 for (int i = 0; i < n - 1; i++) {
1267 compile_node(comp, pns1->nodes[i]);
1268 }
Damiend99b0522013-12-21 18:17:45 +00001269 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1270 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001271 }
Damiend99b0522013-12-21 18:17:45 +00001272 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001273 // can't delete function calls
1274 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001275 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001276 compile_node(comp, pns1->nodes[0]);
1277 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001278 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1279 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001280 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001281 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001282 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001283 }
1284 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001285 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001286 }
1287
Damiend99b0522013-12-21 18:17:45 +00001288 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001289 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001290 }
Damiend99b0522013-12-21 18:17:45 +00001291 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1292 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1293 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1294 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001295 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1296
Damiend99b0522013-12-21 18:17:45 +00001297 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1298 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1299 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001300 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001301 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001302 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001303 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001304 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001305 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001306 c_del_stmt(comp, pns->nodes[0]);
1307 for (int i = 0; i < n; i++) {
1308 c_del_stmt(comp, pns1->nodes[i]);
1309 }
Damiend99b0522013-12-21 18:17:45 +00001310 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001311 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001312 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001313 } else {
1314 // sequence with 2 items
1315 goto sequence_with_2_items;
1316 }
1317 } else {
1318 // sequence with 2 items
1319 sequence_with_2_items:
1320 c_del_stmt(comp, pns->nodes[0]);
1321 c_del_stmt(comp, pns->nodes[1]);
1322 }
1323 } else {
1324 // tuple with 1 element
1325 c_del_stmt(comp, pn);
1326 }
1327 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001328 // TODO is there anything else to implement?
1329 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001330 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001331
1332 return;
1333
1334cannot_delete:
1335 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001336}
1337
Damiend99b0522013-12-21 18:17:45 +00001338void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001339 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1340}
1341
Damiend99b0522013-12-21 18:17:45 +00001342void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001343 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001344 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001345 }
Damien Georgecbddb272014-02-01 20:08:18 +00001346 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001347}
1348
Damiend99b0522013-12-21 18:17:45 +00001349void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001350 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001351 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001352 }
Damien Georgecbddb272014-02-01 20:08:18 +00001353 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001354}
1355
Damiend99b0522013-12-21 18:17:45 +00001356void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001357 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001358 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001359 return;
1360 }
Damiend99b0522013-12-21 18:17:45 +00001361 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001362 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001363 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001364 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001365 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001366 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1367 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 +01001368
Damien George6f355fd2014-04-10 14:11:31 +01001369 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001370 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1371 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1372 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001373 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001374 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1375 } else {
1376 compile_node(comp, pns->nodes[0]);
1377 }
1378 EMIT(return_value);
1379}
1380
Damiend99b0522013-12-21 18:17:45 +00001381void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001382 compile_node(comp, pns->nodes[0]);
1383 EMIT(pop_top);
1384}
1385
Damiend99b0522013-12-21 18:17:45 +00001386void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1387 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001388 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001389 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001390 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001391 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001392 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001393 compile_node(comp, pns->nodes[0]);
1394 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001395 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001396 } else {
1397 // raise x
1398 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001399 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001400 }
1401}
1402
Damien George635543c2014-04-10 12:56:52 +01001403// q_base holds the base of the name
1404// eg a -> q_base=a
1405// a.b.c -> q_base=a
1406void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001407 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001408 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1409 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001410 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001411 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001412 pn = pns->nodes[0];
1413 is_as = true;
1414 }
Damien George635543c2014-04-10 12:56:52 +01001415 if (MP_PARSE_NODE_IS_NULL(pn)) {
1416 // empty name (eg, from . import x)
1417 *q_base = MP_QSTR_;
1418 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1419 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001420 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001421 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001422 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001423 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001424 }
Damien George635543c2014-04-10 12:56:52 +01001425 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001426 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1427 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1428 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001429 // a name of the form a.b.c
1430 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001431 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001432 }
Damiend99b0522013-12-21 18:17:45 +00001433 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001434 int len = n - 1;
1435 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001436 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001437 }
Damien George55baff42014-01-21 21:40:13 +00001438 byte *q_ptr;
1439 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001440 for (int i = 0; i < n; i++) {
1441 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001442 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001443 }
Damien George55baff42014-01-21 21:40:13 +00001444 uint str_src_len;
1445 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001446 memcpy(str_dest, str_src, str_src_len);
1447 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001448 }
Damien George635543c2014-04-10 12:56:52 +01001449 qstr q_full = qstr_build_end(q_ptr);
1450 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001451 if (is_as) {
1452 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001453 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001454 }
1455 }
1456 } else {
Damien George635543c2014-04-10 12:56:52 +01001457 // shouldn't happen
1458 assert(0);
Damien429d7192013-10-04 19:53:11 +01001459 }
1460 } else {
Damien George635543c2014-04-10 12:56:52 +01001461 // shouldn't happen
1462 assert(0);
Damien429d7192013-10-04 19:53:11 +01001463 }
1464}
1465
Damiend99b0522013-12-21 18:17:45 +00001466void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001467 EMIT_ARG(load_const_small_int, 0); // level 0 import
1468 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1469 qstr q_base;
1470 do_import_name(comp, pn, &q_base);
1471 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001472}
1473
Damiend99b0522013-12-21 18:17:45 +00001474void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001475 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1476}
1477
Damiend99b0522013-12-21 18:17:45 +00001478void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001479 mp_parse_node_t pn_import_source = pns->nodes[0];
1480
1481 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1482 uint import_level = 0;
1483 do {
1484 mp_parse_node_t pn_rel;
1485 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)) {
1486 // This covers relative imports with dots only like "from .. import"
1487 pn_rel = pn_import_source;
1488 pn_import_source = MP_PARSE_NODE_NULL;
1489 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1490 // This covers relative imports starting with dot(s) like "from .foo import"
1491 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1492 pn_rel = pns_2b->nodes[0];
1493 pn_import_source = pns_2b->nodes[1];
1494 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1495 } else {
1496 // Not a relative import
1497 break;
1498 }
1499
1500 // get the list of . and/or ...'s
1501 mp_parse_node_t *nodes;
1502 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1503
1504 // count the total number of .'s
1505 for (int i = 0; i < n; i++) {
1506 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1507 import_level++;
1508 } else {
1509 // should be an MP_TOKEN_ELLIPSIS
1510 import_level += 3;
1511 }
1512 }
1513 } while (0);
1514
Damiend99b0522013-12-21 18:17:45 +00001515 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001516 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001517
1518 // build the "fromlist" tuple
1519#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001520 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001521#else
Damien George708c0732014-04-27 19:23:46 +01001522 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001523 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001524#endif
1525
1526 // do the import
Damien George635543c2014-04-10 12:56:52 +01001527 qstr dummy_q;
1528 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001529 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001530
Damien429d7192013-10-04 19:53:11 +01001531 } else {
Damien George635543c2014-04-10 12:56:52 +01001532 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001533
1534 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001535 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001536 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001537#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001538 {
1539 vstr_t *vstr = vstr_new();
1540 vstr_printf(vstr, "(");
1541 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001542 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1543 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1544 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001545 if (i > 0) {
1546 vstr_printf(vstr, ", ");
1547 }
1548 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001549 uint len;
1550 const byte *str = qstr_data(id2, &len);
1551 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001552 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001553 }
Damien02f89412013-12-12 15:13:36 +00001554 if (n == 1) {
1555 vstr_printf(vstr, ",");
1556 }
1557 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001558 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001559 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001560 }
Damiendb4c3612013-12-10 17:27:24 +00001561#else
1562 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001563 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1564 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1565 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001566 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001567 }
Damien Georgeb9791222014-01-23 00:34:21 +00001568 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001569#endif
1570
1571 // do the import
Damien George635543c2014-04-10 12:56:52 +01001572 qstr dummy_q;
1573 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001574 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001575 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1576 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1577 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001578 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001579 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001580 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001581 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001582 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001583 }
1584 }
1585 EMIT(pop_top);
1586 }
1587}
1588
Damiend99b0522013-12-21 18:17:45 +00001589void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001590 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001591 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1592 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001593 } else {
Damiend99b0522013-12-21 18:17:45 +00001594 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1595 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001596 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001597 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001598 }
Damien429d7192013-10-04 19:53:11 +01001599 }
1600 }
1601}
1602
Damiend99b0522013-12-21 18:17:45 +00001603void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001604 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001605 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1606 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001607 } else {
Damiend99b0522013-12-21 18:17:45 +00001608 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1609 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001610 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001611 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001612 }
Damien429d7192013-10-04 19:53:11 +01001613 }
1614 }
1615}
1616
Damiend99b0522013-12-21 18:17:45 +00001617void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001618 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001619 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001620 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 +00001621 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001622 // assertion message
1623 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001624 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001625 }
Damien Georgeb9791222014-01-23 00:34:21 +00001626 EMIT_ARG(raise_varargs, 1);
1627 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001628}
1629
Damiend99b0522013-12-21 18:17:45 +00001630void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001631 // TODO proper and/or short circuiting
1632
Damien George6f355fd2014-04-10 14:11:31 +01001633 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001634
Damien George6f355fd2014-04-10 14:11:31 +01001635 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001636 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1637
1638 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001639
1640 if (
1641#if !MICROPY_EMIT_CPYTHON
1642 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1643 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1644#endif
1645 // optimisation to not jump if last instruction was return
1646 !EMIT(last_emit_was_return_value)
1647 ) {
1648 // jump over elif/else blocks
1649 EMIT_ARG(jump, l_end);
1650 }
1651
Damien Georgeb9791222014-01-23 00:34:21 +00001652 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001653
Damiend99b0522013-12-21 18:17:45 +00001654 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001655 // compile elif blocks
1656
Damiend99b0522013-12-21 18:17:45 +00001657 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001658
Damiend99b0522013-12-21 18:17:45 +00001659 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001660 // multiple elif blocks
1661
Damiend99b0522013-12-21 18:17:45 +00001662 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001663 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001664 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001665 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001666 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1667
1668 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001669 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001670 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001671 }
Damien Georgeb9791222014-01-23 00:34:21 +00001672 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001673 }
1674
1675 } else {
1676 // a single elif block
1677
Damienb05d7072013-10-05 13:37:10 +01001678 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001679 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1680
1681 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001682 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001683 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001684 }
Damien Georgeb9791222014-01-23 00:34:21 +00001685 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001686 }
1687 }
1688
1689 // compile else block
1690 compile_node(comp, pns->nodes[3]); // can be null
1691
Damien Georgeb9791222014-01-23 00:34:21 +00001692 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001693}
1694
Damien Georgecbddb272014-02-01 20:08:18 +00001695#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001696 uint old_break_label = comp->break_label; \
1697 uint old_continue_label = comp->continue_label; \
1698 uint break_label = comp_next_label(comp); \
1699 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001700 comp->break_label = break_label; \
1701 comp->continue_label = continue_label; \
1702 comp->break_continue_except_level = comp->cur_except_level;
1703
1704#define END_BREAK_CONTINUE_BLOCK \
1705 comp->break_label = old_break_label; \
1706 comp->continue_label = old_continue_label; \
1707 comp->break_continue_except_level = comp->cur_except_level;
1708
Damiend99b0522013-12-21 18:17:45 +00001709void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001710 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001711
Damience89a212013-10-15 22:25:17 +01001712 // compared to CPython, we have an optimised version of while loops
1713#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001714 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001715 EMIT_ARG(setup_loop, break_label);
1716 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001717 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1718 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001719 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001720 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001721 }
Damien Georgeb9791222014-01-23 00:34:21 +00001722 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001723 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1724 // this is a small hack to agree with CPython
1725 if (!node_is_const_true(pns->nodes[0])) {
1726 EMIT(pop_block);
1727 }
Damience89a212013-10-15 22:25:17 +01001728#else
Damien George6f355fd2014-04-10 14:11:31 +01001729 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001730 EMIT_ARG(jump, continue_label);
1731 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001732 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001733 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001734 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1735#endif
1736
1737 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001738 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001739
1740 compile_node(comp, pns->nodes[2]); // else
1741
Damien Georgeb9791222014-01-23 00:34:21 +00001742 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001743}
1744
Damienf72fd0e2013-11-06 20:20:49 +00001745// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001746// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1747// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001748void 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 +00001749 START_BREAK_CONTINUE_BLOCK
Damien George5b5562c2014-05-31 17:59:11 +01001750 // note that we don't need to pop anything when breaking from an optimise for loop
Damienf72fd0e2013-11-06 20:20:49 +00001751
Damien George6f355fd2014-04-10 14:11:31 +01001752 uint top_label = comp_next_label(comp);
1753 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001754
Damien George3ff2d032014-03-31 18:02:22 +01001755 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001756 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001757 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001758
Damien Georgeb9791222014-01-23 00:34:21 +00001759 EMIT_ARG(jump, entry_label);
1760 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001761
Damien George3ff2d032014-03-31 18:02:22 +01001762 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001763 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001764
1765 // store next value to var
1766 c_assign(comp, pn_var, ASSIGN_STORE);
1767
Damienf3822fc2013-11-09 20:12:03 +00001768 // compile body
1769 compile_node(comp, pn_body);
1770
Damien Georgeb9791222014-01-23 00:34:21 +00001771 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001772
Damien George3ff2d032014-03-31 18:02:22 +01001773 // compile: var + step, duplicated on stack
1774 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001775 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001776 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001777 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001778
Damien Georgeb9791222014-01-23 00:34:21 +00001779 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001780
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001781 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001782 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001783 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1784 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001785 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001786 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001787 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001788 }
Damien Georgeb9791222014-01-23 00:34:21 +00001789 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001790
Damien George3ff2d032014-03-31 18:02:22 +01001791 // discard final value of var that failed the loop condition
1792 EMIT(pop_top);
1793
Damienf72fd0e2013-11-06 20:20:49 +00001794 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001795 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001796
1797 compile_node(comp, pn_else);
1798
Damien Georgeb9791222014-01-23 00:34:21 +00001799 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001800}
1801
Damiend99b0522013-12-21 18:17:45 +00001802void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001803#if !MICROPY_EMIT_CPYTHON
1804 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1805 // this is actually slower, but uses no heap memory
1806 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001807 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 +00001808 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001809 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1810 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1811 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1812 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001813 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1814 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001815 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001816 mp_parse_node_t pn_range_start;
1817 mp_parse_node_t pn_range_end;
1818 mp_parse_node_t pn_range_step;
1819 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001820 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001821 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001822 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001823 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001824 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001825 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001826 } else if (n_args == 2) {
1827 pn_range_start = args[0];
1828 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001829 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001830 } else {
1831 pn_range_start = args[0];
1832 pn_range_end = args[1];
1833 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001834 // We need to know sign of step. This is possible only if it's constant
1835 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1836 optimize = false;
1837 }
Damienf72fd0e2013-11-06 20:20:49 +00001838 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001839 }
1840 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001841 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1842 return;
1843 }
1844 }
1845 }
1846#endif
1847
Damien Georgecbddb272014-02-01 20:08:18 +00001848 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001849 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001850
Damien George6f355fd2014-04-10 14:11:31 +01001851 uint pop_label = comp_next_label(comp);
1852 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001853
Damience89a212013-10-15 22:25:17 +01001854 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1855#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001856 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001857#endif
1858
Damien429d7192013-10-04 19:53:11 +01001859 compile_node(comp, pns->nodes[1]); // iterator
1860 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001861 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001862 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001863 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1864 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001865 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001866 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001867 }
Damien Georgeb9791222014-01-23 00:34:21 +00001868 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001869 EMIT(for_iter_end);
1870
1871 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001872 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001873
Damience89a212013-10-15 22:25:17 +01001874#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001875 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001876#endif
Damien429d7192013-10-04 19:53:11 +01001877
1878 compile_node(comp, pns->nodes[3]); // else (not tested)
1879
Damien Georgeb9791222014-01-23 00:34:21 +00001880 EMIT_ARG(label_assign, break_label);
1881 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001882}
1883
Damiend99b0522013-12-21 18:17:45 +00001884void 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 +01001885 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001886 uint l1 = comp_next_label(comp);
1887 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001888
Damien Georgeb9791222014-01-23 00:34:21 +00001889 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001890 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001891
Damien429d7192013-10-04 19:53:11 +01001892 compile_node(comp, pn_body); // body
1893 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001894 EMIT_ARG(jump, success_label); // jump over exception handler
1895
1896 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001897 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 +00001898
Damien George6f355fd2014-04-10 14:11:31 +01001899 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001900
1901 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001902 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1903 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001904
1905 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001906 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001907
Damiend99b0522013-12-21 18:17:45 +00001908 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001909 // this is a catch all exception handler
1910 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001911 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001912 return;
1913 }
1914 } else {
1915 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001916 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1917 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1918 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1919 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001920 // handler binds the exception to a local
1921 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001922 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001923 }
1924 }
1925 EMIT(dup_top);
1926 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001927 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001928 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001929 }
1930
1931 EMIT(pop_top);
1932
1933 if (qstr_exception_local == 0) {
1934 EMIT(pop_top);
1935 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001936 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001937 }
1938
1939 EMIT(pop_top);
1940
Damien George6f355fd2014-04-10 14:11:31 +01001941 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001942 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001943 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001944 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001945 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001946 }
1947 compile_node(comp, pns_except->nodes[1]);
1948 if (qstr_exception_local != 0) {
1949 EMIT(pop_block);
1950 }
1951 EMIT(pop_except);
1952 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001953 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1954 EMIT_ARG(label_assign, l3);
1955 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1956 EMIT_ARG(store_id, qstr_exception_local);
1957 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001958
Damien George8dcc0c72014-03-27 10:55:21 +00001959 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001960 EMIT(end_finally);
1961 }
Damien Georgeb9791222014-01-23 00:34:21 +00001962 EMIT_ARG(jump, l2);
1963 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001964 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001965 }
1966
Damien George8dcc0c72014-03-27 10:55:21 +00001967 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001968 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001969 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001970
Damien Georgeb9791222014-01-23 00:34:21 +00001971 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001972 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001973 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001974}
1975
Damiend99b0522013-12-21 18:17:45 +00001976void 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 +01001977 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001978
Damien Georgeb9791222014-01-23 00:34:21 +00001979 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001980 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001981
Damien429d7192013-10-04 19:53:11 +01001982 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001983 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001984 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001985 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001986 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001987 } else {
1988 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1989 }
1990 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001991 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1992 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001993 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001994
Damien George8dcc0c72014-03-27 10:55:21 +00001995 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001996 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001997}
1998
Damiend99b0522013-12-21 18:17:45 +00001999void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2000 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2001 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2002 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002003 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002004 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2005 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002006 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002007 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002008 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002009 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002010 // no finally
2011 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2012 } else {
2013 // have finally
Damiend99b0522013-12-21 18:17:45 +00002014 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 +01002015 }
2016 } else {
2017 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002018 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002019 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002020 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002021 }
2022 } else {
2023 // shouldn't happen
2024 assert(0);
2025 }
2026}
2027
Damiend99b0522013-12-21 18:17:45 +00002028void 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 +01002029 if (n == 0) {
2030 // no more pre-bits, compile the body of the with
2031 compile_node(comp, body);
2032 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002033 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002034 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002035 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002036 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002037 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002038 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002039 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2040 } else {
2041 // this pre-bit is just an expression
2042 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002043 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002044 EMIT(pop_top);
2045 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002046 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002047 // compile additional pre-bits and the body
2048 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2049 // finish this with block
2050 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002051 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2052 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002053 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002054 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002055 EMIT(end_finally);
2056 }
2057}
2058
Damiend99b0522013-12-21 18:17:45 +00002059void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002060 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002061 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002062 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2063 assert(n > 0);
2064
2065 // compile in a nested fashion
2066 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2067}
2068
Damiend99b0522013-12-21 18:17:45 +00002069void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2070 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002071 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2072 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002073 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002074 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002075 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002076 EMIT(pop_top);
2077
Damien429d7192013-10-04 19:53:11 +01002078 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002079 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002080 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2081 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002082 // do nothing with a lonely constant
2083 } else {
2084 compile_node(comp, pns->nodes[0]); // just an expression
2085 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2086 }
Damien429d7192013-10-04 19:53:11 +01002087 }
2088 } else {
Damiend99b0522013-12-21 18:17:45 +00002089 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2090 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002091 if (kind == PN_expr_stmt_augassign) {
2092 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2093 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002094 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002095 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002096 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002097 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2098 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2099 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2100 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2101 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2102 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2103 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2104 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2105 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2106 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2107 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2108 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2109 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002110 }
Damien George7e5fb242014-02-01 22:18:47 +00002111 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002112 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2113 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002114 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2115 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002116 // following CPython, we store left-most first
2117 if (rhs > 0) {
2118 EMIT(dup_top);
2119 }
2120 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2121 for (int i = 0; i < rhs; i++) {
2122 if (i + 1 < rhs) {
2123 EMIT(dup_top);
2124 }
Damiend99b0522013-12-21 18:17:45 +00002125 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002126 }
2127 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002128 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002129 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2130 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2131 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002132 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002133 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2134 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002135 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2136 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2137 // can't optimise when it's a star expression on the lhs
2138 goto no_optimisation;
2139 }
Damien429d7192013-10-04 19:53:11 +01002140 compile_node(comp, pns10->nodes[0]); // rhs
2141 compile_node(comp, pns10->nodes[1]); // rhs
2142 EMIT(rot_two);
2143 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2144 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002145 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2146 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2147 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2148 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002149 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002150 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2151 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002152 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2153 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2154 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2155 // can't optimise when it's a star expression on the lhs
2156 goto no_optimisation;
2157 }
Damien429d7192013-10-04 19:53:11 +01002158 compile_node(comp, pns10->nodes[0]); // rhs
2159 compile_node(comp, pns10->nodes[1]); // rhs
2160 compile_node(comp, pns10->nodes[2]); // rhs
2161 EMIT(rot_three);
2162 EMIT(rot_two);
2163 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2164 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2165 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2166 } else {
Damien George495d7812014-04-08 17:51:47 +01002167 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002168 compile_node(comp, pns1->nodes[0]); // rhs
2169 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2170 }
2171 } else {
2172 // shouldn't happen
2173 assert(0);
2174 }
2175 }
2176}
2177
Damien Georged17926d2014-03-30 13:35:08 +01002178void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002179 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002180 compile_node(comp, pns->nodes[0]);
2181 for (int i = 1; i < num_nodes; i += 1) {
2182 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002183 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002184 }
2185}
2186
Damiend99b0522013-12-21 18:17:45 +00002187void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2188 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2189 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002190
Damien George6f355fd2014-04-10 14:11:31 +01002191 uint l_fail = comp_next_label(comp);
2192 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002193 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2194 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002195 EMIT_ARG(jump, l_end);
2196 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002197 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002198 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002199 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002200}
2201
Damiend99b0522013-12-21 18:17:45 +00002202void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002203 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002204 //mp_parse_node_t pn_params = pns->nodes[0];
2205 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002206
Damien George36db6bc2014-05-07 17:24:22 +01002207 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002208 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002209 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 +01002210 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002211 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002212 }
2213
2214 // get the scope for this lambda
2215 scope_t *this_scope = (scope_t*)pns->nodes[2];
2216
2217 // make the lambda
2218 close_over_variables_etc(comp, this_scope, 0, 0);
2219}
2220
Damiend99b0522013-12-21 18:17:45 +00002221void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002222 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002223 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002224 for (int i = 0; i < n; i += 1) {
2225 compile_node(comp, pns->nodes[i]);
2226 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002227 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002228 }
2229 }
Damien Georgeb9791222014-01-23 00:34:21 +00002230 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002231}
2232
Damiend99b0522013-12-21 18:17:45 +00002233void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002234 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002235 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002236 for (int i = 0; i < n; i += 1) {
2237 compile_node(comp, pns->nodes[i]);
2238 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002239 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002240 }
2241 }
Damien Georgeb9791222014-01-23 00:34:21 +00002242 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002243}
2244
Damiend99b0522013-12-21 18:17:45 +00002245void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002246 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002247 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002248}
2249
Damiend99b0522013-12-21 18:17:45 +00002250void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002251 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002252 compile_node(comp, pns->nodes[0]);
2253 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002254 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002255 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002256 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002257 }
2258 for (int i = 1; i + 1 < num_nodes; i += 2) {
2259 compile_node(comp, pns->nodes[i + 1]);
2260 if (i + 2 < num_nodes) {
2261 EMIT(dup_top);
2262 EMIT(rot_three);
2263 }
Damien George7e5fb242014-02-01 22:18:47 +00002264 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002265 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002266 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002267 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2268 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2269 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2270 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2271 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2272 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2273 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2274 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002275 }
2276 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002277 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2278 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2279 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002280 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002281 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002282 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002283 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002284 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002285 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002286 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002287 }
2288 } else {
2289 // shouldn't happen
2290 assert(0);
2291 }
2292 } else {
2293 // shouldn't happen
2294 assert(0);
2295 }
2296 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002297 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002298 }
2299 }
2300 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002301 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002302 EMIT_ARG(jump, l_end);
2303 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002304 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002305 EMIT(rot_two);
2306 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002307 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002308 }
2309}
2310
Damiend99b0522013-12-21 18:17:45 +00002311void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002312 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002313}
2314
Damiend99b0522013-12-21 18:17:45 +00002315void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002316 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002317}
2318
Damiend99b0522013-12-21 18:17:45 +00002319void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002320 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002321}
2322
Damiend99b0522013-12-21 18:17:45 +00002323void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002324 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002325}
2326
Damiend99b0522013-12-21 18:17:45 +00002327void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2328 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002329 compile_node(comp, pns->nodes[0]);
2330 for (int i = 1; i + 1 < num_nodes; i += 2) {
2331 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002332 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002333 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002334 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002335 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002336 } else {
2337 // shouldn't happen
2338 assert(0);
2339 }
2340 }
2341}
2342
Damiend99b0522013-12-21 18:17:45 +00002343void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2344 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002345 compile_node(comp, pns->nodes[0]);
2346 for (int i = 1; i + 1 < num_nodes; i += 2) {
2347 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002348 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002349 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002350 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002351 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002352 } else {
2353 // shouldn't happen
2354 assert(0);
2355 }
2356 }
2357}
2358
Damiend99b0522013-12-21 18:17:45 +00002359void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2360 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002361 compile_node(comp, pns->nodes[0]);
2362 for (int i = 1; i + 1 < num_nodes; i += 2) {
2363 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002364 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002365 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002366 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002367 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002368 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002369 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002370 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002371 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002372 } else {
2373 // shouldn't happen
2374 assert(0);
2375 }
2376 }
2377}
2378
Damiend99b0522013-12-21 18:17:45 +00002379void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002380 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002381 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002382 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002383 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002384 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002385 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002386 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002387 } else {
2388 // shouldn't happen
2389 assert(0);
2390 }
2391}
2392
Damien George35e2a4e2014-02-05 00:51:47 +00002393void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2394 // this is to handle special super() call
2395 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2396
2397 compile_generic_all_nodes(comp, pns);
2398}
2399
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002400STATIC 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 +01002401 // function to call is on top of stack
2402
Damien George35e2a4e2014-02-05 00:51:47 +00002403#if !MICROPY_EMIT_CPYTHON
2404 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002405 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 +00002406 EMIT_ARG(load_id, MP_QSTR___class__);
2407 // get first argument to function
2408 bool found = false;
2409 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002410 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2411 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 +00002412 found = true;
2413 break;
2414 }
2415 }
2416 if (!found) {
2417 printf("TypeError: super() call cannot find self\n");
2418 return;
2419 }
Damien George922ddd62014-04-09 12:43:17 +01002420 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002421 return;
2422 }
2423#endif
2424
Damien George2c0842b2014-04-27 16:46:51 +01002425 // get the list of arguments
2426 mp_parse_node_t *args;
2427 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002428
Damien George2c0842b2014-04-27 16:46:51 +01002429 // compile the arguments
2430 // Rather than calling compile_node on the list, we go through the list of args
2431 // explicitly here so that we can count the number of arguments and give sensible
2432 // error messages.
2433 int n_positional = n_positional_extra;
2434 uint n_keyword = 0;
2435 uint star_flags = 0;
2436 for (int i = 0; i < n_args; i++) {
2437 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2438 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2439 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2440 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2441 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2442 return;
2443 }
2444 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2445 compile_node(comp, pns_arg->nodes[0]);
2446 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2447 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2448 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2449 return;
2450 }
2451 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2452 compile_node(comp, pns_arg->nodes[0]);
2453 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2454 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2455 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2456 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2457 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2458 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2459 return;
2460 }
Damien George968bf342014-04-27 19:12:05 +01002461 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002462 compile_node(comp, pns2->nodes[0]);
2463 n_keyword += 1;
2464 } else {
2465 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2466 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2467 n_positional++;
2468 }
2469 } else {
2470 goto normal_argument;
2471 }
2472 } else {
2473 normal_argument:
2474 if (n_keyword > 0) {
2475 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2476 return;
2477 }
2478 compile_node(comp, args[i]);
2479 n_positional++;
2480 }
Damien429d7192013-10-04 19:53:11 +01002481 }
2482
Damien George2c0842b2014-04-27 16:46:51 +01002483 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002484 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002485 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002486 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002487 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002488 }
Damien429d7192013-10-04 19:53:11 +01002489}
2490
Damiend99b0522013-12-21 18:17:45 +00002491void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2492 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002493 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002494 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 +01002495 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002496 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2497 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002498 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002499 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002500 i += 1;
2501 } else {
2502 compile_node(comp, pns->nodes[i]);
2503 }
Damien George35e2a4e2014-02-05 00:51:47 +00002504 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002505 }
2506}
2507
Damiend99b0522013-12-21 18:17:45 +00002508void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002509 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002510 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002511}
2512
Damiend99b0522013-12-21 18:17:45 +00002513void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002514 // a list of strings
Damien63321742013-12-10 17:41:49 +00002515
2516 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002517 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002518 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002519 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002520 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002521 int pn_kind;
2522 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2523 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2524 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2525 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2526 } else {
2527 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2528 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2529 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2530 pn_kind = MP_PARSE_NODE_STRING;
2531 n_bytes += (machine_uint_t)pns_string->nodes[1];
2532 }
Damien63321742013-12-10 17:41:49 +00002533 if (i == 0) {
2534 string_kind = pn_kind;
2535 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002536 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002537 return;
2538 }
Damien429d7192013-10-04 19:53:11 +01002539 }
Damien63321742013-12-10 17:41:49 +00002540
Damien63321742013-12-10 17:41:49 +00002541 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002542 byte *q_ptr;
2543 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002544 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002545 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2546 uint s_len;
2547 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2548 memcpy(s_dest, s, s_len);
2549 s_dest += s_len;
2550 } else {
2551 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2552 memcpy(s_dest, (const char*)pns_string->nodes[0], (machine_uint_t)pns_string->nodes[1]);
2553 s_dest += (machine_uint_t)pns_string->nodes[1];
2554 }
Damien63321742013-12-10 17:41:49 +00002555 }
Damien George55baff42014-01-21 21:40:13 +00002556 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002557
Damien Georgeb9791222014-01-23 00:34:21 +00002558 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002559}
2560
2561// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002562void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2563 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2564 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2565 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002566
Damien George36db6bc2014-05-07 17:24:22 +01002567 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002568 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002569 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 +01002570 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002571 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002572 }
2573
2574 // get the scope for this comprehension
2575 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2576
2577 // compile the comprehension
2578 close_over_variables_etc(comp, this_scope, 0, 0);
2579
2580 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2581 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002582 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002583}
2584
Damiend99b0522013-12-21 18:17:45 +00002585void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2586 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002587 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002588 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2589 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2590 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2591 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2592 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2593 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2594 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002595 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002596 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002597 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002598 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002599 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002600 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002601 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002602 // generator expression
2603 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2604 } else {
2605 // tuple with 2 items
2606 goto tuple_with_2_items;
2607 }
2608 } else {
2609 // tuple with 2 items
2610 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002611 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002612 }
2613 } else {
2614 // parenthesis around a single item, is just that item
2615 compile_node(comp, pns->nodes[0]);
2616 }
2617}
2618
Damiend99b0522013-12-21 18:17:45 +00002619void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2620 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002621 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002622 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002623 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2624 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2625 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2626 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2627 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002628 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002629 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002630 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002631 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002632 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002633 // list of many items
2634 compile_node(comp, pns2->nodes[0]);
2635 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002636 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002637 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002638 // list comprehension
2639 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2640 } else {
2641 // list with 2 items
2642 goto list_with_2_items;
2643 }
2644 } else {
2645 // list with 2 items
2646 list_with_2_items:
2647 compile_node(comp, pns2->nodes[0]);
2648 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002649 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002650 }
2651 } else {
2652 // list with 1 item
2653 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002654 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002655 }
2656}
2657
Damiend99b0522013-12-21 18:17:45 +00002658void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2659 mp_parse_node_t pn = pns->nodes[0];
2660 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002661 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002662 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002663 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2664 pns = (mp_parse_node_struct_t*)pn;
2665 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002666 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002667 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002668 compile_node(comp, pn);
2669 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002670 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2671 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2672 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2673 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002674 // dict/set with multiple elements
2675
2676 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002677 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002678 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2679
2680 // first element sets whether it's a dict or set
2681 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002682 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002683 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002684 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002685 compile_node(comp, pns->nodes[0]);
2686 EMIT(store_map);
2687 is_dict = true;
2688 } else {
2689 // a set
2690 compile_node(comp, pns->nodes[0]); // 1st value of set
2691 is_dict = false;
2692 }
2693
2694 // process rest of elements
2695 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002696 mp_parse_node_t pn = nodes[i];
2697 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002698 compile_node(comp, pn);
2699 if (is_dict) {
2700 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002701 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002702 return;
2703 }
2704 EMIT(store_map);
2705 } else {
2706 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002707 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002708 return;
2709 }
2710 }
2711 }
2712
2713 // if it's a set, build it
2714 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002715 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002716 }
Damiend99b0522013-12-21 18:17:45 +00002717 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002718 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002719 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002720 // a dictionary comprehension
2721 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2722 } else {
2723 // a set comprehension
2724 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2725 }
2726 } else {
2727 // shouldn't happen
2728 assert(0);
2729 }
2730 } else {
2731 // set with one element
2732 goto set_with_one_element;
2733 }
2734 } else {
2735 // set with one element
2736 set_with_one_element:
2737 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002738 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002739 }
2740}
2741
Damiend99b0522013-12-21 18:17:45 +00002742void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002743 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002744}
2745
Damiend99b0522013-12-21 18:17:45 +00002746void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002747 // object who's index we want is on top of stack
2748 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002749 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002750}
2751
Damiend99b0522013-12-21 18:17:45 +00002752void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002753 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002754 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002755}
2756
Damiend99b0522013-12-21 18:17:45 +00002757void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2758 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2759 mp_parse_node_t pn = pns->nodes[0];
2760 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002761 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002762 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2763 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002764 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2765 pns = (mp_parse_node_struct_t*)pn;
2766 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002767 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002768 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002769 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002770 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002771 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002772 } else {
2773 // [?::x]
2774 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002775 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002776 }
Damiend99b0522013-12-21 18:17:45 +00002777 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002778 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002779 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2780 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2781 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2782 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002783 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002784 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002785 } else {
2786 // [?:x:x]
2787 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002788 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002789 }
2790 } else {
2791 // [?:x]
2792 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002793 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002794 }
2795 } else {
2796 // [?:x]
2797 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002798 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002799 }
2800}
2801
Damiend99b0522013-12-21 18:17:45 +00002802void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002803 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002804 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2805 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002806}
2807
Damiend99b0522013-12-21 18:17:45 +00002808void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002809 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002810 compile_subscript_3_helper(comp, pns);
2811}
2812
Damiend99b0522013-12-21 18:17:45 +00002813void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002814 // if this is called then we are compiling a dict key:value pair
2815 compile_node(comp, pns->nodes[1]); // value
2816 compile_node(comp, pns->nodes[0]); // key
2817}
2818
Damiend99b0522013-12-21 18:17:45 +00002819void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002820 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002821 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002822 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002823}
2824
Damiend99b0522013-12-21 18:17:45 +00002825void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002826 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002827 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002828 return;
2829 }
Damiend99b0522013-12-21 18:17:45 +00002830 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002831 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002832 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002833 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2834 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002835 compile_node(comp, pns->nodes[0]);
2836 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002837 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002838 EMIT(yield_from);
2839 } else {
2840 compile_node(comp, pns->nodes[0]);
2841 EMIT(yield_value);
2842 }
2843}
2844
Damiend99b0522013-12-21 18:17:45 +00002845typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002846STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002847 NULL,
2848#define nc NULL
2849#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002850#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002851#include "grammar.h"
2852#undef nc
2853#undef c
2854#undef DEF_RULE
2855};
2856
Damiend99b0522013-12-21 18:17:45 +00002857void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2858 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002859 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002860 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2861 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2862 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002863 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002864 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002865 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002866 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002867 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2868 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2869 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2870 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002871 case MP_PARSE_NODE_TOKEN:
2872 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002873 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002874 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002875 // do nothing
2876 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002877 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002878 }
2879 break;
Damien429d7192013-10-04 19:53:11 +01002880 default: assert(0);
2881 }
2882 } else {
Damiend99b0522013-12-21 18:17:45 +00002883 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002884 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002885 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
2886 EMIT_ARG(load_const_str, qstr_from_strn((const char*)pns->nodes[0], (machine_uint_t)pns->nodes[1]), false);
Damien429d7192013-10-04 19:53:11 +01002887 } else {
Damien George5042bce2014-05-25 22:06:06 +01002888 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2889 if (f == NULL) {
2890 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2891#if MICROPY_DEBUG_PRINTERS
2892 mp_parse_node_print(pn, 0);
2893#endif
2894 compile_syntax_error(comp, pn, "internal compiler error");
2895 } else {
2896 f(comp, pns);
2897 }
Damien429d7192013-10-04 19:53:11 +01002898 }
2899 }
2900}
2901
Damiend99b0522013-12-21 18:17:45 +00002902void 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 +01002903 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002904 qstr param_name = MP_QSTR_NULL;
2905 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002906 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2907 if (MP_PARSE_NODE_IS_ID(pn)) {
2908 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002909 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002910 // comes after a star, so counts as a keyword-only parameter
2911 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002912 } else {
Damien George2827d622014-04-27 15:50:52 +01002913 // comes before a star, so counts as a positional parameter
2914 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002915 }
Damienb14de212013-10-06 00:28:28 +01002916 } else {
Damiend99b0522013-12-21 18:17:45 +00002917 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2918 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2919 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2920 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002921 //int node_index = 1; unused
2922 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002923 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002924 // this parameter has an annotation
2925 pn_annotation = pns->nodes[1];
2926 }
2927 //node_index = 2; unused
2928 }
2929 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002930 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002931 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002932 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002933 comp->scope_cur->num_dict_params += 1;
2934 } else {
2935 comp->scope_cur->num_default_params += 1;
2936 }
2937 }
2938 */
Damien George8b19db02014-04-11 23:25:34 +01002939 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002940 // comes after a star, so counts as a keyword-only parameter
2941 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002942 } else {
Damien George2827d622014-04-27 15:50:52 +01002943 // comes before a star, so counts as a positional parameter
2944 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002945 }
Damiend99b0522013-12-21 18:17:45 +00002946 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002947 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002948 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002949 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002950 // bare star
2951 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002952 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002953 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002954 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002955 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002956 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2957 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002958 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002959 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002960 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2961 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002962 pn_annotation = pns->nodes[1];
2963 } else {
2964 // shouldn't happen
2965 assert(0);
2966 }
Damiend99b0522013-12-21 18:17:45 +00002967 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2968 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002969 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002970 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002971 // this parameter has an annotation
2972 pn_annotation = pns->nodes[1];
2973 }
Damien George8725f8f2014-02-15 19:33:11 +00002974 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002975 } else {
Damienb14de212013-10-06 00:28:28 +01002976 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002977 assert(0);
2978 }
Damien429d7192013-10-04 19:53:11 +01002979 }
2980
Damien George2827d622014-04-27 15:50:52 +01002981 if (param_name != MP_QSTR_NULL) {
Damiend99b0522013-12-21 18:17:45 +00002982 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002983 // TODO this parameter has an annotation
2984 }
2985 bool added;
2986 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2987 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002988 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002989 return;
2990 }
Damien429d7192013-10-04 19:53:11 +01002991 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002992 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002993 }
2994}
2995
Damien George2827d622014-04-27 15:50:52 +01002996STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002997 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2998}
2999
Damien George2827d622014-04-27 15:50:52 +01003000STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01003001 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
3002}
3003
Damiend99b0522013-12-21 18:17:45 +00003004void 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 +01003005 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003006 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003007 // no more nested if/for; compile inner expression
3008 compile_node(comp, pn_inner_expr);
3009 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003010 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003011 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003012 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003013 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003014 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003015 } else {
3016 EMIT(yield_value);
3017 EMIT(pop_top);
3018 }
Damiend99b0522013-12-21 18:17:45 +00003019 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003020 // if condition
Damiend99b0522013-12-21 18:17:45 +00003021 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003022 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3023 pn_iter = pns_comp_if->nodes[1];
3024 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003025 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003026 // for loop
Damiend99b0522013-12-21 18:17:45 +00003027 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003028 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003029 uint l_end2 = comp_next_label(comp);
3030 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003031 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003032 EMIT_ARG(label_assign, l_top2);
3033 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003034 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3035 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 +00003036 EMIT_ARG(jump, l_top2);
3037 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003038 EMIT(for_iter_end);
3039 } else {
3040 // shouldn't happen
3041 assert(0);
3042 }
3043}
3044
Damien George1463c1f2014-04-25 23:52:57 +01003045STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3046#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003047 // see http://www.python.org/dev/peps/pep-0257/
3048
3049 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003050 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003051 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003052 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003053 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003054 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3055 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003056 for (int i = 0; i < num_nodes; i++) {
3057 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003058 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 +00003059 // not a newline, so this is the first statement; finish search
3060 break;
3061 }
3062 }
3063 // 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 +00003064 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003065 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003066 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003067 } else {
3068 return;
3069 }
3070
3071 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003072 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3073 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003074 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3075 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3076 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3077 // compile the doc string
3078 compile_node(comp, pns->nodes[0]);
3079 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003080 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003081 }
3082 }
Damien George1463c1f2014-04-25 23:52:57 +01003083#endif
Damien429d7192013-10-04 19:53:11 +01003084}
3085
Damien George1463c1f2014-04-25 23:52:57 +01003086STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003087 comp->pass = pass;
3088 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003089 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003090 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003091
Damien George36db6bc2014-05-07 17:24:22 +01003092 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003093 // reset maximum stack sizes in scope
3094 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003095 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003096 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003097 }
3098
Damien5ac1b2e2013-10-18 19:58:12 +01003099#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003100 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003101 scope_print_info(scope);
3102 }
Damien5ac1b2e2013-10-18 19:58:12 +01003103#endif
Damien429d7192013-10-04 19:53:11 +01003104
3105 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003106 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3107 assert(scope->kind == SCOPE_MODULE);
3108 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3109 compile_node(comp, pns->nodes[0]); // compile the expression
3110 EMIT(return_value);
3111 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003112 if (!comp->is_repl) {
3113 check_for_doc_string(comp, scope->pn);
3114 }
Damien429d7192013-10-04 19:53:11 +01003115 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003116 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003117 EMIT(return_value);
3118 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003119 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3120 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3121 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003122
3123 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003124 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003125 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003126 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003127 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3128 }
3129
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003130 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003131
3132 compile_node(comp, pns->nodes[3]); // 3 is function body
3133 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003134 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003135 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003136 EMIT(return_value);
3137 }
3138 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003139 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3140 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3141 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003142
3143 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003144 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003145 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003146 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003147 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3148 }
3149
3150 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003151
3152 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3153 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3154 EMIT(pop_top);
3155 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3156 }
Damien429d7192013-10-04 19:53:11 +01003157 EMIT(return_value);
3158 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3159 // a bit of a hack at the moment
3160
Damiend99b0522013-12-21 18:17:45 +00003161 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3162 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3163 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3164 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3165 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003166
Damien George708c0732014-04-27 19:23:46 +01003167 // We need a unique name for the comprehension argument (the iterator).
3168 // CPython uses .0, but we should be able to use anything that won't
3169 // clash with a user defined variable. Best to use an existing qstr,
3170 // so we use the blank qstr.
3171#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003172 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003173#else
3174 qstr qstr_arg = MP_QSTR_;
3175#endif
Damien George36db6bc2014-05-07 17:24:22 +01003176 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003177 bool added;
3178 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3179 assert(added);
3180 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003181 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003182 }
3183
3184 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003185 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003186 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003187 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003188 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003189 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003190 }
3191
Damien George6f355fd2014-04-10 14:11:31 +01003192 uint l_end = comp_next_label(comp);
3193 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003194 EMIT_ARG(load_id, qstr_arg);
3195 EMIT_ARG(label_assign, l_top);
3196 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003197 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3198 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003199 EMIT_ARG(jump, l_top);
3200 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003201 EMIT(for_iter_end);
3202
3203 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003204 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003205 }
3206 EMIT(return_value);
3207 } else {
3208 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003209 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3210 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3211 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003212
Damien George36db6bc2014-05-07 17:24:22 +01003213 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003214 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003215 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003216 assert(added);
3217 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003218 }
3219
Damien Georgeb9791222014-01-23 00:34:21 +00003220 EMIT_ARG(load_id, MP_QSTR___name__);
3221 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003222 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 +00003223 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003224
3225 check_for_doc_string(comp, pns->nodes[2]);
3226 compile_node(comp, pns->nodes[2]); // 2 is class body
3227
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003228 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003229 assert(id != NULL);
3230 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003231 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003232 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003233#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003234 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003235#else
Damien George2bf7c092014-04-09 15:26:46 +01003236 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003237#endif
Damien429d7192013-10-04 19:53:11 +01003238 }
3239 EMIT(return_value);
3240 }
3241
Damien415eb6f2013-10-05 12:19:06 +01003242 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003243
3244 // make sure we match all the exception levels
3245 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003246}
3247
Damien George094d4502014-04-02 17:31:27 +01003248#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003249// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003250STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003251 comp->pass = pass;
3252 comp->scope_cur = scope;
3253 comp->next_label = 1;
3254
3255 if (scope->kind != SCOPE_FUNCTION) {
3256 printf("Error: inline assembler must be a function\n");
3257 return;
3258 }
3259
Damien George36db6bc2014-05-07 17:24:22 +01003260 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003261 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003262 }
3263
Damien826005c2013-10-05 23:17:28 +01003264 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003265 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3266 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3267 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003268
Damiend99b0522013-12-21 18:17:45 +00003269 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003270
Damiena2f2f7d2013-10-06 00:14:13 +01003271 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003272 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003273 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003274 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003275 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003276 }
3277
Damiend99b0522013-12-21 18:17:45 +00003278 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003279
Damiend99b0522013-12-21 18:17:45 +00003280 mp_parse_node_t pn_body = pns->nodes[3]; // body
3281 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003282 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3283
Damien Georgecbd2f742014-01-19 11:48:48 +00003284 /*
Damien George36db6bc2014-05-07 17:24:22 +01003285 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003286 //printf("----\n");
3287 scope_print_info(scope);
3288 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003289 */
Damien826005c2013-10-05 23:17:28 +01003290
3291 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003292 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3293 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003294 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3295 // no instructions
3296 continue;
3297 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3298 // an instruction; fall through
3299 } else {
3300 // not an instruction; error
3301 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3302 return;
3303 }
Damiend99b0522013-12-21 18:17:45 +00003304 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3305 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3306 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3307 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3308 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3309 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3310 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3311 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3312 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3313 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003314 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3315
3316 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003317 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003318 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003319 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003320 return;
3321 }
Damien George6f355fd2014-04-10 14:11:31 +01003322 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003323 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003324 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003325 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003326 } else if (op == MP_QSTR_align) {
3327 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3328 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3329 return;
3330 }
Damien George36db6bc2014-05-07 17:24:22 +01003331 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003332 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3333 }
3334 } else if (op == MP_QSTR_data) {
3335 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3336 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3337 return;
3338 }
Damien George36db6bc2014-05-07 17:24:22 +01003339 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003340 machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
3341 for (uint i = 1; i < n_args; i++) {
3342 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3343 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3344 return;
3345 }
3346 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3347 }
3348 }
Damien826005c2013-10-05 23:17:28 +01003349 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003350 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003351 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003352 }
3353 }
3354 }
3355
Damien George36db6bc2014-05-07 17:24:22 +01003356 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003357 bool success = EMIT_INLINE_ASM(end_pass);
3358 if (!success) {
3359 comp->had_error = true;
3360 }
Damienb05d7072013-10-05 13:37:10 +01003361 }
Damien429d7192013-10-04 19:53:11 +01003362}
Damien George094d4502014-04-02 17:31:27 +01003363#endif
Damien429d7192013-10-04 19:53:11 +01003364
Damien George1463c1f2014-04-25 23:52:57 +01003365STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003366#if !MICROPY_EMIT_CPYTHON
3367 // in Micro Python we put the *x parameter after all other parameters (except **y)
3368 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3369 id_info_t *id_param = NULL;
3370 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3371 id_info_t *id = &scope->id_info[i];
3372 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3373 if (id_param != NULL) {
3374 // swap star param with last param
3375 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3376 }
3377 break;
3378 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3379 id_param = id;
3380 }
3381 }
3382 }
3383#endif
3384
Damien429d7192013-10-04 19:53:11 +01003385 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003386 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003387 scope->num_locals = 0;
3388 for (int i = 0; i < scope->id_info_len; i++) {
3389 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003390 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003391 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3392 continue;
3393 }
3394 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3395 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3396 }
Damien George2827d622014-04-27 15:50:52 +01003397 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003398 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003399 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003400 }
3401 }
3402
3403 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003404#if MICROPY_EMIT_CPYTHON
3405 int num_cell = 0;
3406#endif
Damien9ecbcff2013-12-11 00:41:43 +00003407 for (int i = 0; i < scope->id_info_len; i++) {
3408 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003409#if MICROPY_EMIT_CPYTHON
3410 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003411 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003412 id->local_num = num_cell;
3413 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003414 }
Damien George6baf76e2013-12-30 22:32:17 +00003415#else
3416 // in Micro Python the cells come right after the fast locals
3417 // parameters are not counted here, since they remain at the start
3418 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003419 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003420 id->local_num = scope->num_locals;
3421 scope->num_locals += 1;
3422 }
3423#endif
Damien9ecbcff2013-12-11 00:41:43 +00003424 }
Damien9ecbcff2013-12-11 00:41:43 +00003425
3426 // compute the index of free vars (freevars[idx] in CPython)
3427 // make sure they are in the order of the parent scope
3428 if (scope->parent != NULL) {
3429 int num_free = 0;
3430 for (int i = 0; i < scope->parent->id_info_len; i++) {
3431 id_info_t *id = &scope->parent->id_info[i];
3432 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3433 for (int j = 0; j < scope->id_info_len; j++) {
3434 id_info_t *id2 = &scope->id_info[j];
3435 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003436 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003437#if MICROPY_EMIT_CPYTHON
3438 // in CPython the frees are numbered after the cells
3439 id2->local_num = num_cell + num_free;
3440#else
3441 // in Micro Python the frees come first, before the params
3442 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003443#endif
3444 num_free += 1;
3445 }
3446 }
3447 }
Damien429d7192013-10-04 19:53:11 +01003448 }
Damien George6baf76e2013-12-30 22:32:17 +00003449#if !MICROPY_EMIT_CPYTHON
3450 // in Micro Python shift all other locals after the free locals
3451 if (num_free > 0) {
3452 for (int i = 0; i < scope->id_info_len; i++) {
3453 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003454 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003455 id->local_num += num_free;
3456 }
3457 }
Damien George2827d622014-04-27 15:50:52 +01003458 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 +00003459 scope->num_locals += num_free;
3460 }
3461#endif
Damien429d7192013-10-04 19:53:11 +01003462 }
3463
Damien George8725f8f2014-02-15 19:33:11 +00003464 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003465
3466#if MICROPY_EMIT_CPYTHON
3467 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003468 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) {
3469 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003470 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003471 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003472 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 +00003473 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003474 }
3475 }
Damien George882b3632014-04-02 15:56:31 +01003476#endif
3477
Damien429d7192013-10-04 19:53:11 +01003478 int num_free = 0;
3479 for (int i = 0; i < scope->id_info_len; i++) {
3480 id_info_t *id = &scope->id_info[i];
3481 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3482 num_free += 1;
3483 }
3484 }
3485 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003486 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003487 }
3488}
3489
Damien George65cad122014-04-06 11:48:15 +01003490mp_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 +01003491 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003492 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003493 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003494
Damien826005c2013-10-05 23:17:28 +01003495 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003496 mp_map_t consts;
3497 mp_map_init(&consts, 0);
3498 pn = fold_constants(comp, pn, &consts);
3499 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003500
3501 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003502 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003503
Damien826005c2013-10-05 23:17:28 +01003504 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003505 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003506 comp->emit_method_table = &emit_pass1_method_table;
3507 comp->emit_inline_asm = NULL;
3508 comp->emit_inline_asm_method_table = NULL;
3509 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003510 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003511 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003512#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003513 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003514 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003515#endif
Damien826005c2013-10-05 23:17:28 +01003516 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003517 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003518 }
3519
3520 // update maximim number of labels needed
3521 if (comp->next_label > max_num_labels) {
3522 max_num_labels = comp->next_label;
3523 }
Damien429d7192013-10-04 19:53:11 +01003524 }
3525
Damien826005c2013-10-05 23:17:28 +01003526 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003527 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003528 compile_scope_compute_things(comp, s);
3529 }
3530
Damien826005c2013-10-05 23:17:28 +01003531 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003532 emit_pass1_free(comp->emit);
3533
Damien826005c2013-10-05 23:17:28 +01003534 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003535#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003536 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003537#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003538 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003539#endif
Damien3ef4abb2013-10-12 16:53:13 +01003540#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003541 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003542#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003543#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003544 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003545 if (false) {
3546 // dummy
3547
Damien3ef4abb2013-10-12 16:53:13 +01003548#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003549 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003550 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003551 if (emit_inline_thumb == NULL) {
3552 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3553 }
3554 comp->emit = NULL;
3555 comp->emit_method_table = NULL;
3556 comp->emit_inline_asm = emit_inline_thumb;
3557 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003558 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea26dc502014-04-12 17:54:52 +01003559 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003560 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003561 }
Damienc025ebb2013-10-12 14:30:21 +01003562#endif
3563
Damien826005c2013-10-05 23:17:28 +01003564 } else {
Damienc025ebb2013-10-12 14:30:21 +01003565
3566 // choose the emit type
3567
Damien3ef4abb2013-10-12 16:53:13 +01003568#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003569 comp->emit = emit_cpython_new(max_num_labels);
3570 comp->emit_method_table = &emit_cpython_method_table;
3571#else
Damien826005c2013-10-05 23:17:28 +01003572 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003573
3574#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003575 case MP_EMIT_OPT_NATIVE_PYTHON:
3576 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003577#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003578 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003579 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003580 }
Damien13ed3a62013-10-08 09:05:10 +01003581 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003582#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003583 if (emit_native == NULL) {
3584 emit_native = emit_native_thumb_new(max_num_labels);
3585 }
3586 comp->emit_method_table = &emit_native_thumb_method_table;
3587#endif
3588 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003589 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien George36db6bc2014-05-07 17:24:22 +01003590
3591 // native emitters need an extra pass to compute stack size
3592 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3593
Damien7af3d192013-10-07 00:02:49 +01003594 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003595#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003596
Damien826005c2013-10-05 23:17:28 +01003597 default:
3598 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003599 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003600 }
3601 comp->emit = emit_bc;
3602 comp->emit_method_table = &emit_bc_method_table;
3603 break;
3604 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003605#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003606
Damien George36db6bc2014-05-07 17:24:22 +01003607 // second last pass: compute code size
Damien Georgea26dc502014-04-12 17:54:52 +01003608 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003609 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3610 }
3611
3612 // final pass: emit code
3613 if (!comp->had_error) {
3614 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003615 }
Damien6cdd3af2013-10-05 18:08:26 +01003616 }
Damien429d7192013-10-04 19:53:11 +01003617 }
3618
Damien George41d02b62014-01-24 22:42:28 +00003619 // free the emitters
3620#if !MICROPY_EMIT_CPYTHON
3621 if (emit_bc != NULL) {
3622 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003623 }
Damien George41d02b62014-01-24 22:42:28 +00003624#if MICROPY_EMIT_NATIVE
3625 if (emit_native != NULL) {
3626#if MICROPY_EMIT_X64
3627 emit_native_x64_free(emit_native);
3628#elif MICROPY_EMIT_THUMB
3629 emit_native_thumb_free(emit_native);
3630#endif
3631 }
3632#endif
3633#if MICROPY_EMIT_INLINE_THUMB
3634 if (emit_inline_thumb != NULL) {
3635 emit_inline_thumb_free(emit_inline_thumb);
3636 }
3637#endif
3638#endif // !MICROPY_EMIT_CPYTHON
3639
3640 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003641 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003642 for (scope_t *s = module_scope; s;) {
3643 scope_t *next = s->next;
3644 scope_free(s);
3645 s = next;
3646 }
Damien5ac1b2e2013-10-18 19:58:12 +01003647
Damien George41d02b62014-01-24 22:42:28 +00003648 // free the compiler
3649 bool had_error = comp->had_error;
3650 m_del_obj(compiler_t, comp);
3651
Damien George1fb03172014-01-03 14:22:03 +00003652 if (had_error) {
3653 // TODO return a proper error message
3654 return mp_const_none;
3655 } else {
3656#if MICROPY_EMIT_CPYTHON
3657 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003658 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003659 return mp_const_true;
3660#else
3661 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003662 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003663#endif
3664 }
Damien429d7192013-10-04 19:53:11 +01003665}