blob: 1f0d90570ef7ff8fbd608a912a5e5a8403e37b6c [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
34#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000035#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010037#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010038#include "parse.h"
Damiend99b0522013-12-21 18:17:45 +000039#include "runtime0.h"
Damien George1fb03172014-01-03 14:22:03 +000040#include "obj.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010041#include "emitglue.h"
42#include "scope.h"
43#include "emit.h"
Damien George1fb03172014-01-03 14:22:03 +000044#include "compile.h"
45#include "runtime.h"
Damien Georgea26dc502014-04-12 17:54:52 +010046#include "builtin.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000047#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010048
49// TODO need to mangle __attr names
50
Damience89a212013-10-15 22:25:17 +010051#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
52
Damien429d7192013-10-04 19:53:11 +010053typedef enum {
54 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000055#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010056#include "grammar.h"
57#undef DEF_RULE
58 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010059 PN_string, // special node for non-interned string
Damien429d7192013-10-04 19:53:11 +010060} pn_kind_t;
61
Damien Georgeb9791222014-01-23 00:34:21 +000062#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
63#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
64#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
65#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010066
67typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000068 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010069 uint8_t is_repl;
70 uint8_t pass; // holds enum type pass_kind_t
71 uint8_t had_error; // try to keep compiler clean from nlr
72 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010073
Damien George6f355fd2014-04-10 14:11:31 +010074 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010075
Damien George25c84642014-05-30 15:20:41 +010076 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
77 uint16_t continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000078 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010079 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010080
Damien George8b19db02014-04-11 23:25:34 +010081 uint8_t have_star;
Damien George69b89d22014-04-11 13:38:30 +000082 uint16_t num_dict_params;
83 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010084
85 scope_t *scope_head;
86 scope_t *scope_cur;
87
Damien6cdd3af2013-10-05 18:08:26 +010088 emit_t *emit; // current emitter
89 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010090
91 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
92 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010093} compiler_t;
94
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010095STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000096 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010097 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
98 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
99 } else {
100 printf(" File \"%s\"\n", qstr_str(comp->source_file));
101 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000102 printf("SyntaxError: %s\n", msg);
103 comp->had_error = true;
104}
105
Damien George57e99eb2014-04-10 22:42:11 +0100106STATIC const mp_map_elem_t mp_constants_table[] = {
107 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100108 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100109};
110
111STATIC const mp_map_t mp_constants_map = {
112 .all_keys_are_qstrs = 1,
113 .table_is_fixed_array = 1,
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
Damien George5b5562c2014-05-31 17:59:11 +01001748 // note that we don't need to pop anything when breaking from an optimise for loop
Damienf72fd0e2013-11-06 20:20:49 +00001749
Damien George6f355fd2014-04-10 14:11:31 +01001750 uint top_label = comp_next_label(comp);
1751 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001752
Damien George3ff2d032014-03-31 18:02:22 +01001753 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001754 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001755 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001756
Damien Georgeb9791222014-01-23 00:34:21 +00001757 EMIT_ARG(jump, entry_label);
1758 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001759
Damien George3ff2d032014-03-31 18:02:22 +01001760 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001761 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001762
1763 // store next value to var
1764 c_assign(comp, pn_var, ASSIGN_STORE);
1765
Damienf3822fc2013-11-09 20:12:03 +00001766 // compile body
1767 compile_node(comp, pn_body);
1768
Damien Georgeb9791222014-01-23 00:34:21 +00001769 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001770
Damien George3ff2d032014-03-31 18:02:22 +01001771 // compile: var + step, duplicated on stack
1772 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001773 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001774 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001775 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001776
Damien Georgeb9791222014-01-23 00:34:21 +00001777 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001778
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001779 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001780 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001781 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1782 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001783 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001784 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001785 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001786 }
Damien Georgeb9791222014-01-23 00:34:21 +00001787 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001788
Damien George3ff2d032014-03-31 18:02:22 +01001789 // discard final value of var that failed the loop condition
1790 EMIT(pop_top);
1791
Damienf72fd0e2013-11-06 20:20:49 +00001792 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001793 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001794
1795 compile_node(comp, pn_else);
1796
Damien Georgeb9791222014-01-23 00:34:21 +00001797 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001798}
1799
Damiend99b0522013-12-21 18:17:45 +00001800void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001801#if !MICROPY_EMIT_CPYTHON
1802 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1803 // this is actually slower, but uses no heap memory
1804 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001805 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 +00001806 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001807 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1808 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1809 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1810 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001811 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1812 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001813 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001814 mp_parse_node_t pn_range_start;
1815 mp_parse_node_t pn_range_end;
1816 mp_parse_node_t pn_range_step;
1817 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001818 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001819 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001820 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001821 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001822 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001823 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001824 } else if (n_args == 2) {
1825 pn_range_start = args[0];
1826 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001827 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001828 } else {
1829 pn_range_start = args[0];
1830 pn_range_end = args[1];
1831 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001832 // We need to know sign of step. This is possible only if it's constant
1833 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1834 optimize = false;
1835 }
Damienf72fd0e2013-11-06 20:20:49 +00001836 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001837 }
1838 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001839 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1840 return;
1841 }
1842 }
1843 }
1844#endif
1845
Damien Georgecbddb272014-02-01 20:08:18 +00001846 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001847 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001848
Damien George6f355fd2014-04-10 14:11:31 +01001849 uint pop_label = comp_next_label(comp);
1850 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001851
Damience89a212013-10-15 22:25:17 +01001852 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1853#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001854 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001855#endif
1856
Damien429d7192013-10-04 19:53:11 +01001857 compile_node(comp, pns->nodes[1]); // iterator
1858 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001859 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001860 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001861 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1862 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001863 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001864 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001865 }
Damien Georgeb9791222014-01-23 00:34:21 +00001866 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001867 EMIT(for_iter_end);
1868
1869 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001870 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001871
Damience89a212013-10-15 22:25:17 +01001872#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001873 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001874#endif
Damien429d7192013-10-04 19:53:11 +01001875
1876 compile_node(comp, pns->nodes[3]); // else (not tested)
1877
Damien Georgeb9791222014-01-23 00:34:21 +00001878 EMIT_ARG(label_assign, break_label);
1879 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001880}
1881
Damiend99b0522013-12-21 18:17:45 +00001882void 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 +01001883 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001884 uint l1 = comp_next_label(comp);
1885 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001886
Damien Georgeb9791222014-01-23 00:34:21 +00001887 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001888 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001889
Damien429d7192013-10-04 19:53:11 +01001890 compile_node(comp, pn_body); // body
1891 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001892 EMIT_ARG(jump, success_label); // jump over exception handler
1893
1894 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001895 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 +00001896
Damien George6f355fd2014-04-10 14:11:31 +01001897 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001898
1899 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001900 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1901 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001902
1903 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001904 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001905
Damiend99b0522013-12-21 18:17:45 +00001906 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001907 // this is a catch all exception handler
1908 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001909 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001910 return;
1911 }
1912 } else {
1913 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001914 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1915 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1916 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1917 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001918 // handler binds the exception to a local
1919 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001920 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001921 }
1922 }
1923 EMIT(dup_top);
1924 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001925 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001926 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001927 }
1928
1929 EMIT(pop_top);
1930
1931 if (qstr_exception_local == 0) {
1932 EMIT(pop_top);
1933 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001934 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001935 }
1936
1937 EMIT(pop_top);
1938
Damien George6f355fd2014-04-10 14:11:31 +01001939 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001940 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001941 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001942 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001943 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001944 }
1945 compile_node(comp, pns_except->nodes[1]);
1946 if (qstr_exception_local != 0) {
1947 EMIT(pop_block);
1948 }
1949 EMIT(pop_except);
1950 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001951 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1952 EMIT_ARG(label_assign, l3);
1953 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1954 EMIT_ARG(store_id, qstr_exception_local);
1955 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001956
Damien George8dcc0c72014-03-27 10:55:21 +00001957 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001958 EMIT(end_finally);
1959 }
Damien Georgeb9791222014-01-23 00:34:21 +00001960 EMIT_ARG(jump, l2);
1961 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001962 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001963 }
1964
Damien George8dcc0c72014-03-27 10:55:21 +00001965 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001966 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001967 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001968
Damien Georgeb9791222014-01-23 00:34:21 +00001969 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001970 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001971 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001972}
1973
Damiend99b0522013-12-21 18:17:45 +00001974void 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 +01001975 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001976
Damien Georgeb9791222014-01-23 00:34:21 +00001977 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001978 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001979
Damien429d7192013-10-04 19:53:11 +01001980 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001981 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001982 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001983 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001984 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001985 } else {
1986 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1987 }
1988 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001989 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1990 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001991 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001992
Damien George8dcc0c72014-03-27 10:55:21 +00001993 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001994 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001995}
1996
Damiend99b0522013-12-21 18:17:45 +00001997void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1998 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1999 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2000 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002001 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002002 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2003 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002004 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002005 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002006 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002007 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002008 // no finally
2009 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2010 } else {
2011 // have finally
Damiend99b0522013-12-21 18:17:45 +00002012 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 +01002013 }
2014 } else {
2015 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002016 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002017 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002018 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002019 }
2020 } else {
2021 // shouldn't happen
2022 assert(0);
2023 }
2024}
2025
Damiend99b0522013-12-21 18:17:45 +00002026void 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 +01002027 if (n == 0) {
2028 // no more pre-bits, compile the body of the with
2029 compile_node(comp, body);
2030 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002031 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002032 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002033 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002034 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002035 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002036 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002037 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2038 } else {
2039 // this pre-bit is just an expression
2040 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002041 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002042 EMIT(pop_top);
2043 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002044 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002045 // compile additional pre-bits and the body
2046 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2047 // finish this with block
2048 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002049 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2050 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002051 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002052 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002053 EMIT(end_finally);
2054 }
2055}
2056
Damiend99b0522013-12-21 18:17:45 +00002057void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002058 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002059 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002060 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2061 assert(n > 0);
2062
2063 // compile in a nested fashion
2064 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2065}
2066
Damiend99b0522013-12-21 18:17:45 +00002067void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2068 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002069 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2070 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002071 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002072 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002073 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002074 EMIT(pop_top);
2075
Damien429d7192013-10-04 19:53:11 +01002076 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002077 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002078 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2079 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002080 // do nothing with a lonely constant
2081 } else {
2082 compile_node(comp, pns->nodes[0]); // just an expression
2083 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2084 }
Damien429d7192013-10-04 19:53:11 +01002085 }
2086 } else {
Damiend99b0522013-12-21 18:17:45 +00002087 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2088 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002089 if (kind == PN_expr_stmt_augassign) {
2090 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2091 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002092 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002093 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002094 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002095 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2096 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2097 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2098 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2099 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2100 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2101 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2102 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2103 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2104 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2105 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2106 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2107 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002108 }
Damien George7e5fb242014-02-01 22:18:47 +00002109 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002110 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2111 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002112 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2113 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002114 // following CPython, we store left-most first
2115 if (rhs > 0) {
2116 EMIT(dup_top);
2117 }
2118 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2119 for (int i = 0; i < rhs; i++) {
2120 if (i + 1 < rhs) {
2121 EMIT(dup_top);
2122 }
Damiend99b0522013-12-21 18:17:45 +00002123 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002124 }
2125 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002126 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002127 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2128 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2129 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002130 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002131 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2132 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002133 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2134 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2135 // can't optimise when it's a star expression on the lhs
2136 goto no_optimisation;
2137 }
Damien429d7192013-10-04 19:53:11 +01002138 compile_node(comp, pns10->nodes[0]); // rhs
2139 compile_node(comp, pns10->nodes[1]); // rhs
2140 EMIT(rot_two);
2141 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2142 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002143 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2144 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2145 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2146 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002147 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002148 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2149 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002150 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2151 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2152 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2153 // can't optimise when it's a star expression on the lhs
2154 goto no_optimisation;
2155 }
Damien429d7192013-10-04 19:53:11 +01002156 compile_node(comp, pns10->nodes[0]); // rhs
2157 compile_node(comp, pns10->nodes[1]); // rhs
2158 compile_node(comp, pns10->nodes[2]); // rhs
2159 EMIT(rot_three);
2160 EMIT(rot_two);
2161 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2162 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2163 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2164 } else {
Damien George495d7812014-04-08 17:51:47 +01002165 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002166 compile_node(comp, pns1->nodes[0]); // rhs
2167 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2168 }
2169 } else {
2170 // shouldn't happen
2171 assert(0);
2172 }
2173 }
2174}
2175
Damien Georged17926d2014-03-30 13:35:08 +01002176void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002177 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002178 compile_node(comp, pns->nodes[0]);
2179 for (int i = 1; i < num_nodes; i += 1) {
2180 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002181 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002182 }
2183}
2184
Damiend99b0522013-12-21 18:17:45 +00002185void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2186 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2187 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002188
Damien George6f355fd2014-04-10 14:11:31 +01002189 uint l_fail = comp_next_label(comp);
2190 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002191 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2192 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002193 EMIT_ARG(jump, l_end);
2194 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002195 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002196 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002197 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002198}
2199
Damiend99b0522013-12-21 18:17:45 +00002200void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002201 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002202 //mp_parse_node_t pn_params = pns->nodes[0];
2203 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002204
Damien George36db6bc2014-05-07 17:24:22 +01002205 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002206 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002207 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 +01002208 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002209 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002210 }
2211
2212 // get the scope for this lambda
2213 scope_t *this_scope = (scope_t*)pns->nodes[2];
2214
2215 // make the lambda
2216 close_over_variables_etc(comp, this_scope, 0, 0);
2217}
2218
Damiend99b0522013-12-21 18:17:45 +00002219void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002220 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002221 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002222 for (int i = 0; i < n; i += 1) {
2223 compile_node(comp, pns->nodes[i]);
2224 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002225 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002226 }
2227 }
Damien Georgeb9791222014-01-23 00:34:21 +00002228 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002229}
2230
Damiend99b0522013-12-21 18:17:45 +00002231void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002232 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002233 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002234 for (int i = 0; i < n; i += 1) {
2235 compile_node(comp, pns->nodes[i]);
2236 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002237 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002238 }
2239 }
Damien Georgeb9791222014-01-23 00:34:21 +00002240 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002241}
2242
Damiend99b0522013-12-21 18:17:45 +00002243void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002244 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002245 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002246}
2247
Damiend99b0522013-12-21 18:17:45 +00002248void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002249 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002250 compile_node(comp, pns->nodes[0]);
2251 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002252 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002253 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002254 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002255 }
2256 for (int i = 1; i + 1 < num_nodes; i += 2) {
2257 compile_node(comp, pns->nodes[i + 1]);
2258 if (i + 2 < num_nodes) {
2259 EMIT(dup_top);
2260 EMIT(rot_three);
2261 }
Damien George7e5fb242014-02-01 22:18:47 +00002262 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002263 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002264 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002265 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2266 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2267 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2268 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2269 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2270 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2271 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2272 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002273 }
2274 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002275 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2276 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2277 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002278 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002279 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002280 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002281 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002282 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002283 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002284 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002285 }
2286 } else {
2287 // shouldn't happen
2288 assert(0);
2289 }
2290 } else {
2291 // shouldn't happen
2292 assert(0);
2293 }
2294 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002295 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002296 }
2297 }
2298 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002299 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002300 EMIT_ARG(jump, l_end);
2301 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002302 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002303 EMIT(rot_two);
2304 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002305 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002306 }
2307}
2308
Damiend99b0522013-12-21 18:17:45 +00002309void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002310 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002311}
2312
Damiend99b0522013-12-21 18:17:45 +00002313void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002314 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002315}
2316
Damiend99b0522013-12-21 18:17:45 +00002317void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002318 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002319}
2320
Damiend99b0522013-12-21 18:17:45 +00002321void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002322 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002323}
2324
Damiend99b0522013-12-21 18:17:45 +00002325void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2326 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002327 compile_node(comp, pns->nodes[0]);
2328 for (int i = 1; i + 1 < num_nodes; i += 2) {
2329 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002330 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002331 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002332 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002333 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002334 } else {
2335 // shouldn't happen
2336 assert(0);
2337 }
2338 }
2339}
2340
Damiend99b0522013-12-21 18:17:45 +00002341void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2342 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002343 compile_node(comp, pns->nodes[0]);
2344 for (int i = 1; i + 1 < num_nodes; i += 2) {
2345 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002346 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002347 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002348 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002349 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002350 } else {
2351 // shouldn't happen
2352 assert(0);
2353 }
2354 }
2355}
2356
Damiend99b0522013-12-21 18:17:45 +00002357void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2358 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002359 compile_node(comp, pns->nodes[0]);
2360 for (int i = 1; i + 1 < num_nodes; i += 2) {
2361 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002362 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002363 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002364 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002365 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002366 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002367 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002368 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002369 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002370 } else {
2371 // shouldn't happen
2372 assert(0);
2373 }
2374 }
2375}
2376
Damiend99b0522013-12-21 18:17:45 +00002377void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002378 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002379 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002380 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002381 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002382 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002383 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002384 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002385 } else {
2386 // shouldn't happen
2387 assert(0);
2388 }
2389}
2390
Damien George35e2a4e2014-02-05 00:51:47 +00002391void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2392 // this is to handle special super() call
2393 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2394
2395 compile_generic_all_nodes(comp, pns);
2396}
2397
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002398STATIC 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 +01002399 // function to call is on top of stack
2400
Damien George35e2a4e2014-02-05 00:51:47 +00002401#if !MICROPY_EMIT_CPYTHON
2402 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002403 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 +00002404 EMIT_ARG(load_id, MP_QSTR___class__);
2405 // get first argument to function
2406 bool found = false;
2407 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002408 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2409 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 +00002410 found = true;
2411 break;
2412 }
2413 }
2414 if (!found) {
2415 printf("TypeError: super() call cannot find self\n");
2416 return;
2417 }
Damien George922ddd62014-04-09 12:43:17 +01002418 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002419 return;
2420 }
2421#endif
2422
Damien George2c0842b2014-04-27 16:46:51 +01002423 // get the list of arguments
2424 mp_parse_node_t *args;
2425 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002426
Damien George2c0842b2014-04-27 16:46:51 +01002427 // compile the arguments
2428 // Rather than calling compile_node on the list, we go through the list of args
2429 // explicitly here so that we can count the number of arguments and give sensible
2430 // error messages.
2431 int n_positional = n_positional_extra;
2432 uint n_keyword = 0;
2433 uint star_flags = 0;
2434 for (int i = 0; i < n_args; i++) {
2435 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2436 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2437 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2438 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2439 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2440 return;
2441 }
2442 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2443 compile_node(comp, pns_arg->nodes[0]);
2444 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2445 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2446 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2447 return;
2448 }
2449 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2450 compile_node(comp, pns_arg->nodes[0]);
2451 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2452 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2453 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2454 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2455 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2456 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2457 return;
2458 }
Damien George968bf342014-04-27 19:12:05 +01002459 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002460 compile_node(comp, pns2->nodes[0]);
2461 n_keyword += 1;
2462 } else {
2463 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2464 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2465 n_positional++;
2466 }
2467 } else {
2468 goto normal_argument;
2469 }
2470 } else {
2471 normal_argument:
2472 if (n_keyword > 0) {
2473 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2474 return;
2475 }
2476 compile_node(comp, args[i]);
2477 n_positional++;
2478 }
Damien429d7192013-10-04 19:53:11 +01002479 }
2480
Damien George2c0842b2014-04-27 16:46:51 +01002481 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002482 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002483 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002484 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002485 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002486 }
Damien429d7192013-10-04 19:53:11 +01002487}
2488
Damiend99b0522013-12-21 18:17:45 +00002489void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2490 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002491 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002492 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 +01002493 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002494 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2495 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002496 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002497 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002498 i += 1;
2499 } else {
2500 compile_node(comp, pns->nodes[i]);
2501 }
Damien George35e2a4e2014-02-05 00:51:47 +00002502 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002503 }
2504}
2505
Damiend99b0522013-12-21 18:17:45 +00002506void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002507 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002508 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002509}
2510
Damiend99b0522013-12-21 18:17:45 +00002511void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002512 // a list of strings
Damien63321742013-12-10 17:41:49 +00002513
2514 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002515 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002516 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002517 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002518 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002519 int pn_kind;
2520 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2521 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2522 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2523 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2524 } else {
2525 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2526 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2527 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2528 pn_kind = MP_PARSE_NODE_STRING;
2529 n_bytes += (machine_uint_t)pns_string->nodes[1];
2530 }
Damien63321742013-12-10 17:41:49 +00002531 if (i == 0) {
2532 string_kind = pn_kind;
2533 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002534 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002535 return;
2536 }
Damien429d7192013-10-04 19:53:11 +01002537 }
Damien63321742013-12-10 17:41:49 +00002538
Damien63321742013-12-10 17:41:49 +00002539 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002540 byte *q_ptr;
2541 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002542 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002543 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2544 uint s_len;
2545 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2546 memcpy(s_dest, s, s_len);
2547 s_dest += s_len;
2548 } else {
2549 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2550 memcpy(s_dest, (const char*)pns_string->nodes[0], (machine_uint_t)pns_string->nodes[1]);
2551 s_dest += (machine_uint_t)pns_string->nodes[1];
2552 }
Damien63321742013-12-10 17:41:49 +00002553 }
Damien George55baff42014-01-21 21:40:13 +00002554 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002555
Damien Georgeb9791222014-01-23 00:34:21 +00002556 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002557}
2558
2559// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002560void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2561 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2562 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2563 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002564
Damien George36db6bc2014-05-07 17:24:22 +01002565 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002566 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002567 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 +01002568 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002569 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002570 }
2571
2572 // get the scope for this comprehension
2573 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2574
2575 // compile the comprehension
2576 close_over_variables_etc(comp, this_scope, 0, 0);
2577
2578 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2579 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002580 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002581}
2582
Damiend99b0522013-12-21 18:17:45 +00002583void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2584 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002585 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002586 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2587 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2588 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2589 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2590 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2591 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2592 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002593 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002594 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002595 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002596 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002597 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002598 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002599 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002600 // generator expression
2601 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2602 } else {
2603 // tuple with 2 items
2604 goto tuple_with_2_items;
2605 }
2606 } else {
2607 // tuple with 2 items
2608 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002609 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002610 }
2611 } else {
2612 // parenthesis around a single item, is just that item
2613 compile_node(comp, pns->nodes[0]);
2614 }
2615}
2616
Damiend99b0522013-12-21 18:17:45 +00002617void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2618 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002619 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002620 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002621 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2622 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2623 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2624 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2625 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002626 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002627 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002628 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002629 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002630 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002631 // list of many items
2632 compile_node(comp, pns2->nodes[0]);
2633 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002634 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002635 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002636 // list comprehension
2637 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2638 } else {
2639 // list with 2 items
2640 goto list_with_2_items;
2641 }
2642 } else {
2643 // list with 2 items
2644 list_with_2_items:
2645 compile_node(comp, pns2->nodes[0]);
2646 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002647 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002648 }
2649 } else {
2650 // list with 1 item
2651 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002652 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002653 }
2654}
2655
Damiend99b0522013-12-21 18:17:45 +00002656void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2657 mp_parse_node_t pn = pns->nodes[0];
2658 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002659 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002660 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002661 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2662 pns = (mp_parse_node_struct_t*)pn;
2663 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002664 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002665 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002666 compile_node(comp, pn);
2667 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002668 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2669 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2670 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2671 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002672 // dict/set with multiple elements
2673
2674 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002675 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002676 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2677
2678 // first element sets whether it's a dict or set
2679 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002680 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002681 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002682 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002683 compile_node(comp, pns->nodes[0]);
2684 EMIT(store_map);
2685 is_dict = true;
2686 } else {
2687 // a set
2688 compile_node(comp, pns->nodes[0]); // 1st value of set
2689 is_dict = false;
2690 }
2691
2692 // process rest of elements
2693 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002694 mp_parse_node_t pn = nodes[i];
2695 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002696 compile_node(comp, pn);
2697 if (is_dict) {
2698 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002699 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002700 return;
2701 }
2702 EMIT(store_map);
2703 } else {
2704 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002705 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002706 return;
2707 }
2708 }
2709 }
2710
2711 // if it's a set, build it
2712 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002713 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002714 }
Damiend99b0522013-12-21 18:17:45 +00002715 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002716 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002717 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002718 // a dictionary comprehension
2719 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2720 } else {
2721 // a set comprehension
2722 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2723 }
2724 } else {
2725 // shouldn't happen
2726 assert(0);
2727 }
2728 } else {
2729 // set with one element
2730 goto set_with_one_element;
2731 }
2732 } else {
2733 // set with one element
2734 set_with_one_element:
2735 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002736 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002737 }
2738}
2739
Damiend99b0522013-12-21 18:17:45 +00002740void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002741 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002742}
2743
Damiend99b0522013-12-21 18:17:45 +00002744void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002745 // object who's index we want is on top of stack
2746 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002747 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002748}
2749
Damiend99b0522013-12-21 18:17:45 +00002750void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002751 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002752 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002753}
2754
Damiend99b0522013-12-21 18:17:45 +00002755void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2756 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2757 mp_parse_node_t pn = pns->nodes[0];
2758 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002759 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002760 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2761 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002762 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2763 pns = (mp_parse_node_struct_t*)pn;
2764 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002765 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002766 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002767 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002768 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002769 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002770 } else {
2771 // [?::x]
2772 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002773 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002774 }
Damiend99b0522013-12-21 18:17:45 +00002775 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002776 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002777 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2778 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2779 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2780 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002781 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002782 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002783 } else {
2784 // [?:x:x]
2785 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002786 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002787 }
2788 } else {
2789 // [?:x]
2790 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002791 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002792 }
2793 } else {
2794 // [?:x]
2795 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002796 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002797 }
2798}
2799
Damiend99b0522013-12-21 18:17:45 +00002800void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002801 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002802 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2803 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002804}
2805
Damiend99b0522013-12-21 18:17:45 +00002806void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002807 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002808 compile_subscript_3_helper(comp, pns);
2809}
2810
Damiend99b0522013-12-21 18:17:45 +00002811void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002812 // if this is called then we are compiling a dict key:value pair
2813 compile_node(comp, pns->nodes[1]); // value
2814 compile_node(comp, pns->nodes[0]); // key
2815}
2816
Damiend99b0522013-12-21 18:17:45 +00002817void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002818 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002819 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002820 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002821}
2822
Damiend99b0522013-12-21 18:17:45 +00002823void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002824 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002825 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002826 return;
2827 }
Damiend99b0522013-12-21 18:17:45 +00002828 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002829 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002830 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002831 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2832 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002833 compile_node(comp, pns->nodes[0]);
2834 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002835 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002836 EMIT(yield_from);
2837 } else {
2838 compile_node(comp, pns->nodes[0]);
2839 EMIT(yield_value);
2840 }
2841}
2842
Damiend99b0522013-12-21 18:17:45 +00002843typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002844STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002845 NULL,
2846#define nc NULL
2847#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002848#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002849#include "grammar.h"
2850#undef nc
2851#undef c
2852#undef DEF_RULE
2853};
2854
Damiend99b0522013-12-21 18:17:45 +00002855void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2856 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002857 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002858 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2859 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2860 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002861 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002862 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002863 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002864 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002865 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2866 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2867 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2868 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002869 case MP_PARSE_NODE_TOKEN:
2870 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002871 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002872 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002873 // do nothing
2874 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002875 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002876 }
2877 break;
Damien429d7192013-10-04 19:53:11 +01002878 default: assert(0);
2879 }
2880 } else {
Damiend99b0522013-12-21 18:17:45 +00002881 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002882 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002883 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
2884 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 +01002885 } else {
Damien George5042bce2014-05-25 22:06:06 +01002886 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2887 if (f == NULL) {
2888 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2889#if MICROPY_DEBUG_PRINTERS
2890 mp_parse_node_print(pn, 0);
2891#endif
2892 compile_syntax_error(comp, pn, "internal compiler error");
2893 } else {
2894 f(comp, pns);
2895 }
Damien429d7192013-10-04 19:53:11 +01002896 }
2897 }
2898}
2899
Damiend99b0522013-12-21 18:17:45 +00002900void 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 +01002901 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002902 qstr param_name = MP_QSTR_NULL;
2903 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002904 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2905 if (MP_PARSE_NODE_IS_ID(pn)) {
2906 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002907 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002908 // comes after a star, so counts as a keyword-only parameter
2909 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002910 } else {
Damien George2827d622014-04-27 15:50:52 +01002911 // comes before a star, so counts as a positional parameter
2912 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002913 }
Damienb14de212013-10-06 00:28:28 +01002914 } else {
Damiend99b0522013-12-21 18:17:45 +00002915 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2916 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2917 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2918 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002919 //int node_index = 1; unused
2920 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002921 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002922 // this parameter has an annotation
2923 pn_annotation = pns->nodes[1];
2924 }
2925 //node_index = 2; unused
2926 }
2927 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002928 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002929 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002930 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002931 comp->scope_cur->num_dict_params += 1;
2932 } else {
2933 comp->scope_cur->num_default_params += 1;
2934 }
2935 }
2936 */
Damien George8b19db02014-04-11 23:25:34 +01002937 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002938 // comes after a star, so counts as a keyword-only parameter
2939 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002940 } else {
Damien George2827d622014-04-27 15:50:52 +01002941 // comes before a star, so counts as a positional parameter
2942 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002943 }
Damiend99b0522013-12-21 18:17:45 +00002944 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002945 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002946 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002947 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002948 // bare star
2949 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002950 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002951 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002952 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002953 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002954 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2955 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002956 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002957 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002958 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2959 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002960 pn_annotation = pns->nodes[1];
2961 } else {
2962 // shouldn't happen
2963 assert(0);
2964 }
Damiend99b0522013-12-21 18:17:45 +00002965 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2966 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002967 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002968 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002969 // this parameter has an annotation
2970 pn_annotation = pns->nodes[1];
2971 }
Damien George8725f8f2014-02-15 19:33:11 +00002972 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002973 } else {
Damienb14de212013-10-06 00:28:28 +01002974 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002975 assert(0);
2976 }
Damien429d7192013-10-04 19:53:11 +01002977 }
2978
Damien George2827d622014-04-27 15:50:52 +01002979 if (param_name != MP_QSTR_NULL) {
Damiend99b0522013-12-21 18:17:45 +00002980 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002981 // TODO this parameter has an annotation
2982 }
2983 bool added;
2984 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2985 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002986 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002987 return;
2988 }
Damien429d7192013-10-04 19:53:11 +01002989 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002990 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002991 }
2992}
2993
Damien George2827d622014-04-27 15:50:52 +01002994STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002995 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2996}
2997
Damien George2827d622014-04-27 15:50:52 +01002998STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002999 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
3000}
3001
Damiend99b0522013-12-21 18:17:45 +00003002void 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 +01003003 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003004 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003005 // no more nested if/for; compile inner expression
3006 compile_node(comp, pn_inner_expr);
3007 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003008 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003009 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003010 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003011 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003012 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003013 } else {
3014 EMIT(yield_value);
3015 EMIT(pop_top);
3016 }
Damiend99b0522013-12-21 18:17:45 +00003017 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003018 // if condition
Damiend99b0522013-12-21 18:17:45 +00003019 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003020 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3021 pn_iter = pns_comp_if->nodes[1];
3022 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003023 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003024 // for loop
Damiend99b0522013-12-21 18:17:45 +00003025 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003026 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003027 uint l_end2 = comp_next_label(comp);
3028 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003029 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003030 EMIT_ARG(label_assign, l_top2);
3031 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003032 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3033 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 +00003034 EMIT_ARG(jump, l_top2);
3035 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003036 EMIT(for_iter_end);
3037 } else {
3038 // shouldn't happen
3039 assert(0);
3040 }
3041}
3042
Damien George1463c1f2014-04-25 23:52:57 +01003043STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3044#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003045 // see http://www.python.org/dev/peps/pep-0257/
3046
3047 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003048 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003049 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003050 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003051 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003052 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3053 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003054 for (int i = 0; i < num_nodes; i++) {
3055 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003056 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 +00003057 // not a newline, so this is the first statement; finish search
3058 break;
3059 }
3060 }
3061 // 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 +00003062 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003063 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003064 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003065 } else {
3066 return;
3067 }
3068
3069 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003070 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3071 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003072 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3073 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3074 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3075 // compile the doc string
3076 compile_node(comp, pns->nodes[0]);
3077 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003078 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003079 }
3080 }
Damien George1463c1f2014-04-25 23:52:57 +01003081#endif
Damien429d7192013-10-04 19:53:11 +01003082}
3083
Damien George1463c1f2014-04-25 23:52:57 +01003084STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003085 comp->pass = pass;
3086 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003087 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003088 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003089
Damien George36db6bc2014-05-07 17:24:22 +01003090 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003091 // reset maximum stack sizes in scope
3092 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003093 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003094 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003095 }
3096
Damien5ac1b2e2013-10-18 19:58:12 +01003097#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003098 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003099 scope_print_info(scope);
3100 }
Damien5ac1b2e2013-10-18 19:58:12 +01003101#endif
Damien429d7192013-10-04 19:53:11 +01003102
3103 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003104 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3105 assert(scope->kind == SCOPE_MODULE);
3106 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3107 compile_node(comp, pns->nodes[0]); // compile the expression
3108 EMIT(return_value);
3109 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003110 if (!comp->is_repl) {
3111 check_for_doc_string(comp, scope->pn);
3112 }
Damien429d7192013-10-04 19:53:11 +01003113 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003114 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003115 EMIT(return_value);
3116 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003117 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3118 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3119 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003120
3121 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003122 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003123 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003124 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003125 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3126 }
3127
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003128 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003129
3130 compile_node(comp, pns->nodes[3]); // 3 is function body
3131 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003132 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003133 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003134 EMIT(return_value);
3135 }
3136 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003137 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3138 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3139 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003140
3141 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003142 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003143 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003144 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003145 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3146 }
3147
3148 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003149
3150 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3151 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3152 EMIT(pop_top);
3153 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3154 }
Damien429d7192013-10-04 19:53:11 +01003155 EMIT(return_value);
3156 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3157 // a bit of a hack at the moment
3158
Damiend99b0522013-12-21 18:17:45 +00003159 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3160 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3161 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3162 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3163 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003164
Damien George708c0732014-04-27 19:23:46 +01003165 // We need a unique name for the comprehension argument (the iterator).
3166 // CPython uses .0, but we should be able to use anything that won't
3167 // clash with a user defined variable. Best to use an existing qstr,
3168 // so we use the blank qstr.
3169#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003170 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003171#else
3172 qstr qstr_arg = MP_QSTR_;
3173#endif
Damien George36db6bc2014-05-07 17:24:22 +01003174 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003175 bool added;
3176 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3177 assert(added);
3178 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003179 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003180 }
3181
3182 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003183 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003184 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003185 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003186 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003187 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003188 }
3189
Damien George6f355fd2014-04-10 14:11:31 +01003190 uint l_end = comp_next_label(comp);
3191 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003192 EMIT_ARG(load_id, qstr_arg);
3193 EMIT_ARG(label_assign, l_top);
3194 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003195 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3196 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003197 EMIT_ARG(jump, l_top);
3198 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003199 EMIT(for_iter_end);
3200
3201 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003202 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003203 }
3204 EMIT(return_value);
3205 } else {
3206 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003207 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3208 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3209 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003210
Damien George36db6bc2014-05-07 17:24:22 +01003211 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003212 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003213 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003214 assert(added);
3215 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003216 }
3217
Damien Georgeb9791222014-01-23 00:34:21 +00003218 EMIT_ARG(load_id, MP_QSTR___name__);
3219 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003220 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 +00003221 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003222
3223 check_for_doc_string(comp, pns->nodes[2]);
3224 compile_node(comp, pns->nodes[2]); // 2 is class body
3225
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003226 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003227 assert(id != NULL);
3228 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003229 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003230 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003231#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003232 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003233#else
Damien George2bf7c092014-04-09 15:26:46 +01003234 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003235#endif
Damien429d7192013-10-04 19:53:11 +01003236 }
3237 EMIT(return_value);
3238 }
3239
Damien415eb6f2013-10-05 12:19:06 +01003240 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003241
3242 // make sure we match all the exception levels
3243 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003244}
3245
Damien George094d4502014-04-02 17:31:27 +01003246#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003247// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003248STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003249 comp->pass = pass;
3250 comp->scope_cur = scope;
3251 comp->next_label = 1;
3252
3253 if (scope->kind != SCOPE_FUNCTION) {
3254 printf("Error: inline assembler must be a function\n");
3255 return;
3256 }
3257
Damien George36db6bc2014-05-07 17:24:22 +01003258 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003259 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003260 }
3261
Damien826005c2013-10-05 23:17:28 +01003262 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003263 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3264 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3265 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003266
Damiend99b0522013-12-21 18:17:45 +00003267 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003268
Damiena2f2f7d2013-10-06 00:14:13 +01003269 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003270 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003271 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003272 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003273 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003274 }
3275
Damiend99b0522013-12-21 18:17:45 +00003276 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003277
Damiend99b0522013-12-21 18:17:45 +00003278 mp_parse_node_t pn_body = pns->nodes[3]; // body
3279 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003280 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3281
Damien Georgecbd2f742014-01-19 11:48:48 +00003282 /*
Damien George36db6bc2014-05-07 17:24:22 +01003283 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003284 //printf("----\n");
3285 scope_print_info(scope);
3286 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003287 */
Damien826005c2013-10-05 23:17:28 +01003288
3289 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003290 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3291 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003292 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3293 // no instructions
3294 continue;
3295 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3296 // an instruction; fall through
3297 } else {
3298 // not an instruction; error
3299 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3300 return;
3301 }
Damiend99b0522013-12-21 18:17:45 +00003302 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3303 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3304 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3305 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3306 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3307 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3308 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3309 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3310 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3311 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003312 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3313
3314 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003315 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003316 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003317 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003318 return;
3319 }
Damien George6f355fd2014-04-10 14:11:31 +01003320 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003321 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003322 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003323 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003324 } else if (op == MP_QSTR_align) {
3325 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3326 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3327 return;
3328 }
Damien George36db6bc2014-05-07 17:24:22 +01003329 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003330 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3331 }
3332 } else if (op == MP_QSTR_data) {
3333 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3334 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3335 return;
3336 }
Damien George36db6bc2014-05-07 17:24:22 +01003337 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003338 machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
3339 for (uint i = 1; i < n_args; i++) {
3340 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3341 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3342 return;
3343 }
3344 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3345 }
3346 }
Damien826005c2013-10-05 23:17:28 +01003347 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003348 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003349 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003350 }
3351 }
3352 }
3353
Damien George36db6bc2014-05-07 17:24:22 +01003354 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003355 bool success = EMIT_INLINE_ASM(end_pass);
3356 if (!success) {
3357 comp->had_error = true;
3358 }
Damienb05d7072013-10-05 13:37:10 +01003359 }
Damien429d7192013-10-04 19:53:11 +01003360}
Damien George094d4502014-04-02 17:31:27 +01003361#endif
Damien429d7192013-10-04 19:53:11 +01003362
Damien George1463c1f2014-04-25 23:52:57 +01003363STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003364#if !MICROPY_EMIT_CPYTHON
3365 // in Micro Python we put the *x parameter after all other parameters (except **y)
3366 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3367 id_info_t *id_param = NULL;
3368 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3369 id_info_t *id = &scope->id_info[i];
3370 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3371 if (id_param != NULL) {
3372 // swap star param with last param
3373 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3374 }
3375 break;
3376 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3377 id_param = id;
3378 }
3379 }
3380 }
3381#endif
3382
Damien429d7192013-10-04 19:53:11 +01003383 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003384 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003385 scope->num_locals = 0;
3386 for (int i = 0; i < scope->id_info_len; i++) {
3387 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003388 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003389 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3390 continue;
3391 }
3392 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3393 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3394 }
Damien George2827d622014-04-27 15:50:52 +01003395 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003396 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003397 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003398 }
3399 }
3400
3401 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003402#if MICROPY_EMIT_CPYTHON
3403 int num_cell = 0;
3404#endif
Damien9ecbcff2013-12-11 00:41:43 +00003405 for (int i = 0; i < scope->id_info_len; i++) {
3406 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003407#if MICROPY_EMIT_CPYTHON
3408 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003409 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003410 id->local_num = num_cell;
3411 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003412 }
Damien George6baf76e2013-12-30 22:32:17 +00003413#else
3414 // in Micro Python the cells come right after the fast locals
3415 // parameters are not counted here, since they remain at the start
3416 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003417 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003418 id->local_num = scope->num_locals;
3419 scope->num_locals += 1;
3420 }
3421#endif
Damien9ecbcff2013-12-11 00:41:43 +00003422 }
Damien9ecbcff2013-12-11 00:41:43 +00003423
3424 // compute the index of free vars (freevars[idx] in CPython)
3425 // make sure they are in the order of the parent scope
3426 if (scope->parent != NULL) {
3427 int num_free = 0;
3428 for (int i = 0; i < scope->parent->id_info_len; i++) {
3429 id_info_t *id = &scope->parent->id_info[i];
3430 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3431 for (int j = 0; j < scope->id_info_len; j++) {
3432 id_info_t *id2 = &scope->id_info[j];
3433 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003434 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003435#if MICROPY_EMIT_CPYTHON
3436 // in CPython the frees are numbered after the cells
3437 id2->local_num = num_cell + num_free;
3438#else
3439 // in Micro Python the frees come first, before the params
3440 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003441#endif
3442 num_free += 1;
3443 }
3444 }
3445 }
Damien429d7192013-10-04 19:53:11 +01003446 }
Damien George6baf76e2013-12-30 22:32:17 +00003447#if !MICROPY_EMIT_CPYTHON
3448 // in Micro Python shift all other locals after the free locals
3449 if (num_free > 0) {
3450 for (int i = 0; i < scope->id_info_len; i++) {
3451 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003452 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003453 id->local_num += num_free;
3454 }
3455 }
Damien George2827d622014-04-27 15:50:52 +01003456 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 +00003457 scope->num_locals += num_free;
3458 }
3459#endif
Damien429d7192013-10-04 19:53:11 +01003460 }
3461
Damien George8725f8f2014-02-15 19:33:11 +00003462 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003463
3464#if MICROPY_EMIT_CPYTHON
3465 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003466 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) {
3467 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003468 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003469 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003470 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 +00003471 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003472 }
3473 }
Damien George882b3632014-04-02 15:56:31 +01003474#endif
3475
Damien429d7192013-10-04 19:53:11 +01003476 int num_free = 0;
3477 for (int i = 0; i < scope->id_info_len; i++) {
3478 id_info_t *id = &scope->id_info[i];
3479 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3480 num_free += 1;
3481 }
3482 }
3483 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003484 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003485 }
3486}
3487
Damien George65cad122014-04-06 11:48:15 +01003488mp_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 +01003489 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003490 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003491 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003492
Damien826005c2013-10-05 23:17:28 +01003493 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003494 mp_map_t consts;
3495 mp_map_init(&consts, 0);
3496 pn = fold_constants(comp, pn, &consts);
3497 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003498
3499 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003500 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003501
Damien826005c2013-10-05 23:17:28 +01003502 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003503 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003504 comp->emit_method_table = &emit_pass1_method_table;
3505 comp->emit_inline_asm = NULL;
3506 comp->emit_inline_asm_method_table = NULL;
3507 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003508 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003509 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003510#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003511 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003512 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003513#endif
Damien826005c2013-10-05 23:17:28 +01003514 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003515 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003516 }
3517
3518 // update maximim number of labels needed
3519 if (comp->next_label > max_num_labels) {
3520 max_num_labels = comp->next_label;
3521 }
Damien429d7192013-10-04 19:53:11 +01003522 }
3523
Damien826005c2013-10-05 23:17:28 +01003524 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003525 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003526 compile_scope_compute_things(comp, s);
3527 }
3528
Damien826005c2013-10-05 23:17:28 +01003529 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003530 emit_pass1_free(comp->emit);
3531
Damien826005c2013-10-05 23:17:28 +01003532 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003533#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003534 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003535#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003536 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003537#endif
Damien3ef4abb2013-10-12 16:53:13 +01003538#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003539 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003540#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003541#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003542 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003543 if (false) {
3544 // dummy
3545
Damien3ef4abb2013-10-12 16:53:13 +01003546#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003547 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003548 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003549 if (emit_inline_thumb == NULL) {
3550 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3551 }
3552 comp->emit = NULL;
3553 comp->emit_method_table = NULL;
3554 comp->emit_inline_asm = emit_inline_thumb;
3555 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003556 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea26dc502014-04-12 17:54:52 +01003557 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003558 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003559 }
Damienc025ebb2013-10-12 14:30:21 +01003560#endif
3561
Damien826005c2013-10-05 23:17:28 +01003562 } else {
Damienc025ebb2013-10-12 14:30:21 +01003563
3564 // choose the emit type
3565
Damien3ef4abb2013-10-12 16:53:13 +01003566#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003567 comp->emit = emit_cpython_new(max_num_labels);
3568 comp->emit_method_table = &emit_cpython_method_table;
3569#else
Damien826005c2013-10-05 23:17:28 +01003570 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003571
3572#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003573 case MP_EMIT_OPT_NATIVE_PYTHON:
3574 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003575#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003576 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003577 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003578 }
Damien13ed3a62013-10-08 09:05:10 +01003579 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003580#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003581 if (emit_native == NULL) {
3582 emit_native = emit_native_thumb_new(max_num_labels);
3583 }
3584 comp->emit_method_table = &emit_native_thumb_method_table;
3585#endif
3586 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003587 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien George36db6bc2014-05-07 17:24:22 +01003588
3589 // native emitters need an extra pass to compute stack size
3590 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3591
Damien7af3d192013-10-07 00:02:49 +01003592 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003593#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003594
Damien826005c2013-10-05 23:17:28 +01003595 default:
3596 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003597 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003598 }
3599 comp->emit = emit_bc;
3600 comp->emit_method_table = &emit_bc_method_table;
3601 break;
3602 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003603#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003604
Damien George36db6bc2014-05-07 17:24:22 +01003605 // second last pass: compute code size
Damien Georgea26dc502014-04-12 17:54:52 +01003606 if (!comp->had_error) {
Damien George36db6bc2014-05-07 17:24:22 +01003607 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3608 }
3609
3610 // final pass: emit code
3611 if (!comp->had_error) {
3612 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003613 }
Damien6cdd3af2013-10-05 18:08:26 +01003614 }
Damien429d7192013-10-04 19:53:11 +01003615 }
3616
Damien George41d02b62014-01-24 22:42:28 +00003617 // free the emitters
3618#if !MICROPY_EMIT_CPYTHON
3619 if (emit_bc != NULL) {
3620 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003621 }
Damien George41d02b62014-01-24 22:42:28 +00003622#if MICROPY_EMIT_NATIVE
3623 if (emit_native != NULL) {
3624#if MICROPY_EMIT_X64
3625 emit_native_x64_free(emit_native);
3626#elif MICROPY_EMIT_THUMB
3627 emit_native_thumb_free(emit_native);
3628#endif
3629 }
3630#endif
3631#if MICROPY_EMIT_INLINE_THUMB
3632 if (emit_inline_thumb != NULL) {
3633 emit_inline_thumb_free(emit_inline_thumb);
3634 }
3635#endif
3636#endif // !MICROPY_EMIT_CPYTHON
3637
3638 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003639 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003640 for (scope_t *s = module_scope; s;) {
3641 scope_t *next = s->next;
3642 scope_free(s);
3643 s = next;
3644 }
Damien5ac1b2e2013-10-18 19:58:12 +01003645
Damien George41d02b62014-01-24 22:42:28 +00003646 // free the compiler
3647 bool had_error = comp->had_error;
3648 m_del_obj(compiler_t, comp);
3649
Damien George1fb03172014-01-03 14:22:03 +00003650 if (had_error) {
3651 // TODO return a proper error message
3652 return mp_const_none;
3653 } else {
3654#if MICROPY_EMIT_CPYTHON
3655 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003656 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003657 return mp_const_true;
3658#else
3659 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003660 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003661#endif
3662 }
Damien429d7192013-10-04 19:53:11 +01003663}