blob: 31ecbf0b91160f236987df6dc885edace299ee9a [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 George6f355fd2014-04-10 14:11:31 +010076 uint break_label;
77 uint 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,
Damien George6d3c5e42014-04-26 10:47:29 +0100114 .used = ARRAY_SIZE(mp_constants_table),
115 .alloc = 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 Georgee337f1e2014-03-31 15:18:37 +01001038#if !MICROPY_EMIT_CPYTHON
Damien George69b89d22014-04-11 13:38:30 +00001039 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1040 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001041 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1042 // we need to do this here before we start building the map for the default keywords
1043 if (comp->num_default_params > 0) {
1044 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001045 } else {
1046 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001047 }
Damien George69b89d22014-04-11 13:38:30 +00001048 // first default dict param, so make the map
1049 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001050 }
Damien George69b89d22014-04-11 13:38:30 +00001051#endif
Damien George968bf342014-04-27 19:12:05 +01001052 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001053 compile_node(comp, pn_equal);
1054#if !MICROPY_EMIT_CPYTHON
1055 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1056 EMIT(store_map);
1057#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001058 } else {
Damien George69b89d22014-04-11 13:38:30 +00001059 comp->num_default_params += 1;
1060 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001061 }
1062 }
1063
1064 // TODO pn_colon not implemented
1065 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001066 }
1067}
1068
1069// leaves function object on stack
1070// returns function name
Damiend99b0522013-12-21 18:17:45 +00001071qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001072 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001073 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001074 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001075 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001076 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001077 }
1078
1079 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001080 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001081 uint old_num_dict_params = comp->num_dict_params;
1082 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001083
1084 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001085 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001086 comp->num_dict_params = 0;
1087 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001088 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001089
1090 if (comp->had_error) {
1091 return MP_QSTR_NULL;
1092 }
1093
Damien Georgee337f1e2014-03-31 15:18:37 +01001094#if !MICROPY_EMIT_CPYTHON
1095 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001096 // the default keywords args may have already made the tuple; if not, do it now
1097 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001098 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001099 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001100 }
1101#endif
1102
Damien429d7192013-10-04 19:53:11 +01001103 // get the scope for this function
1104 scope_t *fscope = (scope_t*)pns->nodes[4];
1105
1106 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001107 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001108
1109 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001110 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001111 comp->num_dict_params = old_num_dict_params;
1112 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001113
1114 // return its name (the 'f' in "def f(...):")
1115 return fscope->simple_name;
1116}
1117
1118// leaves class object on stack
1119// returns class name
Damiend99b0522013-12-21 18:17:45 +00001120qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001121 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001122 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001123 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001124 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001125 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001126 }
1127
1128 EMIT(load_build_class);
1129
1130 // scope for this class
1131 scope_t *cscope = (scope_t*)pns->nodes[3];
1132
1133 // compile the class
1134 close_over_variables_etc(comp, cscope, 0, 0);
1135
1136 // get its name
Damien George968bf342014-04-27 19:12:05 +01001137 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001138
1139 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001140 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1141 mp_parse_node_t parents = pns->nodes[1];
1142 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1143 parents = MP_PARSE_NODE_NULL;
1144 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001145 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001146 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001147
1148 // return its name (the 'C' in class C(...):")
1149 return cscope->simple_name;
1150}
1151
Damien6cdd3af2013-10-05 18:08:26 +01001152// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001153STATIC 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 +00001154 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001155 return false;
1156 }
1157
1158 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001159 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001160 return true;
1161 }
1162
Damiend99b0522013-12-21 18:17:45 +00001163 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001164 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001165 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001166#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001167 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001168 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001169 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001170 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001171#endif
Damien3ef4abb2013-10-12 16:53:13 +01001172#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001173 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001174 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001175#endif
Damien6cdd3af2013-10-05 18:08:26 +01001176 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001177 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001178 }
1179
1180 return true;
1181}
1182
Damiend99b0522013-12-21 18:17:45 +00001183void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001184 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001185 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001186 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1187
Damien6cdd3af2013-10-05 18:08:26 +01001188 // inherit emit options for this function/class definition
1189 uint emit_options = comp->scope_cur->emit_options;
1190
1191 // compile each decorator
1192 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001193 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001194 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1195 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001196
1197 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001198 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001199 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1200
1201 // check for built-in decorators
1202 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1203 // this was a built-in
1204 num_built_in_decorators += 1;
1205
1206 } else {
1207 // not a built-in, compile normally
1208
1209 // compile the decorator function
1210 compile_node(comp, name_nodes[0]);
1211 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001212 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001213 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001214 }
1215
1216 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001217 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001218 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001219 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001220 compile_node(comp, pns_decorator->nodes[1]);
1221 }
Damien429d7192013-10-04 19:53:11 +01001222 }
1223 }
1224
1225 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001226 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001227 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001228 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001229 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001230 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001231 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001232 } else {
1233 // shouldn't happen
1234 assert(0);
1235 }
1236
1237 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001238 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001239 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001240 }
1241
1242 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001243 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001244}
1245
Damiend99b0522013-12-21 18:17:45 +00001246void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001247 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001248 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001249 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001250}
1251
Damiend99b0522013-12-21 18:17:45 +00001252void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1253 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001254 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001255 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1256 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001257
1258 compile_node(comp, pns->nodes[0]); // base of the power node
1259
Damiend99b0522013-12-21 18:17:45 +00001260 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1261 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1262 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1263 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001264 for (int i = 0; i < n - 1; i++) {
1265 compile_node(comp, pns1->nodes[i]);
1266 }
Damiend99b0522013-12-21 18:17:45 +00001267 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1268 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001269 }
Damiend99b0522013-12-21 18:17:45 +00001270 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001271 // can't delete function calls
1272 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001273 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001274 compile_node(comp, pns1->nodes[0]);
1275 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001276 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1277 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001278 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001279 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001280 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001281 }
1282 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001283 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001284 }
1285
Damiend99b0522013-12-21 18:17:45 +00001286 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001287 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001288 }
Damiend99b0522013-12-21 18:17:45 +00001289 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1290 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1291 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1292 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001293 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1294
Damiend99b0522013-12-21 18:17:45 +00001295 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1296 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1297 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001298 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001299 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001300 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001301 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001302 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001303 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001304 c_del_stmt(comp, pns->nodes[0]);
1305 for (int i = 0; i < n; i++) {
1306 c_del_stmt(comp, pns1->nodes[i]);
1307 }
Damiend99b0522013-12-21 18:17:45 +00001308 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001309 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001310 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001311 } else {
1312 // sequence with 2 items
1313 goto sequence_with_2_items;
1314 }
1315 } else {
1316 // sequence with 2 items
1317 sequence_with_2_items:
1318 c_del_stmt(comp, pns->nodes[0]);
1319 c_del_stmt(comp, pns->nodes[1]);
1320 }
1321 } else {
1322 // tuple with 1 element
1323 c_del_stmt(comp, pn);
1324 }
1325 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001326 // TODO is there anything else to implement?
1327 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001328 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001329
1330 return;
1331
1332cannot_delete:
1333 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001334}
1335
Damiend99b0522013-12-21 18:17:45 +00001336void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001337 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1338}
1339
Damiend99b0522013-12-21 18:17:45 +00001340void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001341 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001342 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001343 }
Damien Georgecbddb272014-02-01 20:08:18 +00001344 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001345}
1346
Damiend99b0522013-12-21 18:17:45 +00001347void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001348 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001349 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001350 }
Damien Georgecbddb272014-02-01 20:08:18 +00001351 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001352}
1353
Damiend99b0522013-12-21 18:17:45 +00001354void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001355 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001356 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001357 return;
1358 }
Damiend99b0522013-12-21 18:17:45 +00001359 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001360 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001361 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001362 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001363 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001364 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1365 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 +01001366
Damien George6f355fd2014-04-10 14:11:31 +01001367 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001368 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1369 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1370 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001371 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001372 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1373 } else {
1374 compile_node(comp, pns->nodes[0]);
1375 }
1376 EMIT(return_value);
1377}
1378
Damiend99b0522013-12-21 18:17:45 +00001379void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001380 compile_node(comp, pns->nodes[0]);
1381 EMIT(pop_top);
1382}
1383
Damiend99b0522013-12-21 18:17:45 +00001384void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1385 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001386 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001387 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001388 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001389 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001390 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001391 compile_node(comp, pns->nodes[0]);
1392 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001393 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001394 } else {
1395 // raise x
1396 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001397 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001398 }
1399}
1400
Damien George635543c2014-04-10 12:56:52 +01001401// q_base holds the base of the name
1402// eg a -> q_base=a
1403// a.b.c -> q_base=a
1404void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001405 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001406 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1407 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001408 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001409 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001410 pn = pns->nodes[0];
1411 is_as = true;
1412 }
Damien George635543c2014-04-10 12:56:52 +01001413 if (MP_PARSE_NODE_IS_NULL(pn)) {
1414 // empty name (eg, from . import x)
1415 *q_base = MP_QSTR_;
1416 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1417 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001418 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001419 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001420 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001421 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001422 }
Damien George635543c2014-04-10 12:56:52 +01001423 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001424 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1425 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1426 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001427 // a name of the form a.b.c
1428 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001429 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001430 }
Damiend99b0522013-12-21 18:17:45 +00001431 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001432 int len = n - 1;
1433 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001434 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001435 }
Damien George55baff42014-01-21 21:40:13 +00001436 byte *q_ptr;
1437 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001438 for (int i = 0; i < n; i++) {
1439 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001440 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001441 }
Damien George55baff42014-01-21 21:40:13 +00001442 uint str_src_len;
1443 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001444 memcpy(str_dest, str_src, str_src_len);
1445 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001446 }
Damien George635543c2014-04-10 12:56:52 +01001447 qstr q_full = qstr_build_end(q_ptr);
1448 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001449 if (is_as) {
1450 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001451 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001452 }
1453 }
1454 } else {
Damien George635543c2014-04-10 12:56:52 +01001455 // shouldn't happen
1456 assert(0);
Damien429d7192013-10-04 19:53:11 +01001457 }
1458 } else {
Damien George635543c2014-04-10 12:56:52 +01001459 // shouldn't happen
1460 assert(0);
Damien429d7192013-10-04 19:53:11 +01001461 }
1462}
1463
Damiend99b0522013-12-21 18:17:45 +00001464void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001465 EMIT_ARG(load_const_small_int, 0); // level 0 import
1466 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1467 qstr q_base;
1468 do_import_name(comp, pn, &q_base);
1469 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001470}
1471
Damiend99b0522013-12-21 18:17:45 +00001472void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001473 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1474}
1475
Damiend99b0522013-12-21 18:17:45 +00001476void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001477 mp_parse_node_t pn_import_source = pns->nodes[0];
1478
1479 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1480 uint import_level = 0;
1481 do {
1482 mp_parse_node_t pn_rel;
1483 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)) {
1484 // This covers relative imports with dots only like "from .. import"
1485 pn_rel = pn_import_source;
1486 pn_import_source = MP_PARSE_NODE_NULL;
1487 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1488 // This covers relative imports starting with dot(s) like "from .foo import"
1489 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1490 pn_rel = pns_2b->nodes[0];
1491 pn_import_source = pns_2b->nodes[1];
1492 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1493 } else {
1494 // Not a relative import
1495 break;
1496 }
1497
1498 // get the list of . and/or ...'s
1499 mp_parse_node_t *nodes;
1500 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1501
1502 // count the total number of .'s
1503 for (int i = 0; i < n; i++) {
1504 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1505 import_level++;
1506 } else {
1507 // should be an MP_TOKEN_ELLIPSIS
1508 import_level += 3;
1509 }
1510 }
1511 } while (0);
1512
Damiend99b0522013-12-21 18:17:45 +00001513 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001514 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001515
1516 // build the "fromlist" tuple
1517#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001518 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001519#else
Damien George708c0732014-04-27 19:23:46 +01001520 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001521 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001522#endif
1523
1524 // do the import
Damien George635543c2014-04-10 12:56:52 +01001525 qstr dummy_q;
1526 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001527 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001528
Damien429d7192013-10-04 19:53:11 +01001529 } else {
Damien George635543c2014-04-10 12:56:52 +01001530 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001531
1532 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001533 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001534 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001535#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001536 {
1537 vstr_t *vstr = vstr_new();
1538 vstr_printf(vstr, "(");
1539 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001540 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1541 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1542 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001543 if (i > 0) {
1544 vstr_printf(vstr, ", ");
1545 }
1546 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001547 uint len;
1548 const byte *str = qstr_data(id2, &len);
1549 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001550 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001551 }
Damien02f89412013-12-12 15:13:36 +00001552 if (n == 1) {
1553 vstr_printf(vstr, ",");
1554 }
1555 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001556 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001557 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001558 }
Damiendb4c3612013-12-10 17:27:24 +00001559#else
1560 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001561 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1562 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1563 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001564 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001565 }
Damien Georgeb9791222014-01-23 00:34:21 +00001566 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001567#endif
1568
1569 // do the import
Damien George635543c2014-04-10 12:56:52 +01001570 qstr dummy_q;
1571 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001572 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001573 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1574 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1575 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001576 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001577 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001578 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001579 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001580 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001581 }
1582 }
1583 EMIT(pop_top);
1584 }
1585}
1586
Damiend99b0522013-12-21 18:17:45 +00001587void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001588 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001589 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1590 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001591 } else {
Damiend99b0522013-12-21 18:17:45 +00001592 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1593 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001594 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001595 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001596 }
Damien429d7192013-10-04 19:53:11 +01001597 }
1598 }
1599}
1600
Damiend99b0522013-12-21 18:17:45 +00001601void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001602 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001603 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1604 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001605 } else {
Damiend99b0522013-12-21 18:17:45 +00001606 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1607 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001608 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001609 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001610 }
Damien429d7192013-10-04 19:53:11 +01001611 }
1612 }
1613}
1614
Damiend99b0522013-12-21 18:17:45 +00001615void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001616 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001617 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001618 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 +00001619 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001620 // assertion message
1621 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001622 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001623 }
Damien Georgeb9791222014-01-23 00:34:21 +00001624 EMIT_ARG(raise_varargs, 1);
1625 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001626}
1627
Damiend99b0522013-12-21 18:17:45 +00001628void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001629 // TODO proper and/or short circuiting
1630
Damien George6f355fd2014-04-10 14:11:31 +01001631 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001632
Damien George6f355fd2014-04-10 14:11:31 +01001633 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001634 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1635
1636 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001637
1638 if (
1639#if !MICROPY_EMIT_CPYTHON
1640 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1641 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1642#endif
1643 // optimisation to not jump if last instruction was return
1644 !EMIT(last_emit_was_return_value)
1645 ) {
1646 // jump over elif/else blocks
1647 EMIT_ARG(jump, l_end);
1648 }
1649
Damien Georgeb9791222014-01-23 00:34:21 +00001650 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001651
Damiend99b0522013-12-21 18:17:45 +00001652 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001653 // compile elif blocks
1654
Damiend99b0522013-12-21 18:17:45 +00001655 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001656
Damiend99b0522013-12-21 18:17:45 +00001657 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001658 // multiple elif blocks
1659
Damiend99b0522013-12-21 18:17:45 +00001660 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001661 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001662 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001663 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001664 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1665
1666 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001667 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001668 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001669 }
Damien Georgeb9791222014-01-23 00:34:21 +00001670 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001671 }
1672
1673 } else {
1674 // a single elif block
1675
Damienb05d7072013-10-05 13:37:10 +01001676 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001677 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1678
1679 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001680 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001681 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001682 }
Damien Georgeb9791222014-01-23 00:34:21 +00001683 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001684 }
1685 }
1686
1687 // compile else block
1688 compile_node(comp, pns->nodes[3]); // can be null
1689
Damien Georgeb9791222014-01-23 00:34:21 +00001690 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001691}
1692
Damien Georgecbddb272014-02-01 20:08:18 +00001693#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001694 uint old_break_label = comp->break_label; \
1695 uint old_continue_label = comp->continue_label; \
1696 uint break_label = comp_next_label(comp); \
1697 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001698 comp->break_label = break_label; \
1699 comp->continue_label = continue_label; \
1700 comp->break_continue_except_level = comp->cur_except_level;
1701
1702#define END_BREAK_CONTINUE_BLOCK \
1703 comp->break_label = old_break_label; \
1704 comp->continue_label = old_continue_label; \
1705 comp->break_continue_except_level = comp->cur_except_level;
1706
Damiend99b0522013-12-21 18:17:45 +00001707void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001708 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001709
Damience89a212013-10-15 22:25:17 +01001710 // compared to CPython, we have an optimised version of while loops
1711#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001712 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001713 EMIT_ARG(setup_loop, break_label);
1714 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001715 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1716 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001717 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001718 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001719 }
Damien Georgeb9791222014-01-23 00:34:21 +00001720 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001721 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1722 // this is a small hack to agree with CPython
1723 if (!node_is_const_true(pns->nodes[0])) {
1724 EMIT(pop_block);
1725 }
Damience89a212013-10-15 22:25:17 +01001726#else
Damien George6f355fd2014-04-10 14:11:31 +01001727 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001728 EMIT_ARG(jump, continue_label);
1729 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001730 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001731 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001732 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1733#endif
1734
1735 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001736 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001737
1738 compile_node(comp, pns->nodes[2]); // else
1739
Damien Georgeb9791222014-01-23 00:34:21 +00001740 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001741}
1742
Damienf72fd0e2013-11-06 20:20:49 +00001743// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001744// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1745// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001746void 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 +00001747 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001748
Damien George6f355fd2014-04-10 14:11:31 +01001749 uint top_label = comp_next_label(comp);
1750 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001751
Damien George3ff2d032014-03-31 18:02:22 +01001752 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001753 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001754 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001755
Damien Georgeb9791222014-01-23 00:34:21 +00001756 EMIT_ARG(jump, entry_label);
1757 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001758
Damien George3ff2d032014-03-31 18:02:22 +01001759 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001760 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001761
1762 // store next value to var
1763 c_assign(comp, pn_var, ASSIGN_STORE);
1764
Damienf3822fc2013-11-09 20:12:03 +00001765 // compile body
1766 compile_node(comp, pn_body);
1767
Damien Georgeb9791222014-01-23 00:34:21 +00001768 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001769
Damien George3ff2d032014-03-31 18:02:22 +01001770 // compile: var + step, duplicated on stack
1771 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001772 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001773 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001774 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001775
Damien Georgeb9791222014-01-23 00:34:21 +00001776 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001777
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001778 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001779 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001780 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1781 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001782 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001783 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001784 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001785 }
Damien Georgeb9791222014-01-23 00:34:21 +00001786 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001787
Damien George3ff2d032014-03-31 18:02:22 +01001788 // discard final value of var that failed the loop condition
1789 EMIT(pop_top);
1790
Damienf72fd0e2013-11-06 20:20:49 +00001791 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001792 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001793
1794 compile_node(comp, pn_else);
1795
Damien Georgeb9791222014-01-23 00:34:21 +00001796 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001797}
1798
Damiend99b0522013-12-21 18:17:45 +00001799void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001800#if !MICROPY_EMIT_CPYTHON
1801 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1802 // this is actually slower, but uses no heap memory
1803 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001804 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 +00001805 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001806 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1807 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1808 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1809 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001810 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1811 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001812 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001813 mp_parse_node_t pn_range_start;
1814 mp_parse_node_t pn_range_end;
1815 mp_parse_node_t pn_range_step;
1816 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001817 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001818 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001819 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001820 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001821 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001822 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001823 } else if (n_args == 2) {
1824 pn_range_start = args[0];
1825 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001826 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001827 } else {
1828 pn_range_start = args[0];
1829 pn_range_end = args[1];
1830 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001831 // We need to know sign of step. This is possible only if it's constant
1832 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1833 optimize = false;
1834 }
Damienf72fd0e2013-11-06 20:20:49 +00001835 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001836 }
1837 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001838 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1839 return;
1840 }
1841 }
1842 }
1843#endif
1844
Damien Georgecbddb272014-02-01 20:08:18 +00001845 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001846
Damien George6f355fd2014-04-10 14:11:31 +01001847 uint pop_label = comp_next_label(comp);
1848 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001849
Damience89a212013-10-15 22:25:17 +01001850 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1851#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001852 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001853#endif
1854
Damien429d7192013-10-04 19:53:11 +01001855 compile_node(comp, pns->nodes[1]); // iterator
1856 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001857 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001858 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001859 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1860 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001861 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001862 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001863 }
Damien Georgeb9791222014-01-23 00:34:21 +00001864 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001865 EMIT(for_iter_end);
1866
1867 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001868 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001869
Damience89a212013-10-15 22:25:17 +01001870#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001871 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001872#endif
Damien429d7192013-10-04 19:53:11 +01001873
1874 compile_node(comp, pns->nodes[3]); // else (not tested)
1875
Damien Georgeb9791222014-01-23 00:34:21 +00001876 EMIT_ARG(label_assign, break_label);
1877 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001878}
1879
Damiend99b0522013-12-21 18:17:45 +00001880void 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 +01001881 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001882 uint l1 = comp_next_label(comp);
1883 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001884
Damien Georgeb9791222014-01-23 00:34:21 +00001885 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001886 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001887
Damien429d7192013-10-04 19:53:11 +01001888 compile_node(comp, pn_body); // body
1889 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001890 EMIT_ARG(jump, success_label); // jump over exception handler
1891
1892 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001893 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 +00001894
Damien George6f355fd2014-04-10 14:11:31 +01001895 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001896
1897 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001898 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1899 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001900
1901 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001902 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001903
Damiend99b0522013-12-21 18:17:45 +00001904 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001905 // this is a catch all exception handler
1906 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001907 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001908 return;
1909 }
1910 } else {
1911 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001912 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1913 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1914 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1915 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001916 // handler binds the exception to a local
1917 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001918 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001919 }
1920 }
1921 EMIT(dup_top);
1922 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001923 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001924 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001925 }
1926
1927 EMIT(pop_top);
1928
1929 if (qstr_exception_local == 0) {
1930 EMIT(pop_top);
1931 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001932 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001933 }
1934
1935 EMIT(pop_top);
1936
Damien George6f355fd2014-04-10 14:11:31 +01001937 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001938 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001939 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001940 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001941 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001942 }
1943 compile_node(comp, pns_except->nodes[1]);
1944 if (qstr_exception_local != 0) {
1945 EMIT(pop_block);
1946 }
1947 EMIT(pop_except);
1948 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001949 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1950 EMIT_ARG(label_assign, l3);
1951 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1952 EMIT_ARG(store_id, qstr_exception_local);
1953 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001954
Damien George8dcc0c72014-03-27 10:55:21 +00001955 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001956 EMIT(end_finally);
1957 }
Damien Georgeb9791222014-01-23 00:34:21 +00001958 EMIT_ARG(jump, l2);
1959 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001960 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001961 }
1962
Damien George8dcc0c72014-03-27 10:55:21 +00001963 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001964 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001965 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001966
Damien Georgeb9791222014-01-23 00:34:21 +00001967 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001968 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001969 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001970}
1971
Damiend99b0522013-12-21 18:17:45 +00001972void 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 +01001973 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001974
Damien Georgeb9791222014-01-23 00:34:21 +00001975 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001976 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001977
Damien429d7192013-10-04 19:53:11 +01001978 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001979 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001980 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001981 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001982 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001983 } else {
1984 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1985 }
1986 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001987 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1988 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001989 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001990
Damien George8dcc0c72014-03-27 10:55:21 +00001991 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001992 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001993}
1994
Damiend99b0522013-12-21 18:17:45 +00001995void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1996 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1997 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1998 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001999 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002000 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2001 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002002 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002003 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002004 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002005 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002006 // no finally
2007 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2008 } else {
2009 // have finally
Damiend99b0522013-12-21 18:17:45 +00002010 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 +01002011 }
2012 } else {
2013 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002014 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002015 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002016 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002017 }
2018 } else {
2019 // shouldn't happen
2020 assert(0);
2021 }
2022}
2023
Damiend99b0522013-12-21 18:17:45 +00002024void 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 +01002025 if (n == 0) {
2026 // no more pre-bits, compile the body of the with
2027 compile_node(comp, body);
2028 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002029 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002030 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002031 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002032 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002033 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002034 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002035 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2036 } else {
2037 // this pre-bit is just an expression
2038 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002039 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002040 EMIT(pop_top);
2041 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002042 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002043 // compile additional pre-bits and the body
2044 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2045 // finish this with block
2046 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002047 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2048 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002049 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002050 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002051 EMIT(end_finally);
2052 }
2053}
2054
Damiend99b0522013-12-21 18:17:45 +00002055void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002056 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002057 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002058 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2059 assert(n > 0);
2060
2061 // compile in a nested fashion
2062 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2063}
2064
Damiend99b0522013-12-21 18:17:45 +00002065void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2066 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002067 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2068 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002069 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002070 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002071 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002072 EMIT(pop_top);
2073
Damien429d7192013-10-04 19:53:11 +01002074 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002075 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002076 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2077 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002078 // do nothing with a lonely constant
2079 } else {
2080 compile_node(comp, pns->nodes[0]); // just an expression
2081 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2082 }
Damien429d7192013-10-04 19:53:11 +01002083 }
2084 } else {
Damiend99b0522013-12-21 18:17:45 +00002085 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2086 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002087 if (kind == PN_expr_stmt_augassign) {
2088 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2089 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002090 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002091 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002092 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002093 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2094 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2095 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2096 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2097 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2098 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2099 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2100 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2101 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2102 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2103 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2104 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2105 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002106 }
Damien George7e5fb242014-02-01 22:18:47 +00002107 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002108 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2109 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002110 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2111 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002112 // following CPython, we store left-most first
2113 if (rhs > 0) {
2114 EMIT(dup_top);
2115 }
2116 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2117 for (int i = 0; i < rhs; i++) {
2118 if (i + 1 < rhs) {
2119 EMIT(dup_top);
2120 }
Damiend99b0522013-12-21 18:17:45 +00002121 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002122 }
2123 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002124 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002125 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2126 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2127 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002128 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002129 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2130 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002131 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2132 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2133 // can't optimise when it's a star expression on the lhs
2134 goto no_optimisation;
2135 }
Damien429d7192013-10-04 19:53:11 +01002136 compile_node(comp, pns10->nodes[0]); // rhs
2137 compile_node(comp, pns10->nodes[1]); // rhs
2138 EMIT(rot_two);
2139 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2140 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002141 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2142 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2143 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2144 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002145 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002146 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2147 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002148 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2149 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2150 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2151 // can't optimise when it's a star expression on the lhs
2152 goto no_optimisation;
2153 }
Damien429d7192013-10-04 19:53:11 +01002154 compile_node(comp, pns10->nodes[0]); // rhs
2155 compile_node(comp, pns10->nodes[1]); // rhs
2156 compile_node(comp, pns10->nodes[2]); // rhs
2157 EMIT(rot_three);
2158 EMIT(rot_two);
2159 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2160 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2161 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2162 } else {
Damien George495d7812014-04-08 17:51:47 +01002163 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002164 compile_node(comp, pns1->nodes[0]); // rhs
2165 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2166 }
2167 } else {
2168 // shouldn't happen
2169 assert(0);
2170 }
2171 }
2172}
2173
Damien Georged17926d2014-03-30 13:35:08 +01002174void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002175 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002176 compile_node(comp, pns->nodes[0]);
2177 for (int i = 1; i < num_nodes; i += 1) {
2178 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002179 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002180 }
2181}
2182
Damiend99b0522013-12-21 18:17:45 +00002183void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2184 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2185 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002186
Damien George6f355fd2014-04-10 14:11:31 +01002187 uint l_fail = comp_next_label(comp);
2188 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002189 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2190 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002191 EMIT_ARG(jump, l_end);
2192 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002193 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002194 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002195 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002196}
2197
Damiend99b0522013-12-21 18:17:45 +00002198void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002199 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002200 //mp_parse_node_t pn_params = pns->nodes[0];
2201 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002202
Damien George36db6bc2014-05-07 17:24:22 +01002203 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002204 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002205 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 +01002206 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002207 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002208 }
2209
2210 // get the scope for this lambda
2211 scope_t *this_scope = (scope_t*)pns->nodes[2];
2212
2213 // make the lambda
2214 close_over_variables_etc(comp, this_scope, 0, 0);
2215}
2216
Damiend99b0522013-12-21 18:17:45 +00002217void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002218 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002219 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002220 for (int i = 0; i < n; i += 1) {
2221 compile_node(comp, pns->nodes[i]);
2222 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002223 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002224 }
2225 }
Damien Georgeb9791222014-01-23 00:34:21 +00002226 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002227}
2228
Damiend99b0522013-12-21 18:17:45 +00002229void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002230 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002231 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002232 for (int i = 0; i < n; i += 1) {
2233 compile_node(comp, pns->nodes[i]);
2234 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002235 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002236 }
2237 }
Damien Georgeb9791222014-01-23 00:34:21 +00002238 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002239}
2240
Damiend99b0522013-12-21 18:17:45 +00002241void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002242 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002243 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002244}
2245
Damiend99b0522013-12-21 18:17:45 +00002246void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002247 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002248 compile_node(comp, pns->nodes[0]);
2249 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002250 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002251 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002252 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002253 }
2254 for (int i = 1; i + 1 < num_nodes; i += 2) {
2255 compile_node(comp, pns->nodes[i + 1]);
2256 if (i + 2 < num_nodes) {
2257 EMIT(dup_top);
2258 EMIT(rot_three);
2259 }
Damien George7e5fb242014-02-01 22:18:47 +00002260 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002261 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002262 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002263 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2264 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2265 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2266 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2267 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2268 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2269 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2270 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002271 }
2272 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002273 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2274 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2275 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002276 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002277 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002278 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002279 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002280 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002281 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002282 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002283 }
2284 } else {
2285 // shouldn't happen
2286 assert(0);
2287 }
2288 } else {
2289 // shouldn't happen
2290 assert(0);
2291 }
2292 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002293 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002294 }
2295 }
2296 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002297 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002298 EMIT_ARG(jump, l_end);
2299 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002300 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002301 EMIT(rot_two);
2302 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002303 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002304 }
2305}
2306
Damiend99b0522013-12-21 18:17:45 +00002307void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002308 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002309}
2310
Damiend99b0522013-12-21 18:17:45 +00002311void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002312 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002313}
2314
Damiend99b0522013-12-21 18:17:45 +00002315void compile_xor_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_XOR);
Damien429d7192013-10-04 19:53:11 +01002317}
2318
Damiend99b0522013-12-21 18:17:45 +00002319void compile_and_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_AND);
Damien429d7192013-10-04 19:53:11 +01002321}
2322
Damiend99b0522013-12-21 18:17:45 +00002323void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2324 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002325 compile_node(comp, pns->nodes[0]);
2326 for (int i = 1; i + 1 < num_nodes; i += 2) {
2327 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002328 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002329 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002330 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002331 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002332 } else {
2333 // shouldn't happen
2334 assert(0);
2335 }
2336 }
2337}
2338
Damiend99b0522013-12-21 18:17:45 +00002339void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2340 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002341 compile_node(comp, pns->nodes[0]);
2342 for (int i = 1; i + 1 < num_nodes; i += 2) {
2343 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002344 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002345 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002346 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002347 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002348 } else {
2349 // shouldn't happen
2350 assert(0);
2351 }
2352 }
2353}
2354
Damiend99b0522013-12-21 18:17:45 +00002355void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2356 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002357 compile_node(comp, pns->nodes[0]);
2358 for (int i = 1; i + 1 < num_nodes; i += 2) {
2359 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002360 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002361 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002362 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002363 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002364 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002365 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002366 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002367 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002368 } else {
2369 // shouldn't happen
2370 assert(0);
2371 }
2372 }
2373}
2374
Damiend99b0522013-12-21 18:17:45 +00002375void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002376 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002377 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002378 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002379 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002380 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002381 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002382 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002383 } else {
2384 // shouldn't happen
2385 assert(0);
2386 }
2387}
2388
Damien George35e2a4e2014-02-05 00:51:47 +00002389void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2390 // this is to handle special super() call
2391 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2392
2393 compile_generic_all_nodes(comp, pns);
2394}
2395
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002396STATIC 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 +01002397 // function to call is on top of stack
2398
Damien George35e2a4e2014-02-05 00:51:47 +00002399#if !MICROPY_EMIT_CPYTHON
2400 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002401 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 +00002402 EMIT_ARG(load_id, MP_QSTR___class__);
2403 // get first argument to function
2404 bool found = false;
2405 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002406 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2407 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 +00002408 found = true;
2409 break;
2410 }
2411 }
2412 if (!found) {
2413 printf("TypeError: super() call cannot find self\n");
2414 return;
2415 }
Damien George922ddd62014-04-09 12:43:17 +01002416 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002417 return;
2418 }
2419#endif
2420
Damien George2c0842b2014-04-27 16:46:51 +01002421 // get the list of arguments
2422 mp_parse_node_t *args;
2423 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002424
Damien George2c0842b2014-04-27 16:46:51 +01002425 // compile the arguments
2426 // Rather than calling compile_node on the list, we go through the list of args
2427 // explicitly here so that we can count the number of arguments and give sensible
2428 // error messages.
2429 int n_positional = n_positional_extra;
2430 uint n_keyword = 0;
2431 uint star_flags = 0;
2432 for (int i = 0; i < n_args; i++) {
2433 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2434 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2435 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2436 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2437 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2438 return;
2439 }
2440 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2441 compile_node(comp, pns_arg->nodes[0]);
2442 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2443 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2444 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2445 return;
2446 }
2447 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2448 compile_node(comp, pns_arg->nodes[0]);
2449 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2450 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2451 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2452 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2453 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2454 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2455 return;
2456 }
Damien George968bf342014-04-27 19:12:05 +01002457 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002458 compile_node(comp, pns2->nodes[0]);
2459 n_keyword += 1;
2460 } else {
2461 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2462 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2463 n_positional++;
2464 }
2465 } else {
2466 goto normal_argument;
2467 }
2468 } else {
2469 normal_argument:
2470 if (n_keyword > 0) {
2471 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2472 return;
2473 }
2474 compile_node(comp, args[i]);
2475 n_positional++;
2476 }
Damien429d7192013-10-04 19:53:11 +01002477 }
2478
Damien George2c0842b2014-04-27 16:46:51 +01002479 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002480 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002481 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002482 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002483 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002484 }
Damien429d7192013-10-04 19:53:11 +01002485}
2486
Damiend99b0522013-12-21 18:17:45 +00002487void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2488 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002489 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002490 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 +01002491 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002492 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2493 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002494 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002495 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002496 i += 1;
2497 } else {
2498 compile_node(comp, pns->nodes[i]);
2499 }
Damien George35e2a4e2014-02-05 00:51:47 +00002500 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002501 }
2502}
2503
Damiend99b0522013-12-21 18:17:45 +00002504void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002505 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002506 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002507}
2508
Damiend99b0522013-12-21 18:17:45 +00002509void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002510 // a list of strings
Damien63321742013-12-10 17:41:49 +00002511
2512 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002513 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002514 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002515 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002516 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002517 int pn_kind;
2518 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2519 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2520 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2521 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2522 } else {
2523 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2524 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2525 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2526 pn_kind = MP_PARSE_NODE_STRING;
2527 n_bytes += (machine_uint_t)pns_string->nodes[1];
2528 }
Damien63321742013-12-10 17:41:49 +00002529 if (i == 0) {
2530 string_kind = pn_kind;
2531 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002532 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002533 return;
2534 }
Damien429d7192013-10-04 19:53:11 +01002535 }
Damien63321742013-12-10 17:41:49 +00002536
Damien63321742013-12-10 17:41:49 +00002537 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002538 byte *q_ptr;
2539 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002540 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002541 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2542 uint s_len;
2543 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2544 memcpy(s_dest, s, s_len);
2545 s_dest += s_len;
2546 } else {
2547 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2548 memcpy(s_dest, (const char*)pns_string->nodes[0], (machine_uint_t)pns_string->nodes[1]);
2549 s_dest += (machine_uint_t)pns_string->nodes[1];
2550 }
Damien63321742013-12-10 17:41:49 +00002551 }
Damien George55baff42014-01-21 21:40:13 +00002552 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002553
Damien Georgeb9791222014-01-23 00:34:21 +00002554 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002555}
2556
2557// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002558void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2559 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2560 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2561 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002562
Damien George36db6bc2014-05-07 17:24:22 +01002563 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002564 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002565 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 +01002566 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002567 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002568 }
2569
2570 // get the scope for this comprehension
2571 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2572
2573 // compile the comprehension
2574 close_over_variables_etc(comp, this_scope, 0, 0);
2575
2576 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2577 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002578 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002579}
2580
Damiend99b0522013-12-21 18:17:45 +00002581void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2582 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002583 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002584 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2585 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2586 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2587 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2588 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2589 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2590 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002591 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002592 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002593 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002594 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002595 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002596 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002597 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002598 // generator expression
2599 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2600 } else {
2601 // tuple with 2 items
2602 goto tuple_with_2_items;
2603 }
2604 } else {
2605 // tuple with 2 items
2606 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002607 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002608 }
2609 } else {
2610 // parenthesis around a single item, is just that item
2611 compile_node(comp, pns->nodes[0]);
2612 }
2613}
2614
Damiend99b0522013-12-21 18:17:45 +00002615void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2616 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002617 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002618 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002619 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2620 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2621 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2622 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2623 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002624 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002625 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002626 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002627 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002628 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002629 // list of many items
2630 compile_node(comp, pns2->nodes[0]);
2631 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002632 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002633 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002634 // list comprehension
2635 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2636 } else {
2637 // list with 2 items
2638 goto list_with_2_items;
2639 }
2640 } else {
2641 // list with 2 items
2642 list_with_2_items:
2643 compile_node(comp, pns2->nodes[0]);
2644 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002645 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002646 }
2647 } else {
2648 // list with 1 item
2649 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002650 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002651 }
2652}
2653
Damiend99b0522013-12-21 18:17:45 +00002654void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2655 mp_parse_node_t pn = pns->nodes[0];
2656 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002657 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002658 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002659 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2660 pns = (mp_parse_node_struct_t*)pn;
2661 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002662 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002663 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002664 compile_node(comp, pn);
2665 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002666 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2667 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2668 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2669 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002670 // dict/set with multiple elements
2671
2672 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002673 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002674 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2675
2676 // first element sets whether it's a dict or set
2677 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002678 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002679 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002680 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002681 compile_node(comp, pns->nodes[0]);
2682 EMIT(store_map);
2683 is_dict = true;
2684 } else {
2685 // a set
2686 compile_node(comp, pns->nodes[0]); // 1st value of set
2687 is_dict = false;
2688 }
2689
2690 // process rest of elements
2691 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002692 mp_parse_node_t pn = nodes[i];
2693 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002694 compile_node(comp, pn);
2695 if (is_dict) {
2696 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002697 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002698 return;
2699 }
2700 EMIT(store_map);
2701 } else {
2702 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002703 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002704 return;
2705 }
2706 }
2707 }
2708
2709 // if it's a set, build it
2710 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002711 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002712 }
Damiend99b0522013-12-21 18:17:45 +00002713 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002714 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002715 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002716 // a dictionary comprehension
2717 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2718 } else {
2719 // a set comprehension
2720 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2721 }
2722 } else {
2723 // shouldn't happen
2724 assert(0);
2725 }
2726 } else {
2727 // set with one element
2728 goto set_with_one_element;
2729 }
2730 } else {
2731 // set with one element
2732 set_with_one_element:
2733 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002734 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002735 }
2736}
2737
Damiend99b0522013-12-21 18:17:45 +00002738void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002739 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002740}
2741
Damiend99b0522013-12-21 18:17:45 +00002742void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002743 // object who's index we want is on top of stack
2744 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002745 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002746}
2747
Damiend99b0522013-12-21 18:17:45 +00002748void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002749 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002750 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002751}
2752
Damiend99b0522013-12-21 18:17:45 +00002753void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2754 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2755 mp_parse_node_t pn = pns->nodes[0];
2756 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002757 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002758 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2759 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002760 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2761 pns = (mp_parse_node_struct_t*)pn;
2762 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002763 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002764 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002765 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002766 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002767 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002768 } else {
2769 // [?::x]
2770 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002771 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002772 }
Damiend99b0522013-12-21 18:17:45 +00002773 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002774 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002775 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2776 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2777 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2778 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002779 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002780 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002781 } else {
2782 // [?:x:x]
2783 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002784 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002785 }
2786 } else {
2787 // [?:x]
2788 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002789 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002790 }
2791 } else {
2792 // [?:x]
2793 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002794 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002795 }
2796}
2797
Damiend99b0522013-12-21 18:17:45 +00002798void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002799 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002800 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2801 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002802}
2803
Damiend99b0522013-12-21 18:17:45 +00002804void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002805 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002806 compile_subscript_3_helper(comp, pns);
2807}
2808
Damiend99b0522013-12-21 18:17:45 +00002809void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002810 // if this is called then we are compiling a dict key:value pair
2811 compile_node(comp, pns->nodes[1]); // value
2812 compile_node(comp, pns->nodes[0]); // key
2813}
2814
Damiend99b0522013-12-21 18:17:45 +00002815void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002816 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002817 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002818 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002819}
2820
Damiend99b0522013-12-21 18:17:45 +00002821void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002822 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002823 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002824 return;
2825 }
Damiend99b0522013-12-21 18:17:45 +00002826 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002827 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002828 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002829 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2830 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002831 compile_node(comp, pns->nodes[0]);
2832 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002833 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002834 EMIT(yield_from);
2835 } else {
2836 compile_node(comp, pns->nodes[0]);
2837 EMIT(yield_value);
2838 }
2839}
2840
Damiend99b0522013-12-21 18:17:45 +00002841typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002842STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002843 NULL,
2844#define nc NULL
2845#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002846#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002847#include "grammar.h"
2848#undef nc
2849#undef c
2850#undef DEF_RULE
2851};
2852
Damiend99b0522013-12-21 18:17:45 +00002853void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2854 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002855 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002856 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2857 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2858 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002859 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002860 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002861 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002862 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002863 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2864 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2865 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2866 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002867 case MP_PARSE_NODE_TOKEN:
2868 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002869 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002870 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002871 // do nothing
2872 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002873 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002874 }
2875 break;
Damien429d7192013-10-04 19:53:11 +01002876 default: assert(0);
2877 }
2878 } else {
Damiend99b0522013-12-21 18:17:45 +00002879 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002880 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002881 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
2882 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 +01002883 } else {
Damien George5042bce2014-05-25 22:06:06 +01002884 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2885 if (f == NULL) {
2886 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2887#if MICROPY_DEBUG_PRINTERS
2888 mp_parse_node_print(pn, 0);
2889#endif
2890 compile_syntax_error(comp, pn, "internal compiler error");
2891 } else {
2892 f(comp, pns);
2893 }
Damien429d7192013-10-04 19:53:11 +01002894 }
2895 }
2896}
2897
Damiend99b0522013-12-21 18:17:45 +00002898void 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 +01002899 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002900 qstr param_name = MP_QSTR_NULL;
2901 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002902 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2903 if (MP_PARSE_NODE_IS_ID(pn)) {
2904 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002905 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002906 // comes after a star, so counts as a keyword-only parameter
2907 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002908 } else {
Damien George2827d622014-04-27 15:50:52 +01002909 // comes before a star, so counts as a positional parameter
2910 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002911 }
Damienb14de212013-10-06 00:28:28 +01002912 } else {
Damiend99b0522013-12-21 18:17:45 +00002913 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2914 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2915 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2916 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002917 //int node_index = 1; unused
2918 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002919 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002920 // this parameter has an annotation
2921 pn_annotation = pns->nodes[1];
2922 }
2923 //node_index = 2; unused
2924 }
2925 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002926 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002927 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002928 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002929 comp->scope_cur->num_dict_params += 1;
2930 } else {
2931 comp->scope_cur->num_default_params += 1;
2932 }
2933 }
2934 */
Damien George8b19db02014-04-11 23:25:34 +01002935 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002936 // comes after a star, so counts as a keyword-only parameter
2937 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002938 } else {
Damien George2827d622014-04-27 15:50:52 +01002939 // comes before a star, so counts as a positional parameter
2940 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002941 }
Damiend99b0522013-12-21 18:17:45 +00002942 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002943 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002944 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002945 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002946 // bare star
2947 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002948 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002949 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002950 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002951 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002952 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2953 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002954 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002955 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002956 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2957 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002958 pn_annotation = pns->nodes[1];
2959 } else {
2960 // shouldn't happen
2961 assert(0);
2962 }
Damiend99b0522013-12-21 18:17:45 +00002963 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2964 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002965 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002966 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002967 // this parameter has an annotation
2968 pn_annotation = pns->nodes[1];
2969 }
Damien George8725f8f2014-02-15 19:33:11 +00002970 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002971 } else {
Damienb14de212013-10-06 00:28:28 +01002972 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002973 assert(0);
2974 }
Damien429d7192013-10-04 19:53:11 +01002975 }
2976
Damien George2827d622014-04-27 15:50:52 +01002977 if (param_name != MP_QSTR_NULL) {
Damiend99b0522013-12-21 18:17:45 +00002978 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002979 // TODO this parameter has an annotation
2980 }
2981 bool added;
2982 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2983 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002984 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002985 return;
2986 }
Damien429d7192013-10-04 19:53:11 +01002987 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002988 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002989 }
2990}
2991
Damien George2827d622014-04-27 15:50:52 +01002992STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002993 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2994}
2995
Damien George2827d622014-04-27 15:50:52 +01002996STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002997 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2998}
2999
Damiend99b0522013-12-21 18:17:45 +00003000void 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 +01003001 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003002 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003003 // no more nested if/for; compile inner expression
3004 compile_node(comp, pn_inner_expr);
3005 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003006 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003007 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003008 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003009 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003010 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003011 } else {
3012 EMIT(yield_value);
3013 EMIT(pop_top);
3014 }
Damiend99b0522013-12-21 18:17:45 +00003015 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003016 // if condition
Damiend99b0522013-12-21 18:17:45 +00003017 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003018 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3019 pn_iter = pns_comp_if->nodes[1];
3020 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003021 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003022 // for loop
Damiend99b0522013-12-21 18:17:45 +00003023 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003024 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003025 uint l_end2 = comp_next_label(comp);
3026 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003027 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003028 EMIT_ARG(label_assign, l_top2);
3029 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003030 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3031 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 +00003032 EMIT_ARG(jump, l_top2);
3033 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003034 EMIT(for_iter_end);
3035 } else {
3036 // shouldn't happen
3037 assert(0);
3038 }
3039}
3040
Damien George1463c1f2014-04-25 23:52:57 +01003041STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3042#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003043 // see http://www.python.org/dev/peps/pep-0257/
3044
3045 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003046 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003047 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003048 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003049 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003050 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3051 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003052 for (int i = 0; i < num_nodes; i++) {
3053 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003054 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 +00003055 // not a newline, so this is the first statement; finish search
3056 break;
3057 }
3058 }
3059 // 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 +00003060 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003061 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003062 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003063 } else {
3064 return;
3065 }
3066
3067 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003068 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3069 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003070 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3071 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3072 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3073 // compile the doc string
3074 compile_node(comp, pns->nodes[0]);
3075 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003076 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003077 }
3078 }
Damien George1463c1f2014-04-25 23:52:57 +01003079#endif
Damien429d7192013-10-04 19:53:11 +01003080}
3081
Damien George1463c1f2014-04-25 23:52:57 +01003082STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003083 comp->pass = pass;
3084 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003085 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003086 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003087
Damien George36db6bc2014-05-07 17:24:22 +01003088 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003089 // reset maximum stack sizes in scope
3090 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003091 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003092 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003093 }
3094
Damien5ac1b2e2013-10-18 19:58:12 +01003095#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003096 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003097 scope_print_info(scope);
3098 }
Damien5ac1b2e2013-10-18 19:58:12 +01003099#endif
Damien429d7192013-10-04 19:53:11 +01003100
3101 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003102 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3103 assert(scope->kind == SCOPE_MODULE);
3104 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3105 compile_node(comp, pns->nodes[0]); // compile the expression
3106 EMIT(return_value);
3107 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003108 if (!comp->is_repl) {
3109 check_for_doc_string(comp, scope->pn);
3110 }
Damien429d7192013-10-04 19:53:11 +01003111 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003112 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003113 EMIT(return_value);
3114 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003115 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3116 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3117 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003118
3119 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003120 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003121 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003122 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003123 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3124 }
3125
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003126 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003127
3128 compile_node(comp, pns->nodes[3]); // 3 is function body
3129 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003130 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003131 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003132 EMIT(return_value);
3133 }
3134 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003135 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3136 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3137 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003138
3139 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003140 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003141 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003142 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003143 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3144 }
3145
3146 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003147
3148 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3149 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3150 EMIT(pop_top);
3151 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3152 }
Damien429d7192013-10-04 19:53:11 +01003153 EMIT(return_value);
3154 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3155 // a bit of a hack at the moment
3156
Damiend99b0522013-12-21 18:17:45 +00003157 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3158 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3159 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3160 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3161 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003162
Damien George708c0732014-04-27 19:23:46 +01003163 // We need a unique name for the comprehension argument (the iterator).
3164 // CPython uses .0, but we should be able to use anything that won't
3165 // clash with a user defined variable. Best to use an existing qstr,
3166 // so we use the blank qstr.
3167#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003168 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003169#else
3170 qstr qstr_arg = MP_QSTR_;
3171#endif
Damien George36db6bc2014-05-07 17:24:22 +01003172 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003173 bool added;
3174 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3175 assert(added);
3176 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003177 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003178 }
3179
3180 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003181 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003182 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003183 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003184 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003185 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003186 }
3187
Damien George6f355fd2014-04-10 14:11:31 +01003188 uint l_end = comp_next_label(comp);
3189 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003190 EMIT_ARG(load_id, qstr_arg);
3191 EMIT_ARG(label_assign, l_top);
3192 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003193 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3194 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003195 EMIT_ARG(jump, l_top);
3196 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003197 EMIT(for_iter_end);
3198
3199 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003200 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003201 }
3202 EMIT(return_value);
3203 } else {
3204 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003205 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3206 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3207 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003208
Damien George36db6bc2014-05-07 17:24:22 +01003209 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003210 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003211 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003212 assert(added);
3213 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003214 }
3215
Damien Georgeb9791222014-01-23 00:34:21 +00003216 EMIT_ARG(load_id, MP_QSTR___name__);
3217 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003218 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 +00003219 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003220
3221 check_for_doc_string(comp, pns->nodes[2]);
3222 compile_node(comp, pns->nodes[2]); // 2 is class body
3223
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003224 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003225 assert(id != NULL);
3226 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003227 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003228 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003229#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003230 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003231#else
Damien George2bf7c092014-04-09 15:26:46 +01003232 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003233#endif
Damien429d7192013-10-04 19:53:11 +01003234 }
3235 EMIT(return_value);
3236 }
3237
Damien415eb6f2013-10-05 12:19:06 +01003238 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003239
3240 // make sure we match all the exception levels
3241 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003242}
3243
Damien George094d4502014-04-02 17:31:27 +01003244#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003245// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003246STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003247 comp->pass = pass;
3248 comp->scope_cur = scope;
3249 comp->next_label = 1;
3250
3251 if (scope->kind != SCOPE_FUNCTION) {
3252 printf("Error: inline assembler must be a function\n");
3253 return;
3254 }
3255
Damien George36db6bc2014-05-07 17:24:22 +01003256 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003257 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003258 }
3259
Damien826005c2013-10-05 23:17:28 +01003260 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003261 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3262 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3263 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003264
Damiend99b0522013-12-21 18:17:45 +00003265 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003266
Damiena2f2f7d2013-10-06 00:14:13 +01003267 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003268 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003269 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003270 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003271 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003272 }
3273
Damiend99b0522013-12-21 18:17:45 +00003274 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003275
Damiend99b0522013-12-21 18:17:45 +00003276 mp_parse_node_t pn_body = pns->nodes[3]; // body
3277 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003278 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3279
Damien Georgecbd2f742014-01-19 11:48:48 +00003280 /*
Damien George36db6bc2014-05-07 17:24:22 +01003281 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003282 //printf("----\n");
3283 scope_print_info(scope);
3284 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003285 */
Damien826005c2013-10-05 23:17:28 +01003286
3287 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003288 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3289 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003290 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3291 // no instructions
3292 continue;
3293 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3294 // an instruction; fall through
3295 } else {
3296 // not an instruction; error
3297 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3298 return;
3299 }
Damiend99b0522013-12-21 18:17:45 +00003300 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3301 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3302 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3303 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3304 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3305 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3306 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3307 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3308 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3309 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003310 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3311
3312 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003313 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003314 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003315 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003316 return;
3317 }
Damien George6f355fd2014-04-10 14:11:31 +01003318 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003319 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003320 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003321 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003322 } else if (op == MP_QSTR_align) {
3323 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3324 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3325 return;
3326 }
Damien George36db6bc2014-05-07 17:24:22 +01003327 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003328 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3329 }
3330 } else if (op == MP_QSTR_data) {
3331 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3332 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3333 return;
3334 }
Damien George36db6bc2014-05-07 17:24:22 +01003335 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003336 machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
3337 for (uint i = 1; i < n_args; i++) {
3338 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3339 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3340 return;
3341 }
3342 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3343 }
3344 }
Damien826005c2013-10-05 23:17:28 +01003345 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003346 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003347 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003348 }
3349 }
3350 }
3351
Damien George36db6bc2014-05-07 17:24:22 +01003352 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003353 bool success = EMIT_INLINE_ASM(end_pass);
3354 if (!success) {
3355 comp->had_error = true;
3356 }
Damienb05d7072013-10-05 13:37:10 +01003357 }
Damien429d7192013-10-04 19:53:11 +01003358}
Damien George094d4502014-04-02 17:31:27 +01003359#endif
Damien429d7192013-10-04 19:53:11 +01003360
Damien George1463c1f2014-04-25 23:52:57 +01003361STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003362#if !MICROPY_EMIT_CPYTHON
3363 // in Micro Python we put the *x parameter after all other parameters (except **y)
3364 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3365 id_info_t *id_param = NULL;
3366 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3367 id_info_t *id = &scope->id_info[i];
3368 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3369 if (id_param != NULL) {
3370 // swap star param with last param
3371 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3372 }
3373 break;
3374 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3375 id_param = id;
3376 }
3377 }
3378 }
3379#endif
3380
Damien429d7192013-10-04 19:53:11 +01003381 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003382 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003383 scope->num_locals = 0;
3384 for (int i = 0; i < scope->id_info_len; i++) {
3385 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003386 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003387 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3388 continue;
3389 }
3390 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3391 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3392 }
Damien George2827d622014-04-27 15:50:52 +01003393 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003394 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003395 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003396 }
3397 }
3398
3399 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003400#if MICROPY_EMIT_CPYTHON
3401 int num_cell = 0;
3402#endif
Damien9ecbcff2013-12-11 00:41:43 +00003403 for (int i = 0; i < scope->id_info_len; i++) {
3404 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003405#if MICROPY_EMIT_CPYTHON
3406 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003407 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003408 id->local_num = num_cell;
3409 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003410 }
Damien George6baf76e2013-12-30 22:32:17 +00003411#else
3412 // in Micro Python the cells come right after the fast locals
3413 // parameters are not counted here, since they remain at the start
3414 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003415 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003416 id->local_num = scope->num_locals;
3417 scope->num_locals += 1;
3418 }
3419#endif
Damien9ecbcff2013-12-11 00:41:43 +00003420 }
Damien9ecbcff2013-12-11 00:41:43 +00003421
3422 // compute the index of free vars (freevars[idx] in CPython)
3423 // make sure they are in the order of the parent scope
3424 if (scope->parent != NULL) {
3425 int num_free = 0;
3426 for (int i = 0; i < scope->parent->id_info_len; i++) {
3427 id_info_t *id = &scope->parent->id_info[i];
3428 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3429 for (int j = 0; j < scope->id_info_len; j++) {
3430 id_info_t *id2 = &scope->id_info[j];
3431 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003432 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003433#if MICROPY_EMIT_CPYTHON
3434 // in CPython the frees are numbered after the cells
3435 id2->local_num = num_cell + num_free;
3436#else
3437 // in Micro Python the frees come first, before the params
3438 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003439#endif
3440 num_free += 1;
3441 }
3442 }
3443 }
Damien429d7192013-10-04 19:53:11 +01003444 }
Damien George6baf76e2013-12-30 22:32:17 +00003445#if !MICROPY_EMIT_CPYTHON
3446 // in Micro Python shift all other locals after the free locals
3447 if (num_free > 0) {
3448 for (int i = 0; i < scope->id_info_len; i++) {
3449 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003450 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003451 id->local_num += num_free;
3452 }
3453 }
Damien George2827d622014-04-27 15:50:52 +01003454 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 +00003455 scope->num_locals += num_free;
3456 }
3457#endif
Damien429d7192013-10-04 19:53:11 +01003458 }
3459
Damien George8725f8f2014-02-15 19:33:11 +00003460 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003461
3462#if MICROPY_EMIT_CPYTHON
3463 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003464 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) {
3465 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003466 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003467 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003468 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 +00003469 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003470 }
3471 }
Damien George882b3632014-04-02 15:56:31 +01003472#endif
3473
Damien429d7192013-10-04 19:53:11 +01003474 int num_free = 0;
3475 for (int i = 0; i < scope->id_info_len; i++) {
3476 id_info_t *id = &scope->id_info[i];
3477 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3478 num_free += 1;
3479 }
3480 }
3481 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003482 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003483 }
3484}
3485
Damien George65cad122014-04-06 11:48:15 +01003486mp_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 +01003487 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003488 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003489 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003490
Damien826005c2013-10-05 23:17:28 +01003491 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003492 mp_map_t consts;
3493 mp_map_init(&consts, 0);
3494 pn = fold_constants(comp, pn, &consts);
3495 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003496
3497 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003498 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003499
Damien826005c2013-10-05 23:17:28 +01003500 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003501 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003502 comp->emit_method_table = &emit_pass1_method_table;
3503 comp->emit_inline_asm = NULL;
3504 comp->emit_inline_asm_method_table = NULL;
3505 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003506 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003507 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003508#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003509 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003510 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003511#endif
Damien826005c2013-10-05 23:17:28 +01003512 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003513 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003514 }
3515
3516 // update maximim number of labels needed
3517 if (comp->next_label > max_num_labels) {
3518 max_num_labels = comp->next_label;
3519 }
Damien429d7192013-10-04 19:53:11 +01003520 }
3521
Damien826005c2013-10-05 23:17:28 +01003522 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003523 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003524 compile_scope_compute_things(comp, s);
3525 }
3526
Damien826005c2013-10-05 23:17:28 +01003527 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003528 emit_pass1_free(comp->emit);
3529
Damien826005c2013-10-05 23:17:28 +01003530 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003531#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003532 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003533#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003534 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003535#endif
Damien3ef4abb2013-10-12 16:53:13 +01003536#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003537 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003538#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003539#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003540 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003541 if (false) {
3542 // dummy
3543
Damien3ef4abb2013-10-12 16:53:13 +01003544#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003545 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003546 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003547 if (emit_inline_thumb == NULL) {
3548 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3549 }
3550 comp->emit = NULL;
3551 comp->emit_method_table = NULL;
3552 comp->emit_inline_asm = emit_inline_thumb;
3553 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003554 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea26dc502014-04-12 17:54:52 +01003555 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003556 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003557 }
Damienc025ebb2013-10-12 14:30:21 +01003558#endif
3559
Damien826005c2013-10-05 23:17:28 +01003560 } else {
Damienc025ebb2013-10-12 14:30:21 +01003561
3562 // choose the emit type
3563
Damien3ef4abb2013-10-12 16:53:13 +01003564#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003565 comp->emit = emit_cpython_new(max_num_labels);
3566 comp->emit_method_table = &emit_cpython_method_table;
3567#else
Damien826005c2013-10-05 23:17:28 +01003568 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003569
3570#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003571 case MP_EMIT_OPT_NATIVE_PYTHON:
3572 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003573#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003574 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003575 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003576 }
Damien13ed3a62013-10-08 09:05:10 +01003577 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003578#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003579 if (emit_native == NULL) {
3580 emit_native = emit_native_thumb_new(max_num_labels);
3581 }
3582 comp->emit_method_table = &emit_native_thumb_method_table;
3583#endif
3584 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003585 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien George36db6bc2014-05-07 17:24:22 +01003586
3587 // native emitters need an extra pass to compute stack size
3588 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3589
Damien7af3d192013-10-07 00:02:49 +01003590 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003591#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003592
Damien826005c2013-10-05 23:17:28 +01003593 default:
3594 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003595 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003596 }
3597 comp->emit = emit_bc;
3598 comp->emit_method_table = &emit_bc_method_table;
3599 break;
3600 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003601#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003602
Damien George36db6bc2014-05-07 17:24:22 +01003603 // second last pass: compute code size
Damien Georgea26dc502014-04-12 17:54:52 +01003604 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003605 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3606 }
3607
3608 // final pass: emit code
3609 if (!comp->had_error) {
3610 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003611 }
Damien6cdd3af2013-10-05 18:08:26 +01003612 }
Damien429d7192013-10-04 19:53:11 +01003613 }
3614
Damien George41d02b62014-01-24 22:42:28 +00003615 // free the emitters
3616#if !MICROPY_EMIT_CPYTHON
3617 if (emit_bc != NULL) {
3618 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003619 }
Damien George41d02b62014-01-24 22:42:28 +00003620#if MICROPY_EMIT_NATIVE
3621 if (emit_native != NULL) {
3622#if MICROPY_EMIT_X64
3623 emit_native_x64_free(emit_native);
3624#elif MICROPY_EMIT_THUMB
3625 emit_native_thumb_free(emit_native);
3626#endif
3627 }
3628#endif
3629#if MICROPY_EMIT_INLINE_THUMB
3630 if (emit_inline_thumb != NULL) {
3631 emit_inline_thumb_free(emit_inline_thumb);
3632 }
3633#endif
3634#endif // !MICROPY_EMIT_CPYTHON
3635
3636 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003637 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003638 for (scope_t *s = module_scope; s;) {
3639 scope_t *next = s->next;
3640 scope_free(s);
3641 s = next;
3642 }
Damien5ac1b2e2013-10-18 19:58:12 +01003643
Damien George41d02b62014-01-24 22:42:28 +00003644 // free the compiler
3645 bool had_error = comp->had_error;
3646 m_del_obj(compiler_t, comp);
3647
Damien George1fb03172014-01-03 14:22:03 +00003648 if (had_error) {
3649 // TODO return a proper error message
3650 return mp_const_none;
3651 } else {
3652#if MICROPY_EMIT_CPYTHON
3653 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003654 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003655 return mp_const_true;
3656#else
3657 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003658 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003659#endif
3660 }
Damien429d7192013-10-04 19:53:11 +01003661}