blob: 2fae5c9f64c29b8c92d28a71a0ca8a7dcea1a768 [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 *
Damien George9d5e5c02015-09-24 13:15:57 +01006 * Copyright (c) 2013-2015 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
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>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
Damien George51dfcb42015-01-01 20:27:54 +000036#include "py/runtime.h"
Damien429d7192013-10-04 19:53:11 +010037
Damien Georgedd5353a2015-12-18 12:35:44 +000038#if MICROPY_ENABLE_COMPILER
39
Damien429d7192013-10-04 19:53:11 +010040// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien George65dc9602015-08-14 12:24:11 +010052#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
Damien George41125902015-03-26 16:44:14 +000053
54#if NEED_METHOD_TABLE
55
56// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000057#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
58#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000059#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
60#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien429d7192013-10-04 19:53:11 +010061
Damien George41125902015-03-26 16:44:14 +000062#else
63
64// if we only have the bytecode emitter enabled then we can do a direct call to the functions
65#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
66#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
67#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
68#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
69
70#endif
71
Damien George0496de22015-10-03 17:07:54 +010072#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
73#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
74
Damien Georgea91ac202014-10-05 19:01:34 +010075// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010076typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000077 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010078
Damien George78035b92014-04-09 12:27:39 +010079 uint8_t is_repl;
80 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010081 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010082 uint8_t have_star;
83
84 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +000085 mp_obj_t compile_error; // set to an exception object if there's an error
Damien George831137b2015-12-17 13:13:18 +000086 size_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +010087
Damien George6f355fd2014-04-10 14:11:31 +010088 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010089
Damien George69b89d22014-04-11 13:38:30 +000090 uint16_t num_dict_params;
91 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010092
Damien Georgea91ac202014-10-05 19:01:34 +010093 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
94 uint16_t continue_label;
95 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000096 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010097
Damien429d7192013-10-04 19:53:11 +010098 scope_t *scope_head;
99 scope_t *scope_cur;
100
Damien6cdd3af2013-10-05 18:08:26 +0100101 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000102 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100103 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000104 #endif
Damien826005c2013-10-05 23:17:28 +0100105
Damien Georgea77ffe62015-03-14 12:59:31 +0000106 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +0100107 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
108 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien Georgea77ffe62015-03-14 12:59:31 +0000109 #endif
Damien429d7192013-10-04 19:53:11 +0100110} compiler_t;
111
Damien Georgecfc4c332015-07-29 22:16:01 +0000112STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
113 // if the line of the error is unknown then try to update it from the pn
114 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George831137b2015-12-17 13:13:18 +0000115 comp->compile_error_line = ((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100116 }
Damien George84d59c22015-07-27 22:20:00 +0100117}
118
119STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000120 // only register the error if there has been no other error
121 if (comp->compile_error == MP_OBJ_NULL) {
122 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
123 compile_error_set_line(comp, pn);
124 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000125}
126
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200127STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George6be0b0a2014-08-15 14:30:52 +0100128STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000129STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100130
Damien George6f355fd2014-04-10 14:11:31 +0100131STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100132 return comp->next_label++;
133}
134
Damien George8dcc0c72014-03-27 10:55:21 +0000135STATIC void compile_increase_except_level(compiler_t *comp) {
136 comp->cur_except_level += 1;
137 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
138 comp->scope_cur->exc_stack_size = comp->cur_except_level;
139 }
140}
141
142STATIC void compile_decrease_except_level(compiler_t *comp) {
143 assert(comp->cur_except_level > 0);
144 comp->cur_except_level -= 1;
145}
146
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200147STATIC 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 +0100148 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100149 scope->parent = comp->scope_cur;
150 scope->next = NULL;
151 if (comp->scope_head == NULL) {
152 comp->scope_head = scope;
153 } else {
154 scope_t *s = comp->scope_head;
155 while (s->next != NULL) {
156 s = s->next;
157 }
158 s->next = scope;
159 }
160 return scope;
161}
162
Damien George91bc32d2015-04-09 15:31:53 +0000163typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
164
165STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, apply_list_fun_t f) {
Damien George963a5a32015-01-16 17:47:07 +0000166 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000167 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
168 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100169 for (int i = 0; i < num_nodes; i++) {
170 f(comp, pns->nodes[i]);
171 }
Damiend99b0522013-12-21 18:17:45 +0000172 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100173 f(comp, pn);
174 }
175}
176
Damien George969a6b32014-12-10 22:07:04 +0000177STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000178 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100179 for (int i = 0; i < num_nodes; i++) {
180 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000181 if (comp->compile_error != MP_OBJ_NULL) {
182 // add line info for the error in case it didn't have a line number
183 compile_error_set_line(comp, pns->nodes[i]);
184 return;
185 }
Damien429d7192013-10-04 19:53:11 +0100186 }
187}
188
Damien George542bd6b2015-03-26 14:42:40 +0000189STATIC void compile_load_id(compiler_t *comp, qstr qst) {
190 if (comp->pass == MP_PASS_SCOPE) {
191 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
192 } else {
Damien George41125902015-03-26 16:44:14 +0000193 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000194 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000195 #else
196 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
197 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000198 }
199}
200
201STATIC void compile_store_id(compiler_t *comp, qstr qst) {
202 if (comp->pass == MP_PASS_SCOPE) {
203 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
204 } else {
Damien George41125902015-03-26 16:44:14 +0000205 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000206 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000207 #else
208 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
209 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000210 }
211}
212
213STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
214 if (comp->pass == MP_PASS_SCOPE) {
215 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
216 } else {
Damien George41125902015-03-26 16:44:14 +0000217 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000218 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000219 #else
220 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
221 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000222 }
223}
224
Damien George2c0842b2014-04-27 16:46:51 +0100225STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100226 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000227 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100228 compile_node(comp, pn);
229 total += 1;
230 }
231 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000232 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100233 for (int i = 0; i < n; i++) {
234 compile_node(comp, pns_list->nodes[i]);
235 }
236 total += n;
237 }
Damien Georgeb9791222014-01-23 00:34:21 +0000238 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100239}
Damien429d7192013-10-04 19:53:11 +0100240
Damien George969a6b32014-12-10 22:07:04 +0000241STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100242 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000243 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100244}
245
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200246STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000247 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
248 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100249}
250
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200251STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000252 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
253 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100254}
255
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200256STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3a205172013-10-12 15:01:56 +0100257 if (node_is_const_false(pn)) {
258 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000259 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100260 }
261 return;
262 } else if (node_is_const_true(pn)) {
263 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000264 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100265 }
266 return;
Damiend99b0522013-12-21 18:17:45 +0000267 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
268 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
269 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
270 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100271 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000272 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100273 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100274 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000275 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100276 }
Damien George0b2fd912015-02-28 14:37:54 +0000277 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000278 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100279 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000280 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100281 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000282 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100283 }
284 }
285 return;
Damiend99b0522013-12-21 18:17:45 +0000286 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100287 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000288 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100289 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000290 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100291 }
Damiend99b0522013-12-21 18:17:45 +0000292 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100293 c_if_cond(comp, pns->nodes[0], !jump_if, label);
294 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100295 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
296 // cond is something in parenthesis
297 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
298 // empty tuple, acts as false for the condition
299 if (jump_if == false) {
300 EMIT_ARG(jump, label);
301 }
Damien George93b37262016-01-07 13:07:52 +0000302 } else {
303 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100304 // non-empty tuple, acts as true for the condition
305 if (jump_if == true) {
306 EMIT_ARG(jump, label);
307 }
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100308 }
309 return;
Damien3a205172013-10-12 15:01:56 +0100310 }
311 }
312
313 // nothing special, fall back to default compiling for node and jump
314 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000315 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100316}
317
318typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100319STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100320
pohmelie81ebba72016-01-27 23:23:11 +0300321STATIC void c_assign_atom_expr(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100322 if (assign_kind != ASSIGN_AUG_STORE) {
323 compile_node(comp, pns->nodes[0]);
324 }
325
Damiend99b0522013-12-21 18:17:45 +0000326 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
327 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300328 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000329 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100330 if (assign_kind != ASSIGN_AUG_STORE) {
331 for (int i = 0; i < n - 1; i++) {
332 compile_node(comp, pns1->nodes[i]);
333 }
334 }
Damiend99b0522013-12-21 18:17:45 +0000335 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
336 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100337 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000338 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100339 if (assign_kind == ASSIGN_AUG_STORE) {
340 EMIT(rot_three);
341 EMIT(store_subscr);
342 } else {
343 compile_node(comp, pns1->nodes[0]);
344 if (assign_kind == ASSIGN_AUG_LOAD) {
345 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100346 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100347 } else {
348 EMIT(store_subscr);
349 }
350 }
Damiend99b0522013-12-21 18:17:45 +0000351 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
352 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100353 if (assign_kind == ASSIGN_AUG_LOAD) {
354 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000355 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100356 } else {
357 if (assign_kind == ASSIGN_AUG_STORE) {
358 EMIT(rot_two);
359 }
Damien Georgeb9791222014-01-23 00:34:21 +0000360 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100361 }
362 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100363 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100364 }
365 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100366 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100367 }
368
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100369 return;
370
371cannot_assign:
372 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100373}
374
Damien George0288cf02014-04-11 11:53:00 +0000375// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
Damien George6be0b0a2014-08-15 14:30:52 +0100376STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
Damien George0288cf02014-04-11 11:53:00 +0000377 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
378
379 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000380 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000381 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
382 EMIT_ARG(unpack_ex, 0, num_tail);
383 have_star_index = 0;
384 }
Damien George963a5a32015-01-16 17:47:07 +0000385 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000386 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000387 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000388 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
389 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100390 } else {
Damien George9d181f62014-04-27 16:55:27 +0100391 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100392 return;
393 }
394 }
395 }
Damien George963a5a32015-01-16 17:47:07 +0000396 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000397 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100398 }
Damien George0288cf02014-04-11 11:53:00 +0000399 if (num_head != 0) {
400 if (0 == have_star_index) {
401 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100402 } else {
Damien George0288cf02014-04-11 11:53:00 +0000403 c_assign(comp, node_head, ASSIGN_STORE);
404 }
405 }
Damien George963a5a32015-01-16 17:47:07 +0000406 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000407 if (num_head + i == have_star_index) {
408 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
409 } else {
410 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100411 }
412 }
413}
414
415// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100416STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000417 assert(!MP_PARSE_NODE_IS_NULL(pn));
418 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000419 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000420 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100421 switch (assign_kind) {
422 case ASSIGN_STORE:
423 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000424 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100425 break;
426 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000427 default:
Damien George542bd6b2015-03-26 14:42:40 +0000428 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100429 break;
430 }
431 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100432 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100433 return;
434 }
435 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000436 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000437 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
438 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
pohmelie81ebba72016-01-27 23:23:11 +0300439 case PN_atom_expr_normal:
Damien429d7192013-10-04 19:53:11 +0100440 // lhs is an index or attribute
pohmelie81ebba72016-01-27 23:23:11 +0300441 c_assign_atom_expr(comp, pns, assign_kind);
Damien429d7192013-10-04 19:53:11 +0100442 break;
443
444 case PN_testlist_star_expr:
445 case PN_exprlist:
446 // lhs is a tuple
447 if (assign_kind != ASSIGN_STORE) {
448 goto bad_aug;
449 }
Damien George0288cf02014-04-11 11:53:00 +0000450 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100451 break;
452
453 case PN_atom_paren:
454 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000455 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100456 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100457 goto cannot_assign;
Damien George93b37262016-01-07 13:07:52 +0000458 } else {
459 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien George44f65c02015-03-25 23:06:48 +0000460 if (assign_kind != ASSIGN_STORE) {
461 goto bad_aug;
462 }
Damiend99b0522013-12-21 18:17:45 +0000463 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100464 goto testlist_comp;
Damien429d7192013-10-04 19:53:11 +0100465 }
466 break;
467
468 case PN_atom_bracket:
469 // lhs is something in brackets
470 if (assign_kind != ASSIGN_STORE) {
471 goto bad_aug;
472 }
Damiend99b0522013-12-21 18:17:45 +0000473 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100474 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000475 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000476 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
477 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100478 goto testlist_comp;
479 } else {
480 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000481 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100482 }
483 break;
484
485 default:
Damien George9d181f62014-04-27 16:55:27 +0100486 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100487 }
488 return;
489
490 testlist_comp:
491 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000492 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
493 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
494 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100495 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000496 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000497 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000498 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100499 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000500 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
501 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000502 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100503 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100504 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100505 } else {
506 // sequence with 2 items
507 goto sequence_with_2_items;
508 }
509 } else {
510 // sequence with 2 items
511 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000512 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100513 }
514 return;
515 }
516 return;
517
Damien George9d181f62014-04-27 16:55:27 +0100518 cannot_assign:
519 compile_syntax_error(comp, pn, "can't assign to expression");
520 return;
521
Damien429d7192013-10-04 19:53:11 +0100522 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100523 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100524}
525
Damien George65dc9602015-08-14 12:24:11 +0100526// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100527// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
528// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
529// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100530STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
Damien George30565092014-03-31 11:30:17 +0100531 assert(n_pos_defaults >= 0);
532 assert(n_kw_defaults >= 0);
533
Damien George3a3db4d2015-10-22 23:45:37 +0100534 // set flags
535 if (n_kw_defaults > 0) {
536 this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
537 }
538 this_scope->num_def_pos_args = n_pos_defaults;
539
Damien429d7192013-10-04 19:53:11 +0100540 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000541 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100542 int nfree = 0;
543 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000544 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
545 id_info_t *id = &comp->scope_cur->id_info[i];
546 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
547 for (int j = 0; j < this_scope->id_info_len; j++) {
548 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100549 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000550 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000551 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000552 nfree += 1;
553 }
554 }
Damien429d7192013-10-04 19:53:11 +0100555 }
556 }
557 }
Damien429d7192013-10-04 19:53:11 +0100558
559 // make the function/closure
560 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100561 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100562 } else {
Damien George3558f622014-04-20 17:50:40 +0100563 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100564 }
565}
566
Damien George2c838942015-11-17 14:00:14 +0000567STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
568 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)
569 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100570 comp->have_star = true;
571 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000572 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
573 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100574 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100575 } else {
576 // named star
Damien429d7192013-10-04 19:53:11 +0100577 }
Damien George8b19db02014-04-11 23:25:34 +0100578 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000579
Damien George2c838942015-11-17 14:00:14 +0000580 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)
581 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100582 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000583 // TODO do we need to do anything with this?
584
585 } else {
586 mp_parse_node_t pn_id;
587 mp_parse_node_t pn_colon;
588 mp_parse_node_t pn_equal;
589 if (MP_PARSE_NODE_IS_ID(pn)) {
590 // this parameter is just an id
591
592 pn_id = pn;
593 pn_colon = MP_PARSE_NODE_NULL;
594 pn_equal = MP_PARSE_NODE_NULL;
595
Damien George2c838942015-11-17 14:00:14 +0000596 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000597 // this parameter has a colon and/or equal specifier
598
599 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
600 pn_id = pns->nodes[0];
601 pn_colon = pns->nodes[1];
602 pn_equal = pns->nodes[2];
Damien George2c838942015-11-17 14:00:14 +0000603
604 } else {
605 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be
606 // this parameter has an equal specifier
607
608 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
609 pn_id = pns->nodes[0];
610 pn_equal = pns->nodes[1];
Damien Georgef41fdd02014-03-03 23:19:11 +0000611 }
612
613 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
614 // this parameter does not have a default value
615
616 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100617 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100618 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000619 return;
620 }
621
622 } else {
623 // this parameter has a default value
624 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
625
Damien George8b19db02014-04-11 23:25:34 +0100626 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000627 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000628 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
629 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100630 // in Micro Python we put the default positional parameters into a tuple using the bytecode
631 // we need to do this here before we start building the map for the default keywords
632 if (comp->num_default_params > 0) {
633 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100634 } else {
635 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100636 }
Damien George69b89d22014-04-11 13:38:30 +0000637 // first default dict param, so make the map
638 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000639 }
Damien Georgef0778a72014-06-07 22:01:00 +0100640
641 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000642 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000643 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000644 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000645 } else {
Damien George69b89d22014-04-11 13:38:30 +0000646 comp->num_default_params += 1;
647 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000648 }
649 }
650
651 // TODO pn_colon not implemented
652 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100653 }
654}
655
Damien George2c838942015-11-17 14:00:14 +0000656STATIC void compile_funcdef_lambdef(compiler_t *comp, scope_t *scope, mp_parse_node_t pn_params, pn_kind_t pn_list_kind) {
Damien George29e9db02015-12-12 13:42:51 +0000657 // When we call compile_funcdef_lambdef_param below it can compile an arbitrary
658 // expression for default arguments, which may contain a lambda. The lambda will
659 // call here in a nested way, so we must save and restore the relevant state.
660 bool orig_have_star = comp->have_star;
661 uint16_t orig_num_dict_params = comp->num_dict_params;
662 uint16_t orig_num_default_params = comp->num_default_params;
663
Damien George2c838942015-11-17 14:00:14 +0000664 // compile default parameters
665 comp->have_star = false;
666 comp->num_dict_params = 0;
667 comp->num_default_params = 0;
668 apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
669
670 if (comp->compile_error != MP_OBJ_NULL) {
671 return;
672 }
673
674 // in Micro Python we put the default positional parameters into a tuple using the bytecode
675 // the default keywords args may have already made the tuple; if not, do it now
676 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
677 EMIT_ARG(build_tuple, comp->num_default_params);
678 EMIT(load_null); // sentinel indicating empty default keyword args
679 }
680
681 // make the function
682 close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
Damien George29e9db02015-12-12 13:42:51 +0000683
684 // restore state
685 comp->have_star = orig_have_star;
686 comp->num_dict_params = orig_num_dict_params;
687 comp->num_default_params = orig_num_default_params;
Damien George2c838942015-11-17 14:00:14 +0000688}
689
Damien429d7192013-10-04 19:53:11 +0100690// leaves function object on stack
691// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000692STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100693 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100694 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000695 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100696 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000697 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100698 }
699
Damien429d7192013-10-04 19:53:11 +0100700 // get the scope for this function
701 scope_t *fscope = (scope_t*)pns->nodes[4];
702
Damien George2c838942015-11-17 14:00:14 +0000703 // compile the function definition
704 compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
Damien429d7192013-10-04 19:53:11 +0100705
Damien429d7192013-10-04 19:53:11 +0100706 // return its name (the 'f' in "def f(...):")
707 return fscope->simple_name;
708}
709
710// leaves class object on stack
711// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000712STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100713 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100714 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000715 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100716 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000717 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100718 }
719
720 EMIT(load_build_class);
721
722 // scope for this class
723 scope_t *cscope = (scope_t*)pns->nodes[3];
724
725 // compile the class
726 close_over_variables_etc(comp, cscope, 0, 0);
727
728 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000729 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100730
731 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100732 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
733 mp_parse_node_t parents = pns->nodes[1];
734 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
735 parents = MP_PARSE_NODE_NULL;
736 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000737 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100738 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100739
740 // return its name (the 'C' in class C(...):")
741 return cscope->simple_name;
742}
743
Damien6cdd3af2013-10-05 18:08:26 +0100744// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200745STATIC 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 +0000746 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100747 return false;
748 }
749
750 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100751 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100752 return true;
753 }
754
Damiend99b0522013-12-21 18:17:45 +0000755 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100756 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100757 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100758#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000759 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100760 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000761 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100762 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100763#endif
Damien3ef4abb2013-10-12 16:53:13 +0100764#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000765 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +0100766 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100767#endif
Damien6cdd3af2013-10-05 18:08:26 +0100768 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100769 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100770 }
771
772 return true;
773}
774
Damien George969a6b32014-12-10 22:07:04 +0000775STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100776 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000777 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000778 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100779
Damien6cdd3af2013-10-05 18:08:26 +0100780 // inherit emit options for this function/class definition
781 uint emit_options = comp->scope_cur->emit_options;
782
783 // compile each decorator
784 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100785 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000786 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
787 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100788
789 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000790 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000791 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +0100792
793 // check for built-in decorators
794 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
795 // this was a built-in
796 num_built_in_decorators += 1;
797
798 } else {
799 // not a built-in, compile normally
800
801 // compile the decorator function
802 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +0000803 for (int j = 1; j < name_len; j++) {
804 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
805 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +0100806 }
807
808 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000809 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100810 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000811 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100812 compile_node(comp, pns_decorator->nodes[1]);
813 }
Damien429d7192013-10-04 19:53:11 +0100814 }
815 }
816
pohmelie81ebba72016-01-27 23:23:11 +0300817 // compile the body (funcdef, async funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000818 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100819 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000820 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100821 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
pohmelie81ebba72016-01-27 23:23:11 +0300822 #if MICROPY_PY_ASYNC_AWAIT
823 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) {
824 assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0]));
825 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0];
826 body_name = compile_funcdef_helper(comp, pns0, emit_options);
827 scope_t *fscope = (scope_t*)pns0->nodes[4];
828 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
829 #endif
Damien429d7192013-10-04 19:53:11 +0100830 } else {
Damien George0bb97132015-02-27 14:25:47 +0000831 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
832 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100833 }
834
835 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100836 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +0100837 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +0100838 }
839
840 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +0000841 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +0100842}
843
Damien George969a6b32014-12-10 22:07:04 +0000844STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100845 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100846 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +0000847 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +0100848}
849
Damien George6be0b0a2014-08-15 14:30:52 +0100850STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000851 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +0000852 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
pohmelie81ebba72016-01-27 23:23:11 +0300853 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) {
Damiend99b0522013-12-21 18:17:45 +0000854 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100855
pohmelie81ebba72016-01-27 23:23:11 +0300856 compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node
Damien429d7192013-10-04 19:53:11 +0100857
Damiend99b0522013-12-21 18:17:45 +0000858 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
859 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300860 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000861 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100862 for (int i = 0; i < n - 1; i++) {
863 compile_node(comp, pns1->nodes[i]);
864 }
Damiend99b0522013-12-21 18:17:45 +0000865 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
866 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100867 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000868 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100869 compile_node(comp, pns1->nodes[0]);
870 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +0000871 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
872 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +0000873 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100874 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100875 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100876 }
877 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100878 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100879 }
880
Damiend99b0522013-12-21 18:17:45 +0000881 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
882 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien George93b37262016-01-07 13:07:52 +0000883 if (MP_PARSE_NODE_IS_NULL(pn)) {
884 goto cannot_delete;
885 } else {
886 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +0000887 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100888 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
889
Damiend99b0522013-12-21 18:17:45 +0000890 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
891 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
892 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100893 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000894 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100895 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000896 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100897 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +0000898 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100899 c_del_stmt(comp, pns->nodes[0]);
900 for (int i = 0; i < n; i++) {
901 c_del_stmt(comp, pns1->nodes[i]);
902 }
Damiend99b0522013-12-21 18:17:45 +0000903 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000904 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100905 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100906 } else {
907 // sequence with 2 items
908 goto sequence_with_2_items;
909 }
910 } else {
911 // sequence with 2 items
912 sequence_with_2_items:
913 c_del_stmt(comp, pns->nodes[0]);
914 c_del_stmt(comp, pns->nodes[1]);
915 }
Damien429d7192013-10-04 19:53:11 +0100916 }
917 } else {
Damien George93b37262016-01-07 13:07:52 +0000918 // some arbitrary statment that we can't delete (eg del 1)
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100919 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100920 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100921
922 return;
923
924cannot_delete:
925 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +0100926}
927
Damien George969a6b32014-12-10 22:07:04 +0000928STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100929 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
930}
931
Damien George969a6b32014-12-10 22:07:04 +0000932STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100933 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100934 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +0100935 }
Damien George090c9232014-10-17 14:08:49 +0000936 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000937 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100938}
939
Damien George969a6b32014-12-10 22:07:04 +0000940STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100941 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100942 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +0100943 }
Damien George090c9232014-10-17 14:08:49 +0000944 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000945 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100946}
947
Damien George969a6b32014-12-10 22:07:04 +0000948STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +0100949 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100950 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +0100951 return;
952 }
Damiend99b0522013-12-21 18:17:45 +0000953 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +0100954 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +0000955 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +0000956 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +0100957 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +0000958 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
959 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 +0100960
Damien George6f355fd2014-04-10 14:11:31 +0100961 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100962 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
963 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
964 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +0000965 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +0100966 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
967 } else {
968 compile_node(comp, pns->nodes[0]);
969 }
970 EMIT(return_value);
971}
972
Damien George969a6b32014-12-10 22:07:04 +0000973STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100974 compile_node(comp, pns->nodes[0]);
975 EMIT(pop_top);
976}
977
Damien George969a6b32014-12-10 22:07:04 +0000978STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000979 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100980 // raise
Damien Georgeb9791222014-01-23 00:34:21 +0000981 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +0000982 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +0100983 // raise x from y
Damiend99b0522013-12-21 18:17:45 +0000984 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100985 compile_node(comp, pns->nodes[0]);
986 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +0000987 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +0100988 } else {
989 // raise x
990 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +0000991 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +0100992 }
993}
994
Damien George635543c2014-04-10 12:56:52 +0100995// q_base holds the base of the name
996// eg a -> q_base=a
997// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +0100998STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +0100999 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001000 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1001 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001002 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001003 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001004 pn = pns->nodes[0];
1005 is_as = true;
1006 }
Damien George635543c2014-04-10 12:56:52 +01001007 if (MP_PARSE_NODE_IS_NULL(pn)) {
1008 // empty name (eg, from . import x)
1009 *q_base = MP_QSTR_;
1010 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1011 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001012 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001013 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001014 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001015 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001016 }
Damien George635543c2014-04-10 12:56:52 +01001017 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001018 } else {
1019 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001020 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001021 {
Damien429d7192013-10-04 19:53:11 +01001022 // a name of the form a.b.c
1023 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001024 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001025 }
Damiend99b0522013-12-21 18:17:45 +00001026 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001027 int len = n - 1;
1028 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001029 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001030 }
Damien George55baff42014-01-21 21:40:13 +00001031 byte *q_ptr;
1032 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001033 for (int i = 0; i < n; i++) {
1034 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001035 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001036 }
Damien Georgec3f64d92015-11-27 12:23:18 +00001037 size_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001038 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001039 memcpy(str_dest, str_src, str_src_len);
1040 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001041 }
Damien George635543c2014-04-10 12:56:52 +01001042 qstr q_full = qstr_build_end(q_ptr);
1043 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001044 if (is_as) {
1045 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001046 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001047 }
1048 }
Damien429d7192013-10-04 19:53:11 +01001049 }
Damien429d7192013-10-04 19:53:11 +01001050 }
1051}
1052
Damien George6be0b0a2014-08-15 14:30:52 +01001053STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001054 EMIT_ARG(load_const_small_int, 0); // level 0 import
1055 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1056 qstr q_base;
1057 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001058 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001059}
1060
Damien George969a6b32014-12-10 22:07:04 +00001061STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001062 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1063}
1064
Damien George969a6b32014-12-10 22:07:04 +00001065STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001066 mp_parse_node_t pn_import_source = pns->nodes[0];
1067
1068 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1069 uint import_level = 0;
1070 do {
1071 mp_parse_node_t pn_rel;
1072 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)) {
1073 // This covers relative imports with dots only like "from .. import"
1074 pn_rel = pn_import_source;
1075 pn_import_source = MP_PARSE_NODE_NULL;
1076 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1077 // This covers relative imports starting with dot(s) like "from .foo import"
1078 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1079 pn_rel = pns_2b->nodes[0];
1080 pn_import_source = pns_2b->nodes[1];
1081 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1082 } else {
1083 // Not a relative import
1084 break;
1085 }
1086
1087 // get the list of . and/or ...'s
1088 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001089 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001090
1091 // count the total number of .'s
1092 for (int i = 0; i < n; i++) {
1093 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1094 import_level++;
1095 } else {
1096 // should be an MP_TOKEN_ELLIPSIS
1097 import_level += 3;
1098 }
1099 }
1100 } while (0);
1101
Damiend99b0522013-12-21 18:17:45 +00001102 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001103 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001104
1105 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001106 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001107 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001108
1109 // do the import
Damien George635543c2014-04-10 12:56:52 +01001110 qstr dummy_q;
1111 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001112 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001113
Damien429d7192013-10-04 19:53:11 +01001114 } else {
Damien George635543c2014-04-10 12:56:52 +01001115 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001116
1117 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001118 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001119 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001120 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001121 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1122 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1123 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001124 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001125 }
Damien Georgeb9791222014-01-23 00:34:21 +00001126 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001127
1128 // do the import
Damien George635543c2014-04-10 12:56:52 +01001129 qstr dummy_q;
1130 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001131 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001132 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1133 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1134 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001135 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001136 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001137 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001138 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001139 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001140 }
1141 }
1142 EMIT(pop_top);
1143 }
1144}
1145
Damien George584ba672014-12-21 17:26:45 +00001146STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1147 bool added;
1148 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001149 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1150 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001151 return;
1152 }
1153 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1154
1155 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1156 id_info = scope_find_global(comp->scope_cur, qst);
1157 if (id_info != NULL) {
1158 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1159 }
1160}
1161
Damien George969a6b32014-12-10 22:07:04 +00001162STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001163 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001164 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001165 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001166 for (int i = 0; i < n; i++) {
1167 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001168 }
1169 }
1170}
1171
Damien George584ba672014-12-21 17:26:45 +00001172STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1173 bool added;
1174 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001175 if (!added && id_info->kind != ID_INFO_KIND_FREE) {
1176 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001177 return;
1178 }
1179 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1180 if (id_info2 == NULL || !(id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) {
1181 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1182 return;
1183 }
1184 id_info->kind = ID_INFO_KIND_FREE;
1185 scope_close_over_in_parents(comp->scope_cur, qst);
1186}
1187
Damien George969a6b32014-12-10 22:07:04 +00001188STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001189 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001190 if (comp->scope_cur->kind == SCOPE_MODULE) {
1191 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1192 return;
1193 }
1194 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001195 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001196 for (int i = 0; i < n; i++) {
1197 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001198 }
1199 }
1200}
1201
Damien George969a6b32014-12-10 22:07:04 +00001202STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George24df30c2016-08-26 22:28:22 +10001203 // with optimisations enabled we don't compile assertions
1204 if (MP_STATE_VM(mp_optimise_value) != 0) {
1205 return;
1206 }
1207
Damien George6f355fd2014-04-10 14:11:31 +01001208 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001209 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001210 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001211 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001212 // assertion message
1213 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001214 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001215 }
Damien Georgeb9791222014-01-23 00:34:21 +00001216 EMIT_ARG(raise_varargs, 1);
1217 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001218}
1219
Damien George969a6b32014-12-10 22:07:04 +00001220STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001221 // TODO proper and/or short circuiting
1222
Damien George6f355fd2014-04-10 14:11:31 +01001223 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001224
Damien George65dc9602015-08-14 12:24:11 +01001225 // optimisation: don't emit anything when "if False"
1226 if (!node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001227 uint l_fail = comp_next_label(comp);
1228 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001229
Damien George391db862014-10-17 17:57:33 +00001230 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001231
Damien George65dc9602015-08-14 12:24:11 +01001232 // optimisation: skip everything else when "if True"
1233 if (node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001234 goto done;
1235 }
1236
1237 if (
Damien George65dc9602015-08-14 12:24:11 +01001238 // optimisation: don't jump over non-existent elif/else blocks
1239 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001240 // optimisation: don't jump if last instruction was return
1241 && !EMIT(last_emit_was_return_value)
1242 ) {
1243 // jump over elif/else blocks
1244 EMIT_ARG(jump, l_end);
1245 }
1246
1247 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001248 }
1249
Damien George235f9b32014-10-17 17:30:16 +00001250 // compile elif blocks (if any)
1251 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001252 int n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
Damien George235f9b32014-10-17 17:30:16 +00001253 for (int i = 0; i < n_elif; i++) {
1254 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1255 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001256
Damien George65dc9602015-08-14 12:24:11 +01001257 // optimisation: don't emit anything when "if False"
1258 if (!node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001259 uint l_fail = comp_next_label(comp);
1260 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001261
Damien George391db862014-10-17 17:57:33 +00001262 compile_node(comp, pns_elif->nodes[1]); // elif block
1263
Damien George65dc9602015-08-14 12:24:11 +01001264 // optimisation: skip everything else when "elif True"
1265 if (node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001266 goto done;
1267 }
1268
1269 // optimisation: don't jump if last instruction was return
1270 if (!EMIT(last_emit_was_return_value)) {
1271 EMIT_ARG(jump, l_end);
1272 }
1273 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001274 }
1275 }
1276
1277 // compile else block
1278 compile_node(comp, pns->nodes[3]); // can be null
1279
Damien George391db862014-10-17 17:57:33 +00001280done:
Damien Georgeb9791222014-01-23 00:34:21 +00001281 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001282}
1283
Damien Georgecbddb272014-02-01 20:08:18 +00001284#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001285 uint16_t old_break_label = comp->break_label; \
1286 uint16_t old_continue_label = comp->continue_label; \
1287 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001288 uint break_label = comp_next_label(comp); \
1289 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001290 comp->break_label = break_label; \
1291 comp->continue_label = continue_label; \
1292 comp->break_continue_except_level = comp->cur_except_level;
1293
1294#define END_BREAK_CONTINUE_BLOCK \
1295 comp->break_label = old_break_label; \
1296 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001297 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001298
Damien George969a6b32014-12-10 22:07:04 +00001299STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001300 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001301
Damien George391db862014-10-17 17:57:33 +00001302 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1303 uint top_label = comp_next_label(comp);
1304 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1305 EMIT_ARG(jump, continue_label);
1306 }
1307 EMIT_ARG(label_assign, top_label);
1308 compile_node(comp, pns->nodes[1]); // body
1309 EMIT_ARG(label_assign, continue_label);
1310 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1311 }
Damience89a212013-10-15 22:25:17 +01001312
1313 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001314 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001315
1316 compile_node(comp, pns->nodes[2]); // else
1317
Damien Georgeb9791222014-01-23 00:34:21 +00001318 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001319}
1320
Damien Georgee181c0d2014-12-12 17:19:56 +00001321// This function compiles an optimised for-loop of the form:
1322// for <var> in range(<start>, <end>, <step>):
1323// <body>
1324// else:
1325// <else>
1326// <var> must be an identifier and <step> must be a small-int.
1327//
1328// Semantics of for-loop require:
1329// - final failing value should not be stored in the loop variable
1330// - if the loop never runs, the loop variable should never be assigned
1331// - assignments to <var>, <end> or <step> in the body do not alter the loop
1332// (<step> is a constant for us, so no need to worry about it changing)
1333//
1334// If <end> is a small-int, then the stack during the for-loop contains just
1335// the current value of <var>. Otherwise, the stack contains <end> then the
1336// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001337STATIC void 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 +00001338 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001339
Damien George6f355fd2014-04-10 14:11:31 +01001340 uint top_label = comp_next_label(comp);
1341 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001342
Damien Georgee181c0d2014-12-12 17:19:56 +00001343 // put the end value on the stack if it's not a small-int constant
1344 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1345 if (end_on_stack) {
1346 compile_node(comp, pn_end);
1347 }
1348
1349 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001350 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001351
Damien Georgeb9791222014-01-23 00:34:21 +00001352 EMIT_ARG(jump, entry_label);
1353 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001354
Damien Georgec33ce602014-12-11 17:35:23 +00001355 // duplicate next value and store it to var
1356 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001357 c_assign(comp, pn_var, ASSIGN_STORE);
1358
Damienf3822fc2013-11-09 20:12:03 +00001359 // compile body
1360 compile_node(comp, pn_body);
1361
Damien Georgeb9791222014-01-23 00:34:21 +00001362 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001363
Damien Georgee181c0d2014-12-12 17:19:56 +00001364 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001365 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001366 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001367
Damien Georgeb9791222014-01-23 00:34:21 +00001368 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001369
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001370 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001371 if (end_on_stack) {
1372 EMIT(dup_top_two);
1373 EMIT(rot_two);
1374 } else {
1375 EMIT(dup_top);
1376 compile_node(comp, pn_end);
1377 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001378 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1379 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001380 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001381 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001382 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001383 }
Damien George63f38322015-02-28 15:04:06 +00001384 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001385
1386 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001387 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001388
1389 compile_node(comp, pn_else);
1390
Damien Georgeb9791222014-01-23 00:34:21 +00001391 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001392
1393 // discard final value of var that failed the loop condition
1394 EMIT(pop_top);
1395
1396 // discard <end> value if it's on the stack
1397 if (end_on_stack) {
1398 EMIT(pop_top);
1399 }
Damienf72fd0e2013-11-06 20:20:49 +00001400}
1401
Damien George969a6b32014-12-10 22:07:04 +00001402STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001403 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1404 // this is actually slower, but uses no heap memory
1405 // for viper it will be much, much faster
pohmelie81ebba72016-01-27 23:23:11 +03001406 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_atom_expr_normal)) {
Damiend99b0522013-12-21 18:17:45 +00001407 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +03001408 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001409 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
Damien Georgeeacbd7a2016-04-13 15:21:47 +01001410 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)) {
Damiend99b0522013-12-21 18:17:45 +00001411 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1412 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001413 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001414 mp_parse_node_t pn_range_start;
1415 mp_parse_node_t pn_range_end;
1416 mp_parse_node_t pn_range_step;
1417 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001418 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001419 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001420 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001421 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001422 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001423 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001424 } else if (n_args == 2) {
1425 pn_range_start = args[0];
1426 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001427 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001428 } else {
1429 pn_range_start = args[0];
1430 pn_range_end = args[1];
1431 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001432 // We need to know sign of step. This is possible only if it's constant
1433 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1434 optimize = false;
1435 }
Damienf72fd0e2013-11-06 20:20:49 +00001436 }
Damien George33ac0fd2015-12-08 21:05:14 +00001437 // arguments must be able to be compiled as standard expressions
1438 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1439 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start);
1440 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1441 optimize = false;
1442 }
1443 }
1444 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1445 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end);
1446 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1447 optimize = false;
1448 }
1449 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001450 }
1451 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001452 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1453 return;
1454 }
1455 }
1456 }
Damienf72fd0e2013-11-06 20:20:49 +00001457
Damien Georgecbddb272014-02-01 20:08:18 +00001458 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001459 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001460
Damien George6f355fd2014-04-10 14:11:31 +01001461 uint pop_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001462
Damien429d7192013-10-04 19:53:11 +01001463 compile_node(comp, pns->nodes[1]); // iterator
1464 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001465 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001466 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001467 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1468 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001469 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001470 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001471 }
Damien Georgeb9791222014-01-23 00:34:21 +00001472 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001473 EMIT(for_iter_end);
1474
1475 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001476 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001477
Damien429d7192013-10-04 19:53:11 +01001478 compile_node(comp, pns->nodes[3]); // else (not tested)
1479
Damien Georgeb9791222014-01-23 00:34:21 +00001480 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001481}
1482
Damien George6be0b0a2014-08-15 14:30:52 +01001483STATIC void 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 +01001484 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001485 uint l1 = comp_next_label(comp);
1486 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001487
Damien Georgeb9791222014-01-23 00:34:21 +00001488 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001489 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001490
Damien429d7192013-10-04 19:53:11 +01001491 compile_node(comp, pn_body); // body
1492 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001493 EMIT_ARG(jump, success_label); // jump over exception handler
1494
1495 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001496 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001497
Damien Georgef0406852016-09-27 12:37:21 +10001498 // at this point the top of the stack contains the exception instance that was raised
1499
Damien George6f355fd2014-04-10 14:11:31 +01001500 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001501
1502 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001503 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1504 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001505
1506 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001507 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001508
Damiend99b0522013-12-21 18:17:45 +00001509 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001510 // this is a catch all exception handler
1511 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001512 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001513 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001514 return;
1515 }
1516 } else {
1517 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001518 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1519 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1520 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1521 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001522 // handler binds the exception to a local
1523 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001524 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001525 }
1526 }
1527 EMIT(dup_top);
1528 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001529 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001530 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001531 }
1532
Damien Georgef0406852016-09-27 12:37:21 +10001533 // either discard or store the exception instance
Damien429d7192013-10-04 19:53:11 +01001534 if (qstr_exception_local == 0) {
1535 EMIT(pop_top);
1536 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001537 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001538 }
1539
Damien George6f355fd2014-04-10 14:11:31 +01001540 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001541 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001542 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001543 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001544 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001545 }
1546 compile_node(comp, pns_except->nodes[1]);
1547 if (qstr_exception_local != 0) {
1548 EMIT(pop_block);
1549 }
1550 EMIT(pop_except);
1551 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001552 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1553 EMIT_ARG(label_assign, l3);
1554 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001555 compile_store_id(comp, qstr_exception_local);
1556 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001557
Damien George8dcc0c72014-03-27 10:55:21 +00001558 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001559 EMIT(end_finally);
1560 }
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(jump, l2);
1562 EMIT_ARG(label_assign, end_finally_label);
Damien Georgef0406852016-09-27 12:37:21 +10001563 EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance
Damien429d7192013-10-04 19:53:11 +01001564 }
1565
Damien George8dcc0c72014-03-27 10:55:21 +00001566 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001567 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001568 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001569
Damien Georgeb9791222014-01-23 00:34:21 +00001570 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001571 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001572 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001573}
1574
Damien George6be0b0a2014-08-15 14:30:52 +01001575STATIC void 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 +01001576 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001577
Damien Georgeb9791222014-01-23 00:34:21 +00001578 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001579 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001580
Damien429d7192013-10-04 19:53:11 +01001581 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001582 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001583 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001584 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001585 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001586 } else {
1587 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1588 }
1589 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001590 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1591 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001592 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001593
Damien George8dcc0c72014-03-27 10:55:21 +00001594 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001595 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001596}
1597
Damien George969a6b32014-12-10 22:07:04 +00001598STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001599 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1600 {
Damiend99b0522013-12-21 18:17:45 +00001601 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1602 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001603 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001604 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1605 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001606 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001607 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001608 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001609 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001610 // no finally
1611 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1612 } else {
1613 // have finally
Damiend99b0522013-12-21 18:17:45 +00001614 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 +01001615 }
1616 } else {
1617 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001618 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001619 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001620 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001621 }
Damien429d7192013-10-04 19:53:11 +01001622 }
1623}
1624
Damien George6be0b0a2014-08-15 14:30:52 +01001625STATIC void 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 +01001626 if (n == 0) {
1627 // no more pre-bits, compile the body of the with
1628 compile_node(comp, body);
1629 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001630 uint l_end = comp_next_label(comp);
Damien George2c915e12016-04-07 08:53:24 +01001631 if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
1632 // we need to allocate an extra label for the native emitter
1633 // it will use l_end+1 as an auxiliary label
1634 comp_next_label(comp);
1635 }
Damiend99b0522013-12-21 18:17:45 +00001636 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001637 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001638 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001639 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001640 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001641 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1642 } else {
1643 // this pre-bit is just an expression
1644 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001645 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001646 EMIT(pop_top);
1647 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001648 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001649 // compile additional pre-bits and the body
1650 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1651 // finish this with block
Damien Georgece8b4e82016-04-07 08:50:38 +01001652 EMIT_ARG(with_cleanup, l_end);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001653 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001654 EMIT(end_finally);
1655 }
1656}
1657
Damien George969a6b32014-12-10 22:07:04 +00001658STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001659 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001660 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001661 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001662 assert(n > 0);
1663
1664 // compile in a nested fashion
1665 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1666}
1667
pohmelie81ebba72016-01-27 23:23:11 +03001668STATIC void compile_yield_from(compiler_t *comp) {
1669 EMIT(get_iter);
1670 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1671 EMIT(yield_from);
1672}
1673
1674#if MICROPY_PY_ASYNC_AWAIT
1675STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1676 EMIT_ARG(load_method, method);
1677 EMIT_ARG(call_method, 0, 0, 0);
1678 compile_yield_from(comp);
1679}
1680
1681STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1682 // comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1683
1684 qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1685 uint while_else_label = comp_next_label(comp);
1686 uint try_exception_label = comp_next_label(comp);
1687 uint try_else_label = comp_next_label(comp);
1688 uint try_finally_label = comp_next_label(comp);
1689
1690 compile_node(comp, pns->nodes[1]); // iterator
1691 compile_await_object_method(comp, MP_QSTR___aiter__);
1692 compile_store_id(comp, context);
1693
1694 START_BREAK_CONTINUE_BLOCK
1695
1696 EMIT_ARG(label_assign, continue_label);
1697
1698 EMIT_ARG(setup_except, try_exception_label);
1699 compile_increase_except_level(comp);
1700
1701 compile_load_id(comp, context);
1702 compile_await_object_method(comp, MP_QSTR___anext__);
1703 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1704 EMIT(pop_block);
1705 EMIT_ARG(jump, try_else_label);
1706
1707 EMIT_ARG(label_assign, try_exception_label);
1708 EMIT(start_except_handler);
1709 EMIT(dup_top);
1710 EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration);
1711 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1712 EMIT_ARG(pop_jump_if, false, try_finally_label);
1713 EMIT(pop_top);
1714 EMIT(pop_top);
1715 EMIT(pop_top);
1716 EMIT(pop_except);
1717 EMIT_ARG(jump, while_else_label);
1718
1719 EMIT_ARG(label_assign, try_finally_label);
1720 EMIT_ARG(adjust_stack_size, 3);
1721 compile_decrease_except_level(comp);
1722 EMIT(end_finally);
1723 EMIT(end_except_handler);
1724
1725 EMIT_ARG(label_assign, try_else_label);
1726 compile_node(comp, pns->nodes[2]); // body
1727
1728 EMIT_ARG(jump, continue_label);
1729 // break/continue apply to outer loop (if any) in the else block
1730 END_BREAK_CONTINUE_BLOCK
1731
1732 EMIT_ARG(label_assign, while_else_label);
1733 compile_node(comp, pns->nodes[3]); // else
1734
1735 EMIT_ARG(label_assign, break_label);
1736}
1737
1738STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1739 if (n == 0) {
1740 // no more pre-bits, compile the body of the with
1741 compile_node(comp, body);
1742 } else {
1743 uint try_exception_label = comp_next_label(comp);
1744 uint no_reraise_label = comp_next_label(comp);
1745 uint try_else_label = comp_next_label(comp);
1746 uint end_label = comp_next_label(comp);
1747 qstr context;
1748
1749 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1750 // this pre-bit is of the form "a as b"
1751 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
1752 compile_node(comp, pns->nodes[0]);
1753 context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1754 compile_store_id(comp, context);
1755 compile_load_id(comp, context);
1756 compile_await_object_method(comp, MP_QSTR___aenter__);
1757 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1758 } else {
1759 // this pre-bit is just an expression
1760 compile_node(comp, nodes[0]);
1761 context = MP_PARSE_NODE_LEAF_ARG(nodes[0]);
1762 compile_store_id(comp, context);
1763 compile_load_id(comp, context);
1764 compile_await_object_method(comp, MP_QSTR___aenter__);
1765 EMIT(pop_top);
1766 }
1767
1768 compile_load_id(comp, context);
1769 EMIT_ARG(load_method, MP_QSTR___aexit__);
1770
1771 EMIT_ARG(setup_except, try_exception_label);
1772 compile_increase_except_level(comp);
1773 // compile additional pre-bits and the body
1774 compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body);
1775 // finish this with block
1776 EMIT(pop_block);
1777 EMIT_ARG(jump, try_else_label); // jump over exception handler
1778
1779 EMIT_ARG(label_assign, try_exception_label); // start of exception handler
1780 EMIT(start_except_handler);
1781 EMIT(rot_three);
1782 EMIT(rot_two);
1783 EMIT_ARG(call_method, 3, 0, 0);
1784 compile_yield_from(comp);
1785 EMIT_ARG(pop_jump_if, true, no_reraise_label);
1786 EMIT_ARG(raise_varargs, 0);
1787
1788 EMIT_ARG(label_assign, no_reraise_label);
1789 EMIT(pop_except);
1790 EMIT_ARG(jump, end_label);
1791
1792 EMIT_ARG(adjust_stack_size, 5);
1793 compile_decrease_except_level(comp);
1794 EMIT(end_finally);
1795 EMIT(end_except_handler);
1796
1797 EMIT_ARG(label_assign, try_else_label); // start of try-else handler
1798 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1799 EMIT(dup_top);
1800 EMIT(dup_top);
1801 EMIT_ARG(call_method, 3, 0, 0);
1802 compile_yield_from(comp);
1803 EMIT(pop_top);
1804
1805 EMIT_ARG(label_assign, end_label);
1806
1807 }
1808}
1809
1810STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1811 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1812 mp_parse_node_t *nodes;
1813 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1814 assert(n > 0);
1815
1816 // compile in a nested fashion
1817 compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1818}
1819
1820STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1821 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0]));
1822 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
1823 if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) {
1824 // async def
1825 compile_funcdef(comp, pns0);
1826 scope_t *fscope = (scope_t*)pns0->nodes[4];
1827 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1828 } else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
1829 // async for
1830 compile_async_for_stmt(comp, pns0);
1831 } else {
1832 // async with
1833 assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt);
1834 compile_async_with_stmt(comp, pns0);
1835 }
1836}
1837#endif
1838
Damien George969a6b32014-12-10 22:07:04 +00001839STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001840 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001841 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1842 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001843 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001844 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001845 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001846 EMIT(pop_top);
1847
Damien429d7192013-10-04 19:53:11 +01001848 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001849 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001850 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001851 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001852 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1853 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001854 // do nothing with a lonely constant
1855 } else {
1856 compile_node(comp, pns->nodes[0]); // just an expression
1857 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1858 }
Damien429d7192013-10-04 19:53:11 +01001859 }
Damien Georgeb948de32015-10-08 14:26:01 +01001860 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
Damiend99b0522013-12-21 18:17:45 +00001861 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1862 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001863 if (kind == PN_expr_stmt_augassign) {
1864 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1865 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001866 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001867 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001868 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001869 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1870 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1871 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1872 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1873 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1874 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1875 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1876 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1877 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1878 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1879 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001880 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001881 }
Damien George7e5fb242014-02-01 22:18:47 +00001882 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001883 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1884 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001885 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
Damien Georgeb948de32015-10-08 14:26:01 +01001886 compile_node(comp, pns1->nodes[rhs]); // rhs
Damien429d7192013-10-04 19:53:11 +01001887 // following CPython, we store left-most first
1888 if (rhs > 0) {
1889 EMIT(dup_top);
1890 }
1891 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1892 for (int i = 0; i < rhs; i++) {
1893 if (i + 1 < rhs) {
1894 EMIT(dup_top);
1895 }
Damien Georgeb948de32015-10-08 14:26:01 +01001896 c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001897 }
Damien George0bb97132015-02-27 14:25:47 +00001898 } else {
Damien Georgeb948de32015-10-08 14:26:01 +01001899 plain_assign:
Damien George42e0c592015-03-14 13:11:35 +00001900 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001901 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001902 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001903 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2
Damiend99b0522013-12-21 18:17:45 +00001904 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001905 // optimisation for a, b = c, d
Damien Georgeb948de32015-10-08 14:26:01 +01001906 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001907 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001908 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1909 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1910 // can't optimise when it's a star expression on the lhs
1911 goto no_optimisation;
1912 }
Damien429d7192013-10-04 19:53:11 +01001913 compile_node(comp, pns10->nodes[0]); // rhs
1914 compile_node(comp, pns10->nodes[1]); // rhs
1915 EMIT(rot_two);
1916 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1917 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001918 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001919 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001920 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001921 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3
Damiend99b0522013-12-21 18:17:45 +00001922 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001923 // optimisation for a, b, c = d, e, f
Damien Georgeb948de32015-10-08 14:26:01 +01001924 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001925 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001926 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1927 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1928 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1929 // can't optimise when it's a star expression on the lhs
1930 goto no_optimisation;
1931 }
Damien429d7192013-10-04 19:53:11 +01001932 compile_node(comp, pns10->nodes[0]); // rhs
1933 compile_node(comp, pns10->nodes[1]); // rhs
1934 compile_node(comp, pns10->nodes[2]); // rhs
1935 EMIT(rot_three);
1936 EMIT(rot_two);
1937 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1938 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1939 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1940 } else {
Damien George495d7812014-04-08 17:51:47 +01001941 no_optimisation:
Damien Georgeb948de32015-10-08 14:26:01 +01001942 compile_node(comp, pns->nodes[1]); // rhs
Damien429d7192013-10-04 19:53:11 +01001943 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1944 }
Damien429d7192013-10-04 19:53:11 +01001945 }
Damien Georgeb948de32015-10-08 14:26:01 +01001946 } else {
1947 goto plain_assign;
Damien429d7192013-10-04 19:53:11 +01001948 }
1949}
1950
Damien George6be0b0a2014-08-15 14:30:52 +01001951STATIC void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001952 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001953 compile_node(comp, pns->nodes[0]);
1954 for (int i = 1; i < num_nodes; i += 1) {
1955 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001956 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001957 }
1958}
1959
Damien George969a6b32014-12-10 22:07:04 +00001960STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001961 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1962 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001963
Damien George6f355fd2014-04-10 14:11:31 +01001964 uint l_fail = comp_next_label(comp);
1965 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001966 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1967 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001968 EMIT_ARG(jump, l_end);
1969 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001970 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01001971 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001972 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001973}
1974
Damien George969a6b32014-12-10 22:07:04 +00001975STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001976 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001977 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001978 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 +01001979 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001980 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001981 }
1982
1983 // get the scope for this lambda
1984 scope_t *this_scope = (scope_t*)pns->nodes[2];
1985
Damien George2c838942015-11-17 14:00:14 +00001986 // compile the lambda definition
1987 compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
Damien429d7192013-10-04 19:53:11 +01001988}
1989
Damien George7711afb2015-02-28 15:10:18 +00001990STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01001991 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001992 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001993 for (int i = 0; i < n; i += 1) {
1994 compile_node(comp, pns->nodes[i]);
1995 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00001996 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01001997 }
1998 }
Damien Georgeb9791222014-01-23 00:34:21 +00001999 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002000}
2001
Damien George7711afb2015-02-28 15:10:18 +00002002STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2003 compile_or_and_test(comp, pns, true);
2004}
2005
Damien George969a6b32014-12-10 22:07:04 +00002006STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002007 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002008}
2009
Damien George969a6b32014-12-10 22:07:04 +00002010STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002011 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002012 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002013}
2014
Damien George969a6b32014-12-10 22:07:04 +00002015STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002016 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002017 compile_node(comp, pns->nodes[0]);
2018 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002019 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002020 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002021 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002022 }
2023 for (int i = 1; i + 1 < num_nodes; i += 2) {
2024 compile_node(comp, pns->nodes[i + 1]);
2025 if (i + 2 < num_nodes) {
2026 EMIT(dup_top);
2027 EMIT(rot_three);
2028 }
Damien George7e5fb242014-02-01 22:18:47 +00002029 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002030 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002031 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002032 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2033 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2034 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2035 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2036 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2037 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002038 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002039 }
2040 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002041 } else {
2042 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002043 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2044 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002045 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002046 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002047 } else {
2048 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002049 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002050 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002051 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002052 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002053 }
Damien429d7192013-10-04 19:53:11 +01002054 }
Damien429d7192013-10-04 19:53:11 +01002055 }
2056 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002057 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002058 }
2059 }
2060 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002061 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002062 EMIT_ARG(jump, l_end);
2063 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002064 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002065 EMIT(rot_two);
2066 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002067 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002068 }
2069}
2070
Damien George969a6b32014-12-10 22:07:04 +00002071STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002072 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002073}
2074
Damien George969a6b32014-12-10 22:07:04 +00002075STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002076 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002077}
2078
Damien George969a6b32014-12-10 22:07:04 +00002079STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002080 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002081}
2082
Damien George969a6b32014-12-10 22:07:04 +00002083STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002084 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002085}
2086
Damien George969a6b32014-12-10 22:07:04 +00002087STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002088 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002089 compile_node(comp, pns->nodes[0]);
2090 for (int i = 1; i + 1 < num_nodes; i += 2) {
2091 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002092 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002093 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002094 } else {
Damien George0bb97132015-02-27 14:25:47 +00002095 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2096 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002097 }
2098 }
2099}
2100
Damien George969a6b32014-12-10 22:07:04 +00002101STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002102 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002103 compile_node(comp, pns->nodes[0]);
2104 for (int i = 1; i + 1 < num_nodes; i += 2) {
2105 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002106 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002107 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002108 } else {
Damien George0bb97132015-02-27 14:25:47 +00002109 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2110 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002111 }
2112 }
2113}
2114
Damien George969a6b32014-12-10 22:07:04 +00002115STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002116 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002117 compile_node(comp, pns->nodes[0]);
2118 for (int i = 1; i + 1 < num_nodes; i += 2) {
2119 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002120 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002121 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002122 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002123 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002124 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002125 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002126 } else {
Damien George0bb97132015-02-27 14:25:47 +00002127 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2128 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002129 }
2130 }
2131}
2132
Damien George969a6b32014-12-10 22:07:04 +00002133STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002134 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002135 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002136 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002137 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002138 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002139 } else {
Damien George0bb97132015-02-27 14:25:47 +00002140 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2141 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002142 }
2143}
2144
pohmelie81ebba72016-01-27 23:23:11 +03002145STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002146 // this is to handle special super() call
2147 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2148
2149 compile_generic_all_nodes(comp, pns);
pohmelie81ebba72016-01-27 23:23:11 +03002150}
Damien George3acaa282016-03-16 13:04:51 +00002151
pohmelie81ebba72016-01-27 23:23:11 +03002152STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2153 compile_generic_all_nodes(comp, pns); // 2 nodes, arguments of power
2154 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien George35e2a4e2014-02-05 00:51:47 +00002155}
2156
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002157STATIC 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 +01002158 // function to call is on top of stack
2159
Damien George35e2a4e2014-02-05 00:51:47 +00002160 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002161 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George542bd6b2015-03-26 14:42:40 +00002162 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002163 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002164 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002165 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002166 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002167 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002168 EMIT_ARG(call_function, 2, 0, 0);
2169 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002170 }
2171 }
Damien George01039b52014-12-21 17:44:27 +00002172 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002173 return;
2174 }
Damien George35e2a4e2014-02-05 00:51:47 +00002175
Damien George2c0842b2014-04-27 16:46:51 +01002176 // get the list of arguments
2177 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002178 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002179
Damien George2c0842b2014-04-27 16:46:51 +01002180 // compile the arguments
2181 // Rather than calling compile_node on the list, we go through the list of args
2182 // explicitly here so that we can count the number of arguments and give sensible
2183 // error messages.
2184 int n_positional = n_positional_extra;
2185 uint n_keyword = 0;
2186 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002187 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002188 for (int i = 0; i < n_args; i++) {
2189 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2190 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2191 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2192 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2193 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2194 return;
2195 }
2196 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002197 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002198 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2199 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2200 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2201 return;
2202 }
2203 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002204 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002205 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002206 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002207 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2208 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2209 return;
2210 }
Damien George59fba2d2015-06-25 14:42:13 +00002211 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002212 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002213 n_keyword += 1;
2214 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002215 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2216 n_positional++;
2217 }
2218 } else {
2219 goto normal_argument;
2220 }
2221 } else {
2222 normal_argument:
2223 if (n_keyword > 0) {
2224 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2225 return;
2226 }
2227 compile_node(comp, args[i]);
2228 n_positional++;
2229 }
Damien429d7192013-10-04 19:53:11 +01002230 }
2231
Delio Brignolie6978a42015-09-17 12:07:06 +02002232 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002233 // if we had one but not the other then we load "null" as a place holder
2234 if (star_flags != 0) {
2235 if (star_args_node == NULL) {
2236 EMIT(load_null);
2237 } else {
2238 compile_node(comp, star_args_node->nodes[0]);
2239 }
2240 if (dblstar_args_node == NULL) {
2241 EMIT(load_null);
2242 } else {
2243 compile_node(comp, dblstar_args_node->nodes[0]);
2244 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002245 }
2246
Damien George2c0842b2014-04-27 16:46:51 +01002247 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002248 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002249 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002250 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002251 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002252 }
Damien429d7192013-10-04 19:53:11 +01002253}
2254
pohmelie81ebba72016-01-27 23:23:11 +03002255STATIC void compile_atom_expr_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002256 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002257 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002258 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 +01002259 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002260 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2261 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002262 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002263 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002264 i += 1;
2265 } else {
2266 compile_node(comp, pns->nodes[i]);
2267 }
Damien George35e2a4e2014-02-05 00:51:47 +00002268 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002269 }
2270}
2271
Damien George969a6b32014-12-10 22:07:04 +00002272STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002273 // a list of strings
Damien63321742013-12-10 17:41:49 +00002274
2275 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002276 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien George831137b2015-12-17 13:13:18 +00002277 size_t n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002278 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002279 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002280 int pn_kind;
2281 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2282 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2283 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2284 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2285 } else {
2286 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2287 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002288 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2289 pn_kind = MP_PARSE_NODE_STRING;
2290 } else {
2291 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2292 pn_kind = MP_PARSE_NODE_BYTES;
2293 }
Damien George831137b2015-12-17 13:13:18 +00002294 n_bytes += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002295 }
Damien63321742013-12-10 17:41:49 +00002296 if (i == 0) {
2297 string_kind = pn_kind;
2298 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002299 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002300 return;
2301 }
Damien429d7192013-10-04 19:53:11 +01002302 }
Damien63321742013-12-10 17:41:49 +00002303
Damien George65ef6b72015-01-14 21:17:27 +00002304 // if we are not in the last pass, just load a dummy object
2305 if (comp->pass != MP_PASS_EMIT) {
2306 EMIT_ARG(load_const_obj, mp_const_none);
2307 return;
2308 }
2309
Damien63321742013-12-10 17:41:49 +00002310 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002311 vstr_t vstr;
2312 vstr_init_len(&vstr, n_bytes);
2313 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002314 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002315 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien Georgec3f64d92015-11-27 12:23:18 +00002316 size_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002317 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2318 memcpy(s_dest, s, s_len);
2319 s_dest += s_len;
2320 } else {
2321 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George831137b2015-12-17 13:13:18 +00002322 memcpy(s_dest, (const char*)pns_string->nodes[0], pns_string->nodes[1]);
2323 s_dest += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002324 }
Damien63321742013-12-10 17:41:49 +00002325 }
Damien George65ef6b72015-01-14 21:17:27 +00002326
2327 // load the object
Damien George05005f62015-01-21 22:48:37 +00002328 EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr));
Damien429d7192013-10-04 19:53:11 +01002329}
2330
2331// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002332STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002333 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2334 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2335 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002336
Damien George36db6bc2014-05-07 17:24:22 +01002337 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002338 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002339 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 +01002340 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002341 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002342 }
2343
2344 // get the scope for this comprehension
2345 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2346
2347 // compile the comprehension
2348 close_over_variables_etc(comp, this_scope, 0, 0);
2349
2350 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2351 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002352 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002353}
2354
Damien George969a6b32014-12-10 22:07:04 +00002355STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002356 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002357 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002358 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
Damien George93b37262016-01-07 13:07:52 +00002359 } else {
2360 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +00002361 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2362 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2363 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2364 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2365 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002366 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002367 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002368 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002369 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002370 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002371 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002372 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002373 // generator expression
2374 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2375 } else {
2376 // tuple with 2 items
2377 goto tuple_with_2_items;
2378 }
2379 } else {
2380 // tuple with 2 items
2381 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002382 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002383 }
Damien429d7192013-10-04 19:53:11 +01002384 }
2385}
2386
Damien George969a6b32014-12-10 22:07:04 +00002387STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002388 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002389 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002390 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002391 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2392 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2393 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2394 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2395 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002396 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002397 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002398 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002399 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002400 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002401 // list of many items
2402 compile_node(comp, pns2->nodes[0]);
2403 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002404 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002405 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002406 // list comprehension
2407 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2408 } else {
2409 // list with 2 items
2410 goto list_with_2_items;
2411 }
2412 } else {
2413 // list with 2 items
2414 list_with_2_items:
2415 compile_node(comp, pns2->nodes[0]);
2416 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002417 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002418 }
2419 } else {
2420 // list with 1 item
2421 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002422 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002423 }
2424}
2425
Damien George969a6b32014-12-10 22:07:04 +00002426STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002427 mp_parse_node_t pn = pns->nodes[0];
2428 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002429 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002430 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002431 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2432 pns = (mp_parse_node_struct_t*)pn;
2433 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002434 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002435 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002436 compile_node(comp, pn);
2437 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002438 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2439 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2440 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2441 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002442 // dict/set with multiple elements
2443
2444 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002445 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002446 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002447
2448 // first element sets whether it's a dict or set
2449 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002450 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002451 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002452 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002453 compile_node(comp, pns->nodes[0]);
2454 EMIT(store_map);
2455 is_dict = true;
2456 } else {
2457 // a set
2458 compile_node(comp, pns->nodes[0]); // 1st value of set
2459 is_dict = false;
2460 }
2461
2462 // process rest of elements
2463 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002464 mp_parse_node_t pn_i = nodes[i];
2465 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2466 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002467 if (is_dict) {
2468 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002469 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002470 return;
2471 }
2472 EMIT(store_map);
2473 } else {
2474 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002475 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002476 return;
2477 }
2478 }
2479 }
2480
Damien Georgee37dcaa2014-12-27 17:07:16 +00002481 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002482 // if it's a set, build it
2483 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002484 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002485 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002486 #endif
Damien George0bb97132015-02-27 14:25:47 +00002487 } else {
2488 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002489 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002490 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002491 // a dictionary comprehension
2492 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2493 } else {
2494 // a set comprehension
2495 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2496 }
Damien429d7192013-10-04 19:53:11 +01002497 }
2498 } else {
2499 // set with one element
2500 goto set_with_one_element;
2501 }
2502 } else {
2503 // set with one element
2504 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002505 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002506 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002507 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002508 #else
2509 assert(0);
2510 #endif
Damien429d7192013-10-04 19:53:11 +01002511 }
2512}
2513
Damien George969a6b32014-12-10 22:07:04 +00002514STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002515 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002516}
2517
Damien George969a6b32014-12-10 22:07:04 +00002518STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002519 // object who's index we want is on top of stack
2520 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002521 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002522}
2523
Damien George969a6b32014-12-10 22:07:04 +00002524STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002525 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002526 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002527}
2528
Damien George83204f32014-12-27 17:20:41 +00002529#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002530STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002531 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2532 mp_parse_node_t pn = pns->nodes[0];
2533 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002534 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002535 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2536 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002537 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2538 pns = (mp_parse_node_struct_t*)pn;
2539 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002540 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002541 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002542 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002543 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002544 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002545 } else {
2546 // [?::x]
2547 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002548 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002549 }
Damiend99b0522013-12-21 18:17:45 +00002550 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002551 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002552 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2553 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2554 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2555 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002556 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002557 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002558 } else {
2559 // [?:x:x]
2560 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002561 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002562 }
2563 } else {
2564 // [?:x]
2565 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002566 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002567 }
2568 } else {
2569 // [?:x]
2570 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002571 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002572 }
2573}
2574
Damien George969a6b32014-12-10 22:07:04 +00002575STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002576 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002577 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2578 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002579}
2580
Damien George969a6b32014-12-10 22:07:04 +00002581STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002582 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002583 compile_subscript_3_helper(comp, pns);
2584}
Damien George83204f32014-12-27 17:20:41 +00002585#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002586
Damien George969a6b32014-12-10 22:07:04 +00002587STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002588 // if this is called then we are compiling a dict key:value pair
2589 compile_node(comp, pns->nodes[1]); // value
2590 compile_node(comp, pns->nodes[0]); // key
2591}
2592
Damien George969a6b32014-12-10 22:07:04 +00002593STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002594 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002595 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002596 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002597}
2598
Damien George969a6b32014-12-10 22:07:04 +00002599STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002600 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002601 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002602 return;
2603 }
Damiend99b0522013-12-21 18:17:45 +00002604 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002605 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002606 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002607 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2608 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002609 compile_node(comp, pns->nodes[0]);
pohmelie81ebba72016-01-27 23:23:11 +03002610 compile_yield_from(comp);
Damien429d7192013-10-04 19:53:11 +01002611 } else {
2612 compile_node(comp, pns->nodes[0]);
2613 EMIT(yield_value);
2614 }
2615}
2616
pohmelie81ebba72016-01-27 23:23:11 +03002617#if MICROPY_PY_ASYNC_AWAIT
2618STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) {
2619 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
2620 compile_syntax_error(comp, (mp_parse_node_t)pns, "'await' outside function");
2621 return;
2622 }
2623 compile_atom_expr_normal(comp, pns);
2624 compile_yield_from(comp);
2625}
2626#endif
2627
Damien George65ef6b72015-01-14 21:17:27 +00002628STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2629 // only create and load the actual str object on the last pass
2630 if (comp->pass != MP_PASS_EMIT) {
2631 EMIT_ARG(load_const_obj, mp_const_none);
2632 } else {
Damien George831137b2015-12-17 13:13:18 +00002633 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], pns->nodes[1], false));
Damien George65ef6b72015-01-14 21:17:27 +00002634 }
2635}
2636
2637STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2638 // only create and load the actual bytes object on the last pass
2639 if (comp->pass != MP_PASS_EMIT) {
2640 EMIT_ARG(load_const_obj, mp_const_none);
2641 } else {
Damien George831137b2015-12-17 13:13:18 +00002642 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], pns->nodes[1]));
Damien George65ef6b72015-01-14 21:17:27 +00002643 }
2644}
2645
Damien George7d414a12015-02-08 01:57:40 +00002646STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002647 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2648 // nodes are 32-bit pointers, but need to extract 64-bit object
2649 EMIT_ARG(load_const_obj, (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
2650 #else
Damien George7d414a12015-02-08 01:57:40 +00002651 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002652 #endif
Damien George7d414a12015-02-08 01:57:40 +00002653}
2654
Damiend99b0522013-12-21 18:17:45 +00002655typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien George3ff16ff2016-05-20 12:38:15 +01002656STATIC const compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002657#define nc NULL
2658#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002659#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002660#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002661#undef nc
2662#undef c
2663#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002664 NULL,
2665 compile_string,
2666 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002667 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002668};
2669
Damien George6be0b0a2014-08-15 14:30:52 +01002670STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002671 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002672 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002673 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002674 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Damien Georgeea235202016-02-11 22:30:53 +00002675 #if MICROPY_DYNAMIC_COMPILER
2676 mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1));
2677 if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) {
2678 // integer fits in target runtime's small-int
2679 EMIT_ARG(load_const_small_int, arg);
2680 } else {
2681 // integer doesn't fit, so create a multi-precision int object
2682 // (but only create the actual object on the last pass)
2683 if (comp->pass != MP_PASS_EMIT) {
2684 EMIT_ARG(load_const_obj, mp_const_none);
2685 } else {
2686 EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg));
2687 }
2688 }
2689 #else
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002690 EMIT_ARG(load_const_small_int, arg);
Damien Georgeea235202016-02-11 22:30:53 +00002691 #endif
Damiend99b0522013-12-21 18:17:45 +00002692 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George831137b2015-12-17 13:13:18 +00002693 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002694 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002695 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002696 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2697 case MP_PARSE_NODE_BYTES:
2698 // only create and load the actual bytes object on the last pass
2699 if (comp->pass != MP_PASS_EMIT) {
2700 EMIT_ARG(load_const_obj, mp_const_none);
2701 } else {
Damien Georgec3f64d92015-11-27 12:23:18 +00002702 size_t len;
Damien George59fba2d2015-06-25 14:42:13 +00002703 const byte *data = qstr_data(arg, &len);
2704 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2705 }
2706 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002707 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002708 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002709 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002710 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002711 // do nothing
2712 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002713 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002714 }
2715 break;
Damien429d7192013-10-04 19:53:11 +01002716 }
2717 } else {
Damiend99b0522013-12-21 18:17:45 +00002718 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002719 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002720 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2721 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002722#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002723 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2724 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002725#endif
Damien George65ef6b72015-01-14 21:17:27 +00002726 compile_syntax_error(comp, pn, "internal compiler error");
2727 } else {
2728 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002729 }
2730 }
2731}
2732
Damien George2ac4af62014-08-15 16:45:41 +01002733STATIC void 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) {
Damien George9a569122015-11-23 16:50:42 +00002734 // check that **kw is last
2735 if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
2736 compile_syntax_error(comp, pn, "invalid syntax");
2737 return;
2738 }
2739
Damien George2827d622014-04-27 15:50:52 +01002740 qstr param_name = MP_QSTR_NULL;
2741 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002742 if (MP_PARSE_NODE_IS_ID(pn)) {
2743 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002744 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002745 // comes after a star, so counts as a keyword-only parameter
2746 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002747 } else {
Damien George2827d622014-04-27 15:50:52 +01002748 // comes before a star, so counts as a positional parameter
2749 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002750 }
Damienb14de212013-10-06 00:28:28 +01002751 } else {
Damiend99b0522013-12-21 18:17:45 +00002752 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2753 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2754 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2755 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002756 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002757 // comes after a star, so counts as a keyword-only parameter
2758 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002759 } else {
Damien George2827d622014-04-27 15:50:52 +01002760 // comes before a star, so counts as a positional parameter
2761 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002762 }
Damiend99b0522013-12-21 18:17:45 +00002763 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George9a569122015-11-23 16:50:42 +00002764 if (comp->have_star) {
2765 // more than one star
2766 compile_syntax_error(comp, pn, "invalid syntax");
2767 return;
2768 }
Damien George8b19db02014-04-11 23:25:34 +01002769 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002770 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002771 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002772 // bare star
2773 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002774 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002775 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002776 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002777 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002778 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002779 } else {
2780 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002781 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002782 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002783 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2784 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002785 }
Damien George0bb97132015-02-27 14:25:47 +00002786 } else {
2787 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002788 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002789 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002790 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002791 }
Damien429d7192013-10-04 19:53:11 +01002792 }
2793
Damien George2827d622014-04-27 15:50:52 +01002794 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002795 bool added;
2796 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2797 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002798 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002799 return;
2800 }
Damien429d7192013-10-04 19:53:11 +01002801 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002802 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002803 }
2804}
2805
Damien George2827d622014-04-27 15:50:52 +01002806STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002807 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002808}
2809
Damien George2827d622014-04-27 15:50:52 +01002810STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002811 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2812}
2813
Damien Georgeea5b59b2015-09-04 16:44:14 +01002814#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002815STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2816 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2817 // no annotation
2818 return;
2819 }
2820
2821 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2822 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2823 // named parameter with possible annotation
2824 // fallthrough
2825 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2826 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2827 // named star with possible annotation
2828 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2829 // fallthrough
2830 } else {
2831 // no annotation
2832 return;
2833 }
2834 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
2835 // double star with possible annotation
2836 // fallthrough
2837 } else {
2838 // no annotation
2839 return;
2840 }
2841
2842 mp_parse_node_t pn_annotation = pns->nodes[1];
2843
2844 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002845 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2846 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2847 assert(id_info != NULL);
2848
Damien Georgeea5b59b2015-09-04 16:44:14 +01002849 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2850 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2851 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2852 } else {
2853 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002854 }
Damien George2ac4af62014-08-15 16:45:41 +01002855 }
Damien429d7192013-10-04 19:53:11 +01002856}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002857#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002858
Damien Georgea8312432015-12-18 01:37:55 +00002859STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_struct_t *pns_comp_for, mp_parse_node_t pn_inner_expr, int for_depth) {
2860 uint l_top = comp_next_label(comp);
2861 uint l_end = comp_next_label(comp);
2862 EMIT_ARG(label_assign, l_top);
2863 EMIT_ARG(for_iter, l_end);
2864 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2865 mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2866
Damien429d7192013-10-04 19:53:11 +01002867 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002868 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002869 // no more nested if/for; compile inner expression
2870 compile_node(comp, pn_inner_expr);
Damien Georgea5624bf2016-09-18 23:59:47 +10002871 if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
Damien429d7192013-10-04 19:53:11 +01002872 EMIT(yield_value);
2873 EMIT(pop_top);
Damien Georgea5624bf2016-09-18 23:59:47 +10002874 } else {
2875 EMIT_ARG(store_comp, comp->scope_cur->kind, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002876 }
Damiend99b0522013-12-21 18:17:45 +00002877 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002878 // if condition
Damiend99b0522013-12-21 18:17:45 +00002879 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002880 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2881 pn_iter = pns_comp_if->nodes[1];
2882 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002883 } else {
2884 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002885 // for loop
Damiend99b0522013-12-21 18:17:45 +00002886 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002887 compile_node(comp, pns_comp_for2->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002888 EMIT(get_iter);
Damien Georgea8312432015-12-18 01:37:55 +00002889 compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1);
Damien429d7192013-10-04 19:53:11 +01002890 }
Damien Georgea8312432015-12-18 01:37:55 +00002891
2892 EMIT_ARG(jump, l_top);
2893 EMIT_ARG(label_assign, l_end);
2894 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002895}
2896
Damien George1463c1f2014-04-25 23:52:57 +01002897STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002898#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002899 // see http://www.python.org/dev/peps/pep-0257/
2900
2901 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002902 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002903 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002904 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002905 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002906 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2907 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002908 for (int i = 0; i < num_nodes; i++) {
2909 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002910 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 +00002911 // not a newline, so this is the first statement; finish search
2912 break;
2913 }
2914 }
2915 // 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 +00002916 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002917 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002918 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002919 } else {
2920 return;
2921 }
2922
2923 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002924 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002925 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002926 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2927 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2928 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2929 // compile the doc string
2930 compile_node(comp, pns->nodes[0]);
2931 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002932 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002933 }
2934 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002935#else
2936 (void)comp;
2937 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002938#endif
Damien429d7192013-10-04 19:53:11 +01002939}
2940
Damien George1463c1f2014-04-25 23:52:57 +01002941STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002942 comp->pass = pass;
2943 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002944 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002945 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002946
Damien George36db6bc2014-05-07 17:24:22 +01002947 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002948 // reset maximum stack sizes in scope
2949 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002950 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002951 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002952 }
2953
Damien429d7192013-10-04 19:53:11 +01002954 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002955 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2956 assert(scope->kind == SCOPE_MODULE);
2957 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2958 compile_node(comp, pns->nodes[0]); // compile the expression
2959 EMIT(return_value);
2960 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002961 if (!comp->is_repl) {
2962 check_for_doc_string(comp, scope->pn);
2963 }
Damien429d7192013-10-04 19:53:11 +01002964 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002965 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002966 EMIT(return_value);
2967 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002968 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2969 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2970 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002971
2972 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002973 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002974 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002975 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002976 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002977 }
2978 #if MICROPY_EMIT_NATIVE
2979 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002980 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002981 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002982
Damien George2ac4af62014-08-15 16:45:41 +01002983 // argument annotations
2984 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2985
2986 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01002987 mp_parse_node_t pn_annotation = pns->nodes[2];
2988 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01002989 // nodes[2] can be null or a test-expr
2990 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2991 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2992 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2993 } else {
2994 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002995 }
2996 }
Damien George2ac4af62014-08-15 16:45:41 +01002997 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01002998 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002999
3000 compile_node(comp, pns->nodes[3]); // 3 is function body
3001 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003002 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003003 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003004 EMIT(return_value);
3005 }
3006 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003007 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3008 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3009 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003010
3011 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003012 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003013 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003014 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003015 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3016 }
3017
3018 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003019
3020 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3021 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3022 EMIT(pop_top);
3023 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3024 }
Damien429d7192013-10-04 19:53:11 +01003025 EMIT(return_value);
3026 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3027 // a bit of a hack at the moment
3028
Damiend99b0522013-12-21 18:17:45 +00003029 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3030 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3031 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3032 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3033 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003034
Damien George708c0732014-04-27 19:23:46 +01003035 // We need a unique name for the comprehension argument (the iterator).
3036 // CPython uses .0, but we should be able to use anything that won't
3037 // clash with a user defined variable. Best to use an existing qstr,
3038 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01003039 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01003040 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003041 bool added;
3042 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3043 assert(added);
3044 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003045 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003046 }
3047
3048 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003049 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003050 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003051 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003052 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003053 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003054 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003055 #endif
Damien429d7192013-10-04 19:53:11 +01003056 }
3057
Damien George542bd6b2015-03-26 14:42:40 +00003058 compile_load_id(comp, qstr_arg);
Damien Georgea8312432015-12-18 01:37:55 +00003059 compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
Damien429d7192013-10-04 19:53:11 +01003060
3061 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003062 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003063 }
3064 EMIT(return_value);
3065 } else {
3066 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003067 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3068 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3069 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003070
Damien George36db6bc2014-05-07 17:24:22 +01003071 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003072 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003073 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003074 assert(added);
3075 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003076 }
3077
Damien George542bd6b2015-03-26 14:42:40 +00003078 compile_load_id(comp, MP_QSTR___name__);
3079 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003080 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003081 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003082
3083 check_for_doc_string(comp, pns->nodes[2]);
3084 compile_node(comp, pns->nodes[2]); // 2 is class body
3085
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003086 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003087 assert(id != NULL);
3088 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003089 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003090 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003091 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003092 }
3093 EMIT(return_value);
3094 }
3095
Damien415eb6f2013-10-05 12:19:06 +01003096 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003097
3098 // make sure we match all the exception levels
3099 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003100}
3101
Damien George094d4502014-04-02 17:31:27 +01003102#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003103// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003104STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003105 comp->pass = pass;
3106 comp->scope_cur = scope;
3107 comp->next_label = 1;
3108
3109 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003110 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003111 return;
3112 }
3113
Damien George36db6bc2014-05-07 17:24:22 +01003114 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003115 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003116 }
3117
Damien826005c2013-10-05 23:17:28 +01003118 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003119 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3120 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3121 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003122
Damiend99b0522013-12-21 18:17:45 +00003123 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003124
Damiena2f2f7d2013-10-06 00:14:13 +01003125 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003126 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003127 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003128 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003129 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003130 if (comp->compile_error != MP_OBJ_NULL) {
3131 goto inline_asm_error;
3132 }
Damiena2f2f7d2013-10-06 00:14:13 +01003133 }
3134
Damien George8f54c082016-01-15 15:20:43 +00003135 // pns->nodes[2] is function return annotation
3136 mp_uint_t type_sig = MP_NATIVE_TYPE_INT;
3137 mp_parse_node_t pn_annotation = pns->nodes[2];
3138 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3139 // nodes[2] can be null or a test-expr
3140 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3141 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3142 switch (ret_type) {
3143 case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break;
3144 case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break;
3145 case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break;
3146 case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break;
3147 default: compile_syntax_error(comp, pn_annotation, "unknown type"); return;
3148 }
3149 } else {
3150 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3151 }
3152 }
Damien826005c2013-10-05 23:17:28 +01003153
Damiend99b0522013-12-21 18:17:45 +00003154 mp_parse_node_t pn_body = pns->nodes[3]; // body
3155 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003156 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003157
Damien826005c2013-10-05 23:17:28 +01003158 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003159 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3160 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003161 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3162 // no instructions
3163 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003164 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003165 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003166 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003167 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003168 return;
3169 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003170
3171 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003172 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003173 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3174 goto not_an_instruction;
3175 }
Damiend99b0522013-12-21 18:17:45 +00003176 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
pohmelie81ebba72016-01-27 23:23:11 +03003177 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) {
Damien George3d7bf5d2015-02-16 17:46:28 +00003178 goto not_an_instruction;
3179 }
3180 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3181 goto not_an_instruction;
3182 }
3183 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3184 goto not_an_instruction;
3185 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003186
3187 // parse node looks like an instruction
3188 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003189 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3190 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3191 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003192 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003193
3194 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003195 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003196 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003197 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003198 return;
3199 }
Damien George6f355fd2014-04-10 14:11:31 +01003200 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003201 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003202 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3203 compile_syntax_error(comp, nodes[i], "label redefined");
3204 return;
3205 }
Damien826005c2013-10-05 23:17:28 +01003206 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003207 } else if (op == MP_QSTR_align) {
3208 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003209 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003210 return;
3211 }
Damien George36db6bc2014-05-07 17:24:22 +01003212 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003213 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3214 }
3215 } else if (op == MP_QSTR_data) {
3216 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003217 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003218 return;
3219 }
Damien George36db6bc2014-05-07 17:24:22 +01003220 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003221 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003222 for (uint j = 1; j < n_args; j++) {
3223 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003224 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003225 return;
3226 }
Damien George50912e72015-01-20 11:55:10 +00003227 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003228 }
3229 }
Damien826005c2013-10-05 23:17:28 +01003230 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003231 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003232 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003233 }
3234 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003235
3236 if (comp->compile_error != MP_OBJ_NULL) {
3237 pns = pns2; // this is the parse node that had the error
3238 goto inline_asm_error;
3239 }
Damien826005c2013-10-05 23:17:28 +01003240 }
3241
Damien George36db6bc2014-05-07 17:24:22 +01003242 if (comp->pass > MP_PASS_SCOPE) {
Damien George8f54c082016-01-15 15:20:43 +00003243 EMIT_INLINE_ASM_ARG(end_pass, type_sig);
Damien George8dfbd2d2015-02-13 01:00:51 +00003244 }
3245
3246 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003247 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003248 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003249 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003250 }
Damien429d7192013-10-04 19:53:11 +01003251}
Damien George094d4502014-04-02 17:31:27 +01003252#endif
Damien429d7192013-10-04 19:53:11 +01003253
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003254STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003255 // in Micro Python we put the *x parameter after all other parameters (except **y)
3256 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3257 id_info_t *id_param = NULL;
3258 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3259 id_info_t *id = &scope->id_info[i];
3260 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3261 if (id_param != NULL) {
3262 // swap star param with last param
3263 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3264 }
3265 break;
3266 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3267 id_param = id;
3268 }
3269 }
3270 }
Damien George2827d622014-04-27 15:50:52 +01003271
Damien429d7192013-10-04 19:53:11 +01003272 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003273 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003274 scope->num_locals = 0;
3275 for (int i = 0; i < scope->id_info_len; i++) {
3276 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003277 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003278 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3279 continue;
3280 }
3281 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3282 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3283 }
Damien George2827d622014-04-27 15:50:52 +01003284 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003285 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003286 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003287 }
3288 }
3289
Damien George65dc9602015-08-14 12:24:11 +01003290 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003291 for (int i = 0; i < scope->id_info_len; i++) {
3292 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003293 // in Micro Python the cells come right after the fast locals
3294 // parameters are not counted here, since they remain at the start
3295 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003296 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003297 id->local_num = scope->num_locals;
3298 scope->num_locals += 1;
3299 }
Damien9ecbcff2013-12-11 00:41:43 +00003300 }
Damien9ecbcff2013-12-11 00:41:43 +00003301
Damien George65dc9602015-08-14 12:24:11 +01003302 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003303 // make sure they are in the order of the parent scope
3304 if (scope->parent != NULL) {
3305 int num_free = 0;
3306 for (int i = 0; i < scope->parent->id_info_len; i++) {
3307 id_info_t *id = &scope->parent->id_info[i];
3308 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3309 for (int j = 0; j < scope->id_info_len; j++) {
3310 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003311 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003312 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003313 // in Micro Python the frees come first, before the params
3314 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003315 num_free += 1;
3316 }
3317 }
3318 }
Damien429d7192013-10-04 19:53:11 +01003319 }
Damien George6baf76e2013-12-30 22:32:17 +00003320 // in Micro Python shift all other locals after the free locals
3321 if (num_free > 0) {
3322 for (int i = 0; i < scope->id_info_len; i++) {
3323 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003324 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003325 id->local_num += num_free;
3326 }
3327 }
Damien George2827d622014-04-27 15:50:52 +01003328 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 +00003329 scope->num_locals += num_free;
3330 }
Damien429d7192013-10-04 19:53:11 +01003331 }
Damien429d7192013-10-04 19:53:11 +01003332}
3333
Damien Georged4dba882015-11-13 13:38:28 +00003334#if !MICROPY_PERSISTENT_CODE_SAVE
3335STATIC
3336#endif
3337mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
Damien George9d5e5c02015-09-24 13:15:57 +01003338 // put compiler state on the stack, it's relatively small
3339 compiler_t comp_state = {0};
3340 compiler_t *comp = &comp_state;
3341
Damien Georgecbd2f742014-01-19 11:48:48 +00003342 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003343 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003344
Damien George62a3a282015-03-01 12:04:05 +00003345 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003346 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003347
Damien Georgea210c772015-03-26 15:49:53 +00003348 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003349 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003350
Damien826005c2013-10-05 23:17:28 +01003351 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003352 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003353 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003354 comp->emit_method_table = &emit_bc_method_table;
3355 #endif
Damien826005c2013-10-05 23:17:28 +01003356 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003357 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003358 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003359#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003360 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003361 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003362#endif
Damien826005c2013-10-05 23:17:28 +01003363 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003364 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003365 }
3366
3367 // update maximim number of labels needed
3368 if (comp->next_label > max_num_labels) {
3369 max_num_labels = comp->next_label;
3370 }
Damien429d7192013-10-04 19:53:11 +01003371 }
3372
Damien826005c2013-10-05 23:17:28 +01003373 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003374 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003375 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003376 }
3377
Damien Georgea210c772015-03-26 15:49:53 +00003378 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003379 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003380
Damien826005c2013-10-05 23:17:28 +01003381 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003382#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003383 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003384#endif
Damien3ef4abb2013-10-12 16:53:13 +01003385#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003386 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003387#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003388 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003389 if (false) {
3390 // dummy
3391
Damien3ef4abb2013-10-12 16:53:13 +01003392#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003393 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003394 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003395 if (emit_inline_thumb == NULL) {
3396 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3397 }
3398 comp->emit = NULL;
Damien826005c2013-10-05 23:17:28 +01003399 comp->emit_inline_asm = emit_inline_thumb;
3400 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003401 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003402 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003403 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003404 }
Damienc025ebb2013-10-12 14:30:21 +01003405#endif
3406
Damien826005c2013-10-05 23:17:28 +01003407 } else {
Damienc025ebb2013-10-12 14:30:21 +01003408
3409 // choose the emit type
3410
Damien826005c2013-10-05 23:17:28 +01003411 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003412
3413#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003414 case MP_EMIT_OPT_NATIVE_PYTHON:
3415 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003416#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003417 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003418 emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003419 }
Damien13ed3a62013-10-08 09:05:10 +01003420 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003421#elif MICROPY_EMIT_X86
3422 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003423 emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels);
Damien Georgec90f59e2014-09-06 23:06:36 +01003424 }
3425 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003426#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003427 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003428 emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels);
Damienc025ebb2013-10-12 14:30:21 +01003429 }
3430 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003431#elif MICROPY_EMIT_ARM
3432 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003433 emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003434 }
3435 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003436#endif
3437 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003438 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003439 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003440#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003441
Damien826005c2013-10-05 23:17:28 +01003442 default:
Damien826005c2013-10-05 23:17:28 +01003443 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003444 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003445 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003446 #endif
Damien826005c2013-10-05 23:17:28 +01003447 break;
3448 }
Damienc025ebb2013-10-12 14:30:21 +01003449
Damien George1e1779e2015-01-14 00:20:28 +00003450 // need a pass to compute stack size
3451 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3452
Damien George36db6bc2014-05-07 17:24:22 +01003453 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003454 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003455 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3456 }
3457
3458 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003459 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003460 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003461 }
Damien6cdd3af2013-10-05 18:08:26 +01003462 }
Damien429d7192013-10-04 19:53:11 +01003463 }
3464
Damien Georgecfc4c332015-07-29 22:16:01 +00003465 if (comp->compile_error != MP_OBJ_NULL) {
3466 // if there is no line number for the error then use the line
3467 // number for the start of this scope
3468 compile_error_set_line(comp, comp->scope_cur->pn);
3469 // add a traceback to the exception using relevant source info
3470 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3471 comp->compile_error_line, comp->scope_cur->simple_name);
3472 }
3473
Damien George41d02b62014-01-24 22:42:28 +00003474 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003475
Damien Georgea210c772015-03-26 15:49:53 +00003476 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003477#if MICROPY_EMIT_NATIVE
3478 if (emit_native != NULL) {
3479#if MICROPY_EMIT_X64
3480 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003481#elif MICROPY_EMIT_X86
3482 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003483#elif MICROPY_EMIT_THUMB
3484 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003485#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003486 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003487#endif
3488 }
3489#endif
3490#if MICROPY_EMIT_INLINE_THUMB
3491 if (emit_inline_thumb != NULL) {
3492 emit_inline_thumb_free(emit_inline_thumb);
3493 }
3494#endif
Damien George41d02b62014-01-24 22:42:28 +00003495
Damien George52b5d762014-09-23 15:31:56 +00003496 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003497 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003498
Damien George41d02b62014-01-24 22:42:28 +00003499 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003500 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003501 for (scope_t *s = module_scope; s;) {
3502 scope_t *next = s->next;
3503 scope_free(s);
3504 s = next;
3505 }
Damien5ac1b2e2013-10-18 19:58:12 +01003506
Damien George9d5e5c02015-09-24 13:15:57 +01003507 if (comp->compile_error != MP_OBJ_NULL) {
3508 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003509 } else {
Damien Georged4dba882015-11-13 13:38:28 +00003510 return outer_raw_code;
Damien George1fb03172014-01-03 14:22:03 +00003511 }
Damien429d7192013-10-04 19:53:11 +01003512}
Damien Georged4dba882015-11-13 13:38:28 +00003513
3514mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3515 mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl);
3516 // return function that executes the outer module
3517 return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
3518}
Damien Georgedd5353a2015-12-18 12:35:44 +00003519
3520#endif // MICROPY_ENABLE_COMPILER